// 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 () {
    const {cardno} = this.options
    this.getCouponDetail(cardno)
    this.setData({
      cardno
    })
  },
  onLoad() {
    wx.hideShareMenu();
  },

  // 卡券详情
  getCouponDetail(id){
    wx.showLoading({
      title: '加载中'
    })
    wxService.post(`/coupon/coupon/get?cardNo=${id}`).then(res => {
      const {result,data} = res.data
      if(result == 0){
         wx.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(() => {
      wx.hideLoading()
    })
  },
  handelPresentCouponCode(e) {
    // let data = e.currentTarget.dataset
    const {cardno} = this.data
    wxService.router(`/pages/couponCodeDetail/couponCodeDetail`).search({cardno})
  }
})