// component/exchange-coupon-modal/exchange-coupon-modal.js
var app = getApp()
const wxService = require('../../utils/wxService')
const envInfo = require('../../config/index').envInfo

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    isExchange:{
      type: Boolean,
      value: false
    },
    exchangePoint:{
      type: Number,
      value: 0
    },
    exchangeId: {
      type: String,
      value: ''
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    couponNum:'1'
  },

  /**
   * 组件的方法列表
   */
  methods: {
    minusNum(){
      let number = this.data.couponNum
      if (this.data.couponNum > 1) {
        number--
        this.setData({
          couponNum: number
        })
      } else {
        wx.showToast({
          title: '数量不能为0',
          icon: 'none'
        })
      }
    },
    addNum(){
      let number = this.data.couponNum
      if (this.data.couponNum < 500) {
        number++
        this.setData({
          couponNum: number
        })
      } else {
        wx.showToast({
          title: '数量不能超过库存',
          icon: 'none'
        })
      }
    },
    // 再想想
    cancel(){
      this.setData({
        isExchange: false
      })
    },
    // 立即兑换
    handelRedeemNow(){
      wx.showLoading({
        title: '加载中'
      })
      const params = {
        activateId: this.data.exchangeId
      }
      wxService.post(`/coupon/pointsRedemptionCouponSetting/exchange`, this.data.exchangeId).then(res => {
      if(res){
        if(res.data.result == 0){
          wx.hideLoading()
          wx.showToast({
            title: '兑换成功',
            icon: 'success'
          })
          this.setData({
            isExchange: false
          })
        }
      } else  {
        this.setData({
          isExchange: false
        })
      }
      }).catch(err => {
       console.log('err',err)
      })
    }
  }
})
