// shoppingGuid/page/pages/couponQrcode/couponQrcode.js
const wxService = require('../../../../utils/wxService')
import { Integer } from '../../../../utils/integerDigitalConvertion'
const app = getApp();

wxService.page({

  /**
   * 页面的初始数据
   */
  data: {
    qrcodeImg : '',
    couponInfo : {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let couponInfo = wx.getStorageSync('_guidCouponInfo');
    this.setData({
      couponInfo: couponInfo,
    },() => {
      this.getMiniQrcode(couponInfo.id);
    })
    
  },

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

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    wx.removeStorageSync('_guidCouponInfo');
  },


  //获取小程序码
  getMiniQrcode(id){
    // subPackage / page / pages / scanCoupon / scanCoupon
    let currentUserInfo = wx.getStorageSync('guidBaseInfo');
    let memberId = currentUserInfo.memberId;
    console.log(this.data.couponInfo)

    let content = {
      title: this.data.couponInfo.couponName,
      id: this.data.couponInfo.couponId,
      url: this.data.couponInfo.imageBg
    }
    let tentacleInfo = {
      content: JSON.stringify(content),
      contentId: this.data.couponInfo.couponId,
      contentType: app.globalData.contants.SHARE_TYPE.PRODUCT_DETAIL, //内容类型 STAFF_RECOMMAND
      title: this.data.couponInfo.couponName, //标题
      type: 3// 1:门店,2:员工(暂时不做),3:会员,4:第三方外部渠道,5:智能营销
    }

    wxService.getTentacleContent(tentacleInfo).then(res => {
      if (res && res.id) {
        let tentacleId = res.id;
        let inner_id = Integer.digit(this.data.couponInfo.couponId, 10, 64);
        let inner_tentacleId = Integer.digit(tentacleId, 10, 64);
        //生成二维码

        let data = {
          "autoColor": true,
          "page": 'subPackage/page/pages/scanCoupon/scanCoupon',
          "scene": '?i=' + inner_id + '&t=' + inner_tentacleId,
          "width": 260
        }
        wxService.post(`/marketing/quickMark/getAppQrCodePicture`, data).then(res => {
          const { result, data } = res.data
          if (result == 0) {
            this.setData({
              qrcodeImg: app.globalData.imageUrl + data
            });
          }
        })
      }
    });
  },
})