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

  /**
	 * 页面的初始数据
	 */
  data: {
    id: '',
    receive_point: "0.00",
    used_point: "0.00",
    validity_point: 0,
    total_page: 0,
    dataList: [],
    isLoadMore: true,
    activeName: 0,
    statuNum: 0
  },

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

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

  },

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

  },

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

  },

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

  },

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

  },

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

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

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

      },
      fail: function(res) {}
    });
  },
  _getMoreData() {
    let that = this;
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      });
      let urlStr = app.getNetAddresss(
        "plugin.point-activity.Frontend.Modules.Activity.Controllers.activity-receive-records.index");

      app._postNetWork({
        url: urlStr,
        showToastIn: false,
        data: {
          activity_id: that.data.id,
          page_id: that.data.page,
          status: that.data.statuNum
        },
        success: function(resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            var myData = res.data.records.data;
            that.setData({
              dataList: that.data.dataList.concat(myData)
            });
          } else {
            that.setData({
              page: that.data.page - 1,
              isLoadMore: false
            });
            return;
          }

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

  },
  //标签栏点击
  handleClick(e) {
    let idx = e.detail.index;
    this.setData({
      dataList: [],
      isLoadMore: true
    });
    if (Number(idx) === 0) {
      this._getData(null);
    } else {
      this._getData(Number(idx) - 1);
    }
  }
});
