const wx2my = require('../../wx2my');
const Behavior = require('../../Behavior');
// component/exchange-coupon-modal/exchange-coupon-modal.js
var app = getApp();

const wxService = require('../../utils/wxService');

const envInfo = require('../../config/index').envInfo;

Component({
  /**
   * 组件的属性列表
   */
  props: {
    isExchange: false,
    exchangePoint: 0,
    exchangeId: ''
  },
  /**
   * 组件的初始数据
   */
  data: {
    couponNum: '1'
  },

  /**
   * 组件的方法列表
   */
  methods: {
    minusNum() {
      let number = this.data.couponNum;

      if (this.data.couponNum > 1) {
        number--;
        this.setData({
          couponNum: number
        });
      } else {
        wx2my.showToast({
          title: '数量不能为0',
          icon: 'none'
        });
      }
    },

    addNum() {
      let number = this.data.couponNum;

      if (this.data.couponNum < 500) {
        number++;
        this.setData({
          couponNum: number
        });
      } else {
        wx2my.showToast({
          title: '数量不能超过库存',
          icon: 'none'
        });
      }
    },

    // 再想想
    cancel() {
      this.setData({
        isExchange: false
      });
    },

    // 立即兑换
    handelRedeemNow() {
      console.log('this.prophandelRedeemNows', this.props)
      wx2my.showLoading({
        title: '加载中'
      });
      const params = {
        activateId: this.props.exchangeId
      };
      wxService.post(`/coupon/pointsRedemptionCouponSetting/exchange`, this.props.exchangeId).then(res => {
        if (res) {
          if (res.data.result == 0) {
            wx2my.hideLoading();
            wx2my.showToast({
              title: '兑换成功',
              icon: 'success'
            });
            this.setData({
              isExchange: false
            });
          }
        } else {
          this.setData({
            isExchange: false
          });
        }
      }).catch(err => {
        console.log('err', err);
      });
    }

  }
});