// packageE/ranking_2/my_reward/my_reward.js
import DPagination from "../DPagination";
const App = getApp();
const recordPagination = new DPagination("records");
let rewardRecordRequestUrl = App.getNetAddresss(
  "plugin.commission-ranking.api.get-commission-ranking.my-bonus-log"
);
Page({
  /**
   * 页面的初始数据
   */
  data: {
    records: {},
    income: 0,
    userInfo: null,
    recordTotal: 0,
  },

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  onShareTimeline() {},
  loadRewardRecords() {
    if (recordPagination.loading || recordPagination.finished) {
      return;
    }
    wx.showLoading({
      title: "加载中",
      mask: true,
    });
    recordPagination.loading = true;
    App._getNetWork({
      url: rewardRecordRequestUrl,
      data: {
        page: recordPagination.loadPage,
      },
      success: ({ data: { data: response, result, msg } }) => {
        recordPagination.loading = false;
        wx.stopPullDownRefresh();
        wx.hideLoading();
        if (result == 0) {
          wx.showToast({
            title: msg,
            icon: "none",
          });
          return;
        }
        const setData = {
          recordTotal: 0,
        };
        if (
          response.data.data.length < response.per_page ||
          response.current_page == response.last_page
        ) {
          recordPagination.finished = true;
        }
        if (response.total !== this.data.income) {
          setData["income"] = response.total;
        }
        if (recordPagination.limit != response.per_page) {
          recordPagination.limit = response.per_page;
        }
        if (response.data.data.length > 0) {
          recordPagination.push(response.data.data);
        }
        setData["recordTotal"] = response.total;
        if(this.data.userInfo==null){
          setData['userInfo']=response.member;
        }
        this.setData(setData);
      },
    });
  },
});
