// packageC/CouponExchange/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    list: [],
    mallModel: [],
    numModel: [],
    num: 0,
    isNoData: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

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

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  getData() {
    // 获取兑换列表
    this.into();
    let that = this;
    let urlStr = app.getNetAddresss("coupon.exchange-center.index");
    app._getNetWork({
      url: urlStr,
      data: { platform_id: 0},
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            list: res.data.list
          });
          if (app._isTextEmpty(res.data.list)) {
            that.setData({
              isNoData: true
            });
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  into() {
    this.setData({
      list: [],
      mallModel: [],
      numModel: [],
      num: 0,
      isNoData: false
    });
  },
  numberLeft(e){
    let _index = e.target.dataset.index;
    let numModels = this.data.numModel;
    let mallModels = this.data.mallModel;
    if (this.data.numModel[_index]){
      numModels[_index] = numModels[_index] -1;
      if (numModels[_index] == 0) {
        mallModels.forEach((item, ind) => {
          if (item.coupon_id == e.target.dataset.info.coupon_id) {
            return mallModels.splice(ind, 1);
          }
        });
      }else{
        mallModels.forEach((item, ind) => {
          if (item.coupon_id == e.target.dataset.info.coupon_id) {
            return (mallModels[ind].total =
              parseInt(mallModels[ind].total) - 1);
          }
        });
      }

      this.setData({
        numModel: numModels,
        mallModel: mallModels,
        num: this.data.num - 1
      });

    }else{
      console.log("error");
    }
  },
  numberRight(e){
    console.log(e);
    let goods_ids = e.target.dataset.info.belongs_to_coupon.goods_ids[0];
    let json = {
      coupon_id: e.target.dataset.info.coupon_id,
      goods_id: goods_ids.has_many_options && goods_ids.has_many_options.length > 0 ? goods_ids.id : goods_ids,
      option_id:goods_ids.has_many_options && goods_ids.has_many_options.length > 0 ? goods_ids.has_many_options[0].id : ''
    };
    let _index = e.target.dataset.index;
    let numModels = this.data.numModel;
    let mallModels = this.data.mallModel;
    if (this.data.numModel[_index]) {
      if (numModels[_index] + 1 > e.target.dataset.info.total){
        wx.showToast({
          title: '该券数量不足',
          icon: 'none',
          duration: 1000
        });
        return;
      }
      numModels[_index] = numModels[_index] + 1;
      mallModels.forEach((item, ind) => {
        if (item.coupon_id == e.target.dataset.info.coupon_id) {
          return (mallModels[ind].total =
            parseInt(mallModels[ind].total) + 1);
        }
      });
      this.setData({
        numModel: numModels,
        mallModel: mallModels,
        num: this.data.num +1
      });
    } else {
      numModels[_index] = 1;
      json.total=1;
      mallModels.push(json);
      this.setData({
        numModel: numModels,
        mallModel: mallModels,
        num: this.data.num + 1
      });
    }
  },
  gotoExchange(){
    if (this.data.mallModel.length==0){
      wx.showToast({
        title: '请选择兑换券',
        icon: 'none',
        duration: 1000
      });
      return;
    }
    let goods_id = [];
    this.data.mallModel.forEach(item => {
      goods_id.push(item.goods_id);
    });
    goods_id = goods_id.join(",");
    let exchangeData = JSON.stringify(this.data.mallModel);
    console.log(this.data.mallModel, goods_id);
    // exchangeData要这个格式[{"coupon_id":73,"goods_id":768,"total":1}]
    wx.navigateTo({
      url: '/packageD/buy/myOrder_v2/myOrder_v2?tag=fromExchange' + '&goodsId=' + goods_id +
        '&exchangeData=' + exchangeData
    });
  }
});
