// packageH/credit_value/give_history/give_history.js
import DPagination from "../DPagination";
import DHttp from "../DHttp";
const pagination = new DPagination("historyList");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    historyList: {},
    giveHistoryCount: 0,
  },

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

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

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

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

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

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getHistory();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  getHistory() {
    if (pagination.loading || pagination.finished) {
      return;
    }
    wx.showLoading({
      title: "加载中",
      mask: true,
    });
    pagination.loading = true;
    DHttp.request("plugin.credit-inventory.frontend.controllers.give.index")
      .then(({ data, per_page, current_page, last_page }) => {
        pagination.loading = false;
        wx.hideLoading();
        if (last_page == current_page || data.length < per_page) {
          pagination.finished = true;
        }
        pagination.push(data);
        this.setData({
          giveHistoryCount: this.data.giveHistoryCount + data.length,
        });
      })
      .catch(({ msg }) => {
        pagination.loading = false;
        wx.hideLoading();
        wx.showToast({
          title: msg,
          icon: "none",
        });
      });
  },
});
