const { default: activity } = require("../../api/activity");
import QRCode from '../../utils/weapp-qrcode.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activityId:0, //活动id
    userMobile:'', //电话
    codeId:'',//code唯一码
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('接受参数');
    console.log(options);
    let _self = this;
    if(options.id){
      this.activityInfo(options.id)
      _self.setData({
        activityId:options.id,
      })
    }
    //用户手机
    if(options.mobile) {
      _self.setData({
        userMobile:options.mobile
      })
    }
    if(_self.data.activityId!=0 && _self.data.userMobile!='') {
      //获取code
      _self.getCode()
    }

  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 获取活动详细信息
   * @param {*} activityId 
   */
   async activityInfo(activityId){
    let result = await activity.getActivityDetail(activityId);
    console.log('获取活动详细信息');
    console.log(result)
    var objSwitch = getApp().globalData.typeSwitchObj

      var couponTypeAndNumObj = {};
      var couponList = result.activityCouponList;
      for (let j = 0; j < couponList.length; j++) {
        var couponNum = couponList[j]['num'] ? couponList[j]['num'] : 1;

        var value = couponList[j]['coupon']['type'];
        var showText = objSwitch[couponList[j]['coupon']['type']];
        
        var nowNume = 0;
        if (couponTypeAndNumObj[value]) {
          nowNume = couponTypeAndNumObj[value]['num'];
          console.log(nowNume)
        }
        couponTypeAndNumObj[value] = {
          "typeText": showText,
          "num": couponNum + nowNume
        }
    
      }
      result.couponTypeAndNumObj = couponTypeAndNumObj
    this.setData({
      activityInfo:result
    })
  },

  /**
   * 获取分享码
   */
  async getCode() {
    let _self = this;
    let res = await activity.createShareActivityCode(_self.data.activityId,_self.data.userMobile);
    _self.setData({
      codeId:res.codeId,
    })
    //根据codeId 生成二维码
    new QRCode('myQrcode',{
      // text: 'https://oapi.shpr.top?id=Bg5740r78w',
      text: 'https://oapi.shpr.top?id='+_self.data.codeId,
      width: 150,
      height:150,
      padding: 10, // 生成二维码四周自动留边宽度，不传入默认为0
      correctLevel: QRCode.CorrectLevel.L, // 二维码可辨识度
      callback: (res) => {
        console.log(res.path)
        // 接下来就可以直接调用微信小程序的api保存到本地或者将这张二维码直接画在海报上面去，看各自需求
      }
    })
  }
})