// packageI/personRed/personRedRecord/personRedRecord.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    rid: '',
    redpack_info: {},
    receive_amount: '',
    receive_records: [],
    page: 1,
    finished: false,
  },

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

  onShow: function () {
    this.getData();
  },

  getData() {
    let urlStr = app.getNetAddresss("plugin.redpack-user.frontend.receiveLogDetail.index");
    app._getNetWork({
      url: urlStr,
      data: {rid: this.data.rid},
      success: (res) => {
        if (res.data.result !== 1) {
          wx.showToast({
            title: res.data.msg,
          });
          return;
        }
        let data = res.data.data;
        let finished = false;
        let receive_logs = data.receive_logs;
        if (this.data.page >= receive_logs.last_page || receive_logs.data.length < receive_logs.per_page) {
          finished = true;
        }
        this.setData({
          finished,
          page: (++this.data.page),
          receive_amount: data.receive_amount,
          redpack_info: data.redpack_info,
          receive_records: receive_logs.data
        });
      }
    });
  },

  getMoreData() {
    if (this.data.finished) return;
    let urlStr = app.getNetAddresss("plugin.redpack-user.frontend.receiveLogDetail.index");
    app._getNetWork({
      url: urlStr,
      data: {rid: this.data.rid,page: this.data.page},
      success: (res) => {
        if (res.data.result !== 1) {
          wx.showToast({
            title: res.data.msg,
          });
          return;
        }
        let data = res.data.data;
        let finished = false;
        let receive_logs = data.receive_logs;
        let receive_records = this.data.receive_records.concat(receive_logs.data);
        if (this.data.page >= receive_logs.last_page || receive_logs.data.length < receive_logs.per_page) {
          finished = true;
        }
        this.setData({
          finished,
          page: (++this.data.page),
          receive_records: receive_records
        });
      }
    });
  },

  // 触底加载更多
  onReachBottom: function () {
    this.getMoreData();
  },

});