// packageB/member/enterprise/consumerReturnPoint/consumerReturnPoint.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    language: '',
    // more
    isLoadMore: true,
    page: 1,
    total_page: 0,
    recordsList: [],
    moreDataType: 1,
    recordsTeamList: [],
    trans: ''
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    this.getRecordsListData();
  },
  getRecordsListData() {
    var that = this;
    let urlStr = app.getNetAddresss("plugin.interval-consumption.Frontend.controllers.records.index");
    that.data.isLoadMore = true;
    that.data.page = 1;
    that.data.total_page = 0;
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: {
        page: that.data.page
      },
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.data.isLoadMore = true;
          that.data.total_page = res.data.list.last_page;
          if (!that.data.total_page) {
            that.data.total_page = 0;
          }
          if (res.data.info && res.data.info.trans) {
            wx.setNavigationBarTitle({
              title: res.data.info.trans
            });
          }
          that.setData({
            recordsList: res.data.list.data,
            info: res.data.info
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {}
    });
  },
  tabHandel(e) {
    let index = e.detail.index;
    this.data.moreDataType = index + 1;
    if (this.data.moreDataType == 1) {
      this.getRecordsListData();
    } else if (this.data.moreDataType == 2) {
      this.getRecordsTeamList();
    }
  },
  getRecordsTeamList() {
    var that = this;
    let urlStr = app.getNetAddresss("plugin.interval-consumption.Frontend.controllers.records.team-records");
    that.data.isLoadMore = true;
    that.data.page = 1;
    that.data.total_page = 0;
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: {
        page: that.data.page
      },
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.data.isLoadMore = true;
          that.data.total_page = res.data.list.last_page;
          if (!that.data.total_page) {
            that.data.total_page = 0;
          }
          that.setData({
            recordsTeamList: res.data.list.data
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {}
    });
  },
  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {
    let language = wx.getStorageSync('langIndex');
    this.setData({ 'language': language.en});
  },

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

  },

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

  },

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

  },

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

  },
  getMoreData(){
    var that = this;
    let urlStr = '';
    if (this.data.moreDataType == 1) {
      urlStr = app.getNetAddresss("plugin.interval-consumption.Frontend.controllers.records.get-personal");
    } else if (this.data.moreDataType == 2) {
      urlStr = app.getNetAddresss("plugin.interval-consumption.Frontend.controllers.records.get-team");
    }
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      app._postNetWork({
        url: urlStr,
        showToastIn: false,
        data: {
          page: that.data.page
        },
        success: function(resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            myData = res.data.data;
            if (that.data.moreDataType == 1) {
              that.setData({
                recordsList: that.data.recordsList.concat(myData)
              });
            } else if (that.data.moreDataType == 2) {
              that.setData({
                recordsTeamList: that.data.recordsTeamList.concat(myData)
              });
            }
          } else {
            that.data.page = this.data.page - 1;
            that.data.isLoadMore = false;
          }
        },
        fail: function(res) {}
      });
    }
  },
  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多数据');
    }
  },
  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {

  }
});
