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

  /**
	 * 页面的初始数据
	 */
  data: {
    recordsList: [],
    activeName: 'team',
    api: 'plugin.team-dividend.api.results.getList',
    is_show: false,

    //more
    isLoadMore: true,
    page: 1,
    total_page: 0,
  },

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

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

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

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

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

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

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

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

  handleChange(e) {
    this.data.activeName = e.detail.name;
    if(this.data.activeName === 'people') {
      this.data.api = 'plugin.team-dividend.api.results.getMyOrder';
    }else {
      this.data.api = 'plugin.team-dividend.api.results.getList';
    }
    this.initData();
    this.getData();
  },
  initData() {
    this.setData({
      recordsList: [],
      page: 1,
      total_page: 0,
      isLoadMore: true
    });
  },
  getData() {
    let urlStr = app.getNetAddresss(this.data.api);
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        let resData = res.data;
        if (resData.result === 1) {
          this.setData({
            isLoadMore: true,
          });
          if(this.data.activeName === 'people') {
            this.setData({
              recordsList: resData.data.data,
              total_page: resData.data.last_page
            });
          }else {
            this.setData({
              is_show: resData.data.is_show,
              recordsList: resData.data.data.data,
              total_page: resData.data.data.last_page
            });
          }
        } else {
          wx.showToast({
            title: resData.msg,
            icon: 'none'
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  getMoreData() {
    let urlStr = app.getNetAddresss(this.data.api);
    if (this.data.page >= this.data.total_page) {
      this.setData({
        isLoadMore: false
      });
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      });
      urlStr += '&page=' + this.data.page;
      app._getNetWork({
        url: urlStr,
        success: (res) => {
          if (res.data.result == 1) {
            let _data = res.data.data.data;
            if(this.data.activeName === 'people') {
              this.setData({
                isLoadMore: true,
                recordsList: this.data.recordsList.concat(_data)
              });
            }else {
              this.setData({
                isLoadMore: true,
                recordsList: this.data.recordsList.concat(_data.data)
              });
            }
          } else {
            this.setData({
              page: this.data.page - 1,
              isLoadMore: false
            });
            return;
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
});
