// subPackage/page/pages/myValueCardInfo/myValueCardInfo.js
const wxService = require('../../../../utils/wxService')
const utils = require('../../../../utils/util')
var wxbarcode = require('../../../../codeUtils/index.js');
wxService.page({

  /**
   * 页面的初始数据
   */
  data: {
    supValCardId : null,
    no: '',
    valueCardInfo : {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.supValCardId = options.id;
    wx.hideShareMenu();
    this.getMyValueCardInfo();
  },

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

  },
  //获取我的超值卡详情
  getMyValueCardInfo(){
    let supValCardId = this.data.supValCardId;
    let url = `/merchant/superValueCard/miniProgram/getDetail?supValCardId=${supValCardId}`;
    wxService.post(url).then(res => {
      if(res){
        let obj = res.data.data;
        obj.leftTimes = obj.totalTimes - obj.usedTimes;
        // obj.
        //处理图标显示问题
        let arr = [];
        for (let i = 0; i < obj.totalTimes; i ++){
          let tempObj = {
            icon: obj.checkFlagUrl,
          };
          arr.push(tempObj);
        }

        for (let j = 0; j < obj.leftTimes ; j ++){
          arr[j].icon = obj.noCheckFlagUrl;
        }

        obj.icons = arr;
        obj.startTime = obj.startTime.substring(0, 10);
        obj.endTime = obj.endTime.substring(0,10);
        if (obj.buyAwayType == 1){
          obj.buyPriceText = parseFloat(obj.buyPrice/10/10).toFixed(2);
        }

        obj.activityDesc = obj.activityDesc ? JSON.parse(obj.activityDesc) : []; 
        obj.hasDate = obj.activityDesc.filter(item => item.type == 1).length > 0;
        let descArr = JSON.parse(JSON.stringify(obj.activityDesc));
        descArr = descArr.filter(item => item.type != 1);
        // let tempArr = descArr.map(item => {
        //   return item.value;
        // });

        obj.descArr = descArr;
        this.data.valueCardInfo = obj;
        this.setData({
          valueCardInfo: this.data.valueCardInfo,
          no: obj.supValCardCode
        },() => {
          this.generatorBarCode();
        });
      }
    });
  },
  //刷新
  onTapRefreshNo(){
    let supValCardId = this.data.supValCardId;
    let url = `/merchant/superValueCard/miniProgram/getChangeCode?supValCardId=${supValCardId}`;
    wxService.post(url).then(res => {
      if (res) {
        if (res.data.data) {
          let no = res.data.data;
          this.data.no = no;
          this.setData({
            no: this.data.no,
          },() => {
            this.generatorBarCode();
            wx.showToast({
              title: '刷新成功!',
            });
          })
        }
        else {
          wx.showToast({
            title: '刷新失败，请重试',
            icon: ' none'
          })
        }
      }
    });
  },

  //生成条形码
  generatorBarCode() {
    if (this.data.no) {
      wxbarcode.barcode('barcode', this.data.no, 570, 140);
      setTimeout(() => {
        wxbarcode.barcode('barcode', this.data.no, 570, 140);
      }, 200);
    }
  },

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

  },

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

  },
})