// 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)
    // options.id = '651730112529698816';
    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.data.subStoreData = data.subStore ? JSON.parse(data.subStore) : [];
        this.setData({
          couponDetail: data,
          subStoreData: this.data.subStoreData
        });

        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()
         this.setData({
            productData: data.couponDetail && data.couponDetail.useCondition && data.couponDetail.useCondition.productData,
         })
      }
    }).finally(() => {
      wx.hideLoading()
    })
  },
})