// pages/home/index.js
const app = getApp()
import QRCode from '../../utils/weapp-qrcode'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    height: app.globalData.height * 2 + 25,
    navbarData: {
      name: '节俭会'
    },
    background: '',
    tenant: '',
    heat: 80,
    heatColor: {
      '0%': '#E1FFFF',
      '25%': '#00FF7F',
      '50%': '#191970',
      '75%': '#ee0a24',
      '100%': '#ff0000',
    },
    thrList: [
      {
        name: '菜名',
        num: 1,
        reserve: '100'
      },
      {
        name: '菜名',
        num: 1,
        reserve: '100'
      },
      {
        name: '菜名',
        num: 1,
        reserve: '100'
      },
    ],
    thrTitle: ''
  },
  // 查看本周菜谱
  reserveBtn: function () {
    wx.redirectTo({
      url: '/pages/food/index',
    })
  },
  // 报餐：1，余菜打包：2，卡路里计算：3
  twoBtn: function (e) {
    let type = e.currentTarget.dataset.smile
    if (type == '1') {
      wx.switchTab({
        url: '/pages/reserve/index',
      })
    } else if (type == '2') {
      wx.showToast({
        title: '暂未开通',
      })
    } else {
      wx.showToast({
        title: '暂未开通',
      })
      // wx.redirectTo({
      //   url: '/pages/calorie/index',
      // })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.request({
      url: `${app.globalData.publicUrl}/api/st/system/tenant/getTenant/${app.globalData.tenant}`,
      method: "get",
      header: { 'x-tenant': app.globalData.tenant },
      data: {},
      success: res => {
        const { data } = res.data;
        this.setData({ background: `${app.globalData.fileUrl}` + data.img.home })
        this.setData({ tenant: data.name })
      },
      error: err => {
        wx.showToast({
          title: err.msg,
          icon: 'error'
        })
      }
    })
    wx.getWeRunData({
      success(res) {
        const { encryptedData, iv } = res
        const session_key = app.globalData.wxInfo.session_key
        const data = { encryptedData, iv, session_key }
        // // 拿 encryptedData 到开发者后台解密开放数据
        wx.request({
          url: `${app.globalData.publicUrl}/api/st/system/weixin/decrypt`,
          method: "POST",
          data,
          header: {
            'x-tenant': app.globalData.tenant
          },
          success: (res) => {
            console.log(res);
          },
        })
      }
    })
    app.toLogin().then(res => {
      this.getMealCard();
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  getMealCard() {
    const openid = app.globalData.wxInfo.openid;
    if (!openid) return;
    wx.request({
      url: `${app.globalData.publicUrl}/api/st/dining/order/mealCard/${openid}`,
      method: "POST",
      header: {
        'x-tenant': app.globalData.tenant
      },
      success: (res) => {
        if (res.data.errcode === 0) {
          const { data } = res.data
          this.initQrCode(data.data._id)
          this.setData({
            thrList: data.data.list || [],
            thrTitle: data.type
          })
        }
      },
      error: (err) => {
        console.error(err.data.errmsg)
      }
    })
  },
  initQrCode(id) {
    const url = `${app.globalData.publicUrl}/api/st/dining/order/useMeal/${id}?_tenant=${app.globalData.tenant}`;
    console.log(url);
    var qrcode = new QRCode(`myQrcode`, {
      text: url,
      width: 130,
      height: 130,
      padding: 3,
      colorDark: "#000000",
      colorLight: "#ffffff",
      correctLevel: QRCode.CorrectLevel.L,
    });
  }
})