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

  /**
	 * 页面的初始数据
	 */
  data: {
    dataList: [],
    total_page: 0,
    page: 1,
    isLoadMore: true,
    loading: false, //为true时不会触发加载更多，false可以触发
  },

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

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

  },

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

  },

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

  },

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

  },

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

  },

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

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

  },
  _getData() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.point-activity.Frontend.Modules.Activity.Controllers.receive-records.index");
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: {
        page_id: 1
      },
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          let activityDate = res.data;
          that.setData({
            dataList: activityDate.records.data,
            total_page: activityDate.records.last_page ? activityDate.records.last_page : 0
          });
          if (!that.data.total_page) {
            that.setData({
              total_page: 0
            });
          }
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }

      },
      fail: function(res) {}
    });
  },
  _getMoreData(page) {
    let that = this;
    if (that.data.page >= that.data.total_page) {
      return;
    } else {
      that.setData({
        page: that.data.page + 1
      });
      let urlStr = app.getNetAddresss(
        "plugin.point-activity.Frontend.Modules.Activity.Controllers.receive-records.index");
      app._postNetWork({
        url: urlStr,
        showToastIn: false,
        data: {
          page_id: that.data.page
        },
        success: function(resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            var myData = res.data.records.data;
            that.setData({
              loading: false,
              dataList: that.data.dataList.concat(myData)
            });
          } else {
            that.setData({
              page: that.data.page - 1,
              loading: true,
              isLoadMore: false
            });
            return;
          }

        },
        fail: function(res) {}
      });
    }
  },
});
