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

wxService.page({
  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    couponDetail: null,
    productData: [],
    subStoreData: [],
    showDialig: false,
    proData: [
      {
        "id": "636202018767245312",
        "classifyId": "621357257015300096",
        "headline": "2222",
        "type": 0,
        "memberPrice": 57700,
        "plusPrice": 32550,
        "transmitSum": 0,
        "index": 0,
        "url": "https://img3.bigaka.com/test/1001/201910/20191022/10015eb58657-67ec-43c2-9344-c277e0032742.jpg",
        "currentType": 1,
        "explain": "测试文案001/测试文案002/测试文案003"
      },
      {
        "id": "636201959732416512",
        "classifyId": "621357257015300096",
        "headline": "2222",
        "type": 0,
        "memberPrice": 548,
        "plusPrice": 220,
        "transmitSum": 0,
        "index": 0,
        "url": "https://img3.bigaka.com/test/1001/201910/20191022/1001ccee65c3-cced-4518-bbad-16412d671075.png",
        "currentType": 1,
        "explain": "Bigaka Bigaka 玫瑰金皮带腕表"
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  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 () {
    console.log('--------', this.options)
    this.setData({
      id: '638467647583031296'
    },()=>{
      this.getCouponDetail(this.data.id)
    })
  },
  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 () {

  }
})