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

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

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

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

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

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

  onLoad(options) {
    console.log('caoptions', options)
    const {
      cardno
    } = options;
    
    this.setData({
      cardno
    },()=>{
      this.getCouponDetail(cardno);
    });
    wx2my.hideShareMenu();
  },

  // 卡券详情
  getCouponDetail(id) {
    wx2my.showLoading({
      title: '加载中'
    });
    wxService.post(`/coupon/coupon/get?cardNo=${id}`).then(res => {
      const {
        result,
        data
      } = res.data;

      if (result == 0) {
        wx2my.hideLoading();
        data.coupon.startTime = data.coupon.startTime.substring(0, 10);
        data.coupon.endTime = data.coupon.endTime.substring(0, 10);
        data.couponSetting.notice = JSON.parse(data.couponSetting.notice);

        for (let i in data.couponSetting.notice) {
          if (data.couponSetting.notice[i].type == 1) {
            data.couponSetting.notice[i].value = data.coupon.startTime.substring(0, 10) + ' 至 ' + data.coupon.endTime.substring(0, 10);
          }
        }

        this.setData({
          couponDetail: data
        });
      }
    }).finally(() => {
      wx2my.hideLoading();
    });
  },

  handelPresentCouponCode(e) {
    // let data = e.currentTarget.dataset
    const {
      cardno
    } = this.data;
    wxService.router(`/pages/couponCodeDetail/couponCodeDetail`).search({
      cardno
    });
  }

});