// packageH/free_of_charge/FreeRecord/FreeRecord.js
import DPagination from "../../../utils/DPagination.js";
const RecordPagination = new DPagination("records");
const App = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    records: {},
    recordCount: 0,
    orderIdStrings: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      orderIdStrings: options.order_ids === undefined ? "" : options.order_ids,
    });
    RecordPagination.bind(this);
    RecordPagination.clean();
    this.getRecords();
  },

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

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

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  getRecords() {
    if (RecordPagination.loading || RecordPagination.finished) {
      return;
    }
    RecordPagination.loading = true;
    let url = App.getNetAddresss("plugin.free-lottery.frontend.index.dividend");
    wx.showLoading();
    App._getNetWork({
      url,
      data: {
        order_ids: this.data.orderIdStrings,
        page: RecordPagination.loadPage,
      },
      success: ({ data: { data, result, msg } }) => {
        wx.hideLoading();
        if (result === 0) {
          wx.showToast({
            title: msg,
            icon: "none",
          });
          return;
        }
        RecordPagination.push(data.data);
        RecordPagination.loading = false;
        if (
          data.data.length < data.per_page ||
          data.last_page == data.current_page
        ) {
          RecordPagination.finished = true;
        }
        wx.stopPullDownRefresh();
        this.setData({
          recordCount: 1,
        });
      },
      fail(err) {
        wx.hideLoading();
        wx.showToast({
          title: err.msg || err.message,
          icon: "none",
        });
      },
    });
  },
  routerToDetails({ currentTarget: { dataset } }) {
    wx.navigateTo({
      url:
        "/packageH/free_of_charge/FreeDetails/FreeDetails?order_id=" +
        dataset.orderId,
      success(res) {
        res.eventChannel.emit("recordDetails", { data: dataset.order });
      },
    });
  },
});
