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

  /**
	 * 页面的初始数据
	 */
  data: {
    language: '',
    datas:[],
    // more
    page: 1, // 分页数，当前页数
    isLoadMore: true, // 判断是否要加载更多的标志
    total_page: 0, // 总页数
  },

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

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

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

  },

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

  },

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

  },

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

  },

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

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

  },
  //获取数据 未结算佣金
  _getData() {
    let that = this;
    let urlStr = app.getNetAddresss('plugin.commission.api.commission.get-commission-list');
    urlStr += '&commission_type=2';
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            datas: res.data.data,
            total_page: res.data.last_page,
            isLoadMore: true,
            page: res.data.current_page
          });
          if (!that.data.total_page) {
            that.setData({
              total_page: 0
            });
          }
        }
      },
      fail: function(res) {
        console.log(res.msg);
      }
    });
  },
  getMoreData() {
    let that = this;
    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
      });
      let urlStr = app.getNetAddresss('plugin.commission.api.commission.get-commission-list');
      urlStr += '&commission_type=2';
      app._getNetWork({
        url: urlStr,
        data: {
          page: that.data.page
        },
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            var nextPageData = res.data.data;
            let datas = [...that.data.datas, ...nextPageData];
            that.setData({
              datas,
              total_page: res.last_page
            });
          }
        },
        fail: function (res) {
          console.log(res.msg);
        }
      });
    }
  }
});
