const wx2my = require('../../wx2my');
const Behavior = require('../../Behavior');
// pages/couponCodeDetail.js
const app = getApp();

const wxService = require('../../utils/wxService');

const utils = require('../../utils/util');

const envInfo = require('../../config/index').envInfo;

var wxbarcode = require('../../codeUtils/index.js');

wxService.page({
  /**
   * 页面的初始数据
   */
  data: {
    cardno: '',
    codeDetail: {}
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const {
      cardno
    } = this.options;
    this.setData({
      cardno
    }); // 条形码

    if (cardno) {
      setTimeout(() => {
        wxbarcode.barcode('barcode', cardno, 600, 180);
      }, 200);
      this.getCouponDetail(cardno);
    }
  },

  onLoad() {
    wx2my.hideShareMenu(); // const {cardno} = this.data
    //   // 条形码
    //   if(cardno){
    //     wxbarcode.barcode('barcode', cardno, 600, 180);
    //   }
  },

  canvasIdErrorCallback(e) {
    console.log('code', e.detail.errMsg);
  },

  //  查询单个优惠券
  getCouponDetail(cardno) {
    wx2my.showLoading({
      title: '加载中'
    });
    wxService.post(`/coupon/coupon/get?cardNo=${cardno}`).then(res => {
      const {
        result,
        data
      } = res.data;

      if (result == 0) {
        this.setData({
          codeDetail: data
        }, () => {
          wx2my.hideLoading();
        });
      } else {
        wx2my.hideLoading();
        wx2my.showToast({
          title: `${res.desc}`,
          icon: 'none'
        });
      }
    }).finally(() => {});
  }

});