// pages/member/distributionOrder/distributionOrder.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    language: '',
    selected: 0,
    display: "-1",
    _status: '',
    // more
    page: 1, // 分页数，当前页数
    isLoadMore: true, // 判断是否要加载更多的标志
    total_page: 0, // 总页数
		
    statusData0: [],
    statusData1: [],
    statusData2: [],
    statusData3: [],
    
    open_order_buyer: "",
    open_order_detail: "",
    open_order_buyer_info: "",
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    this.setData({
      selected: options.selected || 0
    });
    this._swichTabData(); //获取数据
  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {
    let language = wx.getStorageSync('langIndex');
    this.setData({ 'language': language.en});
  },

  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function() {
    this.customizeIncome();
  },

  //自定义提现收入语言
  customizeIncome() {
    try {
      var value = wx.getStorageSync('mailLanguage');
      if (value) {
        let mailLanguage = JSON.parse(value);
        wx.setNavigationBarTitle({
          title: mailLanguage.commission.commission_order ? mailLanguage.commission.commission_order : '分销订单'
        });
      }
    } catch (e) {
      // Do something when catch error
    }
  },

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

  },

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

  },

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

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      wx.showToast({
        title: '没有更多数据',
        icon: 'none'
      });
    }
  },

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

  },
  swichTab(e) {
    let idx = e.detail.index;
    this.setData({
      selected: idx
    });
    this._swichTabData();
  },
  //切换响应获取数据
  _swichTabData() {
    //点击初始化
    this.setData({
      display: '-1'
    });
    if (this.data.selected == 0) {
      this.getData("");
    } else if (this.data.selected == 1) {
      this.getData(0);
    } else if (this.data.selected == 2) {
      this.getData(1);
    } else if (this.data.selected == 3) {
      this.getData(3);
    }
  },
  //获取数据
  getData(_status) {
    this.setData({
      _status: _status
    });
    let that = this;
    let urlStr = app.getNetAddresss("plugin.commission.api.commission.get-commission-orders");
    urlStr += '&status=' + _status;
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          for (let i = 0; i < res.data.orders.data.length; i++) {
            res.data.orders.data[i].display = false;
          }
          if (_status === '') {
            that.setData({
              statusData0: res.data.orders.data
            });
          } else if (_status === 0) {
            that.setData({
              statusData1: res.data.orders.data
            });
          } else if (_status === 1) {
            that.setData({
              statusData2:res.data.orders.data
            });
          } else if (_status === 3) {
            that.setData({
              statusData3:res.data.orders.data
            });
          }
          that.setData({
            total_page: res.data.orders.last_page,
            isLoadMore: true,
            page: res.data.orders.current_page,
            open_order_buyer: res.data.set.open_order_buyer,
            open_order_detail: res.data.set.open_order_detail,
            open_order_buyer_info: res.data.set.open_order_buyer_info
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  // 获取更多数据，加载更多
  getMoreData() {
    let that = this;
    let json = {};
    this.setData({
      isLoadMore: false
    });
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      let pages = this.data.page;
      pages += 1;
      this.setData({
        page: pages
      });
      json.page = this.data.page;
      json.status = this.data._status;
      let urlStr = app.getNetAddresss("plugin.commission.api.commission.get-commission-orders");
			
      app._getNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          that.setData({
            isLoadMore: true
          });
          var res = resdata.data;
          if (res.result == 1) {
            for (let i = 0; i < res.data.orders.data.length; i++) {
              res.data.orders.data[i].display = false;
            }
            let datas = [];
            if (that.data._status === '') {
              datas = [...that.data.statusData0, ...res.data.orders.data];
              that.setData({
                statusData0: datas
              });
            } else if (that.data._status === 0) {
              datas = [...that.data.statusData1, ...res.data.orders.data];
              that.setData({
                statusData1: datas
              });
            } else if (that.data._status === 1) {
              datas = [...that.data.statusData2, ...res.data.orders.data];
              that.setData({
                statusData2:datas
              });
            } else if (that.data._status === 3) {
              datas = [...that.data.statusData3, ...res.data.orders.data];
              that.setData({
                statusData3:datas
              });
            }
            
            that.setData({
              isLoadMore: true,
            });
						
          } else {
            let p = that.data.page;
            p -= 1;
            that.setData({
              page: p,
              isLoadMore: false
            });
          }
        }
      });
    }
  },
  toggle(e) {
    let idex = e.currentTarget.dataset.idx;
    if (this.data.selected == 0) {
      if (this.data.statusData0[idex].display) {
        this.setData({
          ['statusData0[' + idex + '].display']: false
        });
      } else {
        this.setData({
          ['statusData0[' + idex + '].display']: true
        });
      }
    } else if (this.data.selected == 1) {
      if (this.data.statusData1[idex].display) {
        this.setData({
          ['statusData1[' + idex + '].display']: false
        });
      } else {
        this.setData({
          ['statusData1[' + idex + '].display']: true
        });
      }
    } else if (this.data.selected == 2) {
      if (this.data.statusData2[idex].display) {
        this.setData({
          ['statusData2[' + idex + '].display']: false
        });
      } else {
        this.setData({
          ['statusData2[' + idex + '].display']: true
        });
      }
    } else if (this.data.selected == 3) {
      if (this.data.statusData3[idex].display) {
        this.setData({
          ['statusData3[' + idex + '].display']: false
        });
      } else {
        this.setData({
          ['statusData3[' + idex + '].display']: true
        });
      }
    }
  }
});
