// packageH/credit_value/activate_history/activate_history.js
import DPagination from "../DPagination";
import DHttp from "../DHttp";
const pagination = new DPagination("historyList");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    historyList: {},
    historyDataCount: 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;
    }
    pagination.loading = true;
    wx.showLoading({
      title: "加载中",
      mask: true,
    });
    DHttp.request(
      "plugin.credit-inventory.frontend.controllers.activate.index",
      {
        page: pagination.loadPage,
      }
    )
      .then(({ per_page, data, last_page, current_page }) => {
        pagination.loading = false;
        wx.hideLoading();
        if (data.length < per_page || last_page == current_page) {
          pagination.finished = true;
        }
        pagination.push(data);
        this.setData({
          historyDataCount: data.length + this.data.historyDataCount,
        });
      })
      .catch(({ msg }) => {
        pagination.loading = false;
        wx.hideLoading();
        wx.showToast({
          title: msg,
          icon: "none",
        });
      });
  },
});
