// subPackage/page/pages/scanCoupon/scanCoupon.js
const wxService = require('../../../../utils/wxService')

wxService.page({
  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    couponDetail: null,
    productData: [],
    subStoreData: [],
    showDialig: false,
    proData: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('options', options)
    console.log(this.data)
    this.setData({
      id: options.id
    },()=>{
      this.getCouponDetail(this.data.id)
    })
  },
  closeDialog() {
    this.setData({
      showDialig: false
    },()=>{
    // 分类
    wxService.router(`/pages/category/category`)
    })
  },
  handelGoStoreList(e){
    const { list } = e.currentTarget.dataset
    const storeList = list ? JSON.stringify(list) : []
    wxService.router(`/subPackage/page/pages/scanCouponStoreList/scanCouponStoreList`).search({ storeList })
  },

  handelGet() {
    const { id } = this.data
    wx.showLoading({
      title: '加载中'
    })

    wxService.post(`/marketing/shoppingguide/coupon/exchange`,id).then(res => {
      const { result, data } = res.data
      if (result == 0) {
        wx.hideLoading()
        this.setData({
          showDialig: true
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },
  getCouponDetail(id){
    wx.showLoading({
      title: '加载中'
    })
    wxService.get(`/marketing/shoppingguide/coupon/detail?id=${id}`).then(res => {
      const { result, data } = res.data
      if (result == 0) {
        wx.hideLoading()
        this.setData({
          couponDetail: data
        })
        this.getCouponGoods(data.couponId)
      }
    })
  },
   // 卡券详情
   getCouponGoods(id){
    wx.showLoading({
      title: '加载中'
    })
    wxService.post(`/coupon/coupon/setting/get/${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({
            productData: data.couponDetail && data.couponDetail.useCondition && data.couponDetail.useCondition.productData,
            subStoreData: data.couponDetail && data.couponDetail.useCondition && data.couponDetail.useCondition.subStoreData
         })
      }
    }).finally(() => {
      wx.hideLoading()
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})