// pages/integralMallDetail/integralMallDetail.js
const app = getApp()
const wxService = require('../../../../utils/wxService')
const utils = require('../../../../utils/util')
const envInfo = require('../../../../config/index').envInfo


wxService.page({
  /**
   * 页面的初始数据
   */
  data: {
    isExchange: false,
    dialogUserLimit : false,
    integralMallDetail: {}
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const { id } = this.options
    // this.getIntegralCouponDetail('579702812007600128')
    this.getIntegralCouponDetail(id)
  },

  //立即兑换
  exchange() {
    const { memberId } = wx.getStorageSync('_baseUserInfo');
    const id = this.data.integralMallDetail.id;
    //检查是不是可以兑换
    wx.showLoading({
      title: '检查兑换资格中..',
    });
    wxService.post(`/member/pointsRedemptionProductRecord/checkByMemberIdAndSettingId?memberId=${memberId}&settingId=${id}`).
    then(res => {
      if (res.data.data == -1) {
        this.setData({
          dialogUserLimit: true
        })
      }
      else {
        wx.navigateTo({
          url: '/subPackage/page/pages/exchangeAddress/exchangeAddress?id=' + id,
        });
      }
    });
  },

  //获取积分兑换优惠券详情
  getIntegralCouponDetail(id) {
    wx.showLoading({
      title: '加载中'
    })
    wxService.get(`/member/pointsRedemptionProductSetting/get/${id}`).then(res => {
      const { result, data } = res.data
      if (result == 0) {
        wx.hideLoading()
        data.desc = JSON.parse(data.desc)
        this.setData({
          integralMallDetail: data
        });
      }
    }).finally(() => {
      wx.hideLoading()
    })
  }
})