// subPackage/page/pages/exchangeAddress/exchangeAddress.js
const app = getApp()
const wxService = require('../../../../utils/wxService')

wxService.page({

  /**
   * 页面的初始数据
   */
  data: {
    region: [],
    point: 0,
    dialogStockEmpty: false,
    form: {
      activateId: '',
      name: '',
      mobile: '',
      distinct: '',
      address: '',
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.form.activateId = options.id;
    this.getIntegralCouponDetail(this.data.form.activateId);
  },
  //获取积分换物详情
  //获取积分兑换优惠券详情
  getIntegralCouponDetail(id) {
    wx.showLoading({
      title: '加载中'
    });
    wxService.get(`/member/pointsRedemptionProductSetting/get/${id}`).then(res => {
      const { result, data } = res.data
      if (result == 0) {
        wx.hideLoading();
        this.setData({
          point: data.point ? data.point : 0
        });
      }
    }).finally(() => {
      wx.hideLoading()
    })
  },
  //省市区改变事件
  bindRegionChange(e) {
    this.data.form.distinct = e.detail.value.join(',');
    this.setData({
      region: e.detail.value,
      form: this.data.form
    });
  },

  //选择微信地址
  chooseWxAddress() {
    let self = this;
    wx.chooseAddress({
      success(res) {
        self.data.region = [res.provinceName, res.cityName, res.countyName];
        self.data.form.distinct = self.data.region.join(',');
        self.data.form.name = res.userName;
        self.data.form.mobile = res.telNumber;
        self.data.form.address = res.detailInfo;

        self.setData({
          region: self.data.region,
          form: self.data.form
        });
      },
      fail(err) {

      }
    })
  },

  //输入框
  onInput(e) {
    let prop = e.target.dataset.prop,
      value = e.detail.value;

    this.data.form[prop] = value;
    this.setData({
      form: this.data.form
    })

  },
  //确认提交
  onTapSubmit() {
    if (!this.data.form.name) {
      wx.showToast({
        title: '请输入收货人真实姓名',
        icon: 'none'
      });
      return;
    }
    if (!this.data.form.mobile) {
      wx.showToast({
        title: '请输入收货人手机号',
        icon: 'none'
      });
      return;
    }
    if (!this.data.form.distinct) {
      wx.showToast({
        title: '请选择省市区',
        icon: 'none'
      });
      return;
    }
    if (!this.data.form.address) {
      wx.showToast({
        title: '请输入详细地址',
        icon: 'none'
      });
      return;
    }

    wx.showLoading({
      title: '兑换中..',
    });

    wxService.post(`/member/pointsRedemptionProductSetting/exchange`, this.data.form).then(res => {
      wx.hideLoading();
      if (res.data.data == 0) {
        wx.showToast({
          title: '兑换成功',
        });

        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          });
        }, 300);
      }
      else if (res.data.data == 1) { // 库存不足
        this.setData({
          dialogStockEmpty: true,
        });
      }
      else if (res.data.data == 2) {
        wx.showToast({
          title: '对不起，您积分不足',
          icon: 'none'
        });
      }
      else {
        wx.showToast({
          title: '您已经达到上限了哦',
          icon: 'none'
        });
      }
    });
  },
  //btn回调
  onDialogBtnCallBack(e) {
    let pages = getCurrentPages();
    let index = pages.length - 1;
    for (let i = 0; i < pages.length; i++) {
      if (pages[i].route.indexOf('IntegralMallGoods/IntegralMallGoods') > -1) {
        index = index - i;
        break;
      }
    }

    wx.navigateBack({
      delta: index
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

})