// packageE/ranking_2/reward_log/reward_log.js
import DPagination from "../DPagination";
const App = getApp();
const RewardPagination = new DPagination("rewardRecords");
let rankingRecordRequestUrl = App.getNetAddresss(
  "plugin.commission-ranking.api.get-commission-ranking.bonus-log"
);
let requestRewardRecordTime = "";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    rewardRecords: [],
    customSettings: {
      expectedEarningText: "本月预计收益",
    },
    calendarMonths: {
      1: "01",
      2: "02",
      3: "03",
      4: "04",
      5: "05",
      6: "06",
      7: "07",
      8: "08",
      9: "09",
      10: "10",
      11: "11",
      12: "12",
    },
    showCalendar: false,
    currentSelectedYear: 2000,
    currentSelectedMonth: 1,
    currentYear: 1,
    currentMonth: 1,
    isLastMonth: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "历史排行榜记录",
    });
    this.setNowDate();
    RewardPagination.bind(this);
    this.dateChanged();
  },

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  onShareTimeline() {},
  loadRewardRecord() {
    if (RewardPagination.loading || RewardPagination.finished) {
      return;
    }
    wx.showLoading({
      title: "加载中",
      mask: true,
    });
    RewardPagination.loading = true;

    App._getNetWork({
      url: rankingRecordRequestUrl,
      data: {
        page: RewardPagination.loadPage,
        time: requestRewardRecordTime,
      },
      success: ({ data: { data: response, result, msg } }) => {
        if (result == 0) {
          wx.showToast({
            title: msg,
            icon: "none",
          });
          return;
        }
        if (response.data.length < RewardPagination.limit) {
          RewardPagination.finished = true;
        }
        if (RewardPagination.limit != response.per_page) {
          RewardPagination.limit = response.per_page;
        }
        RewardPagination.push(response.data);
        RewardPagination.loading = false;
        wx.hideLoading();
        wx.stopPullDownRefresh();
      },
    });
  },
  setNowDate() {
    let date = new Date();
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    this.setData({
      currentSelectedYear: year,
      currentSelectedMonth: month,
      currentYear: year,
      currentMonth: month,
    });
  },
  selectMonth({ target: { dataset } }) {
    if (!dataset.month) {
      return;
    }
    let month = Number(dataset.month);
    if (
      this.data.currentSelectedYear == this.data.currentYear &&
      month > this.data.currentMonth
    ) {
      return;
    }

    this.setData({
      currentSelectedMonth: Number(dataset.month),
      isLastMonth: month == this.data.currentMonth,
    });
    this.dateChanged();
  },
  displayCalendar() {
    this.setData({
      showCalendar: !this.data.showCalendar,
    });
  },
  switchToPreviousYear() {
    let currentYear = this.data.currentSelectedYear;
    const setData = {};
    if (currentYear - 1 == 0) {
      return;
    }

    setData["currentSelectedYear"] = currentYear - 1;
    this.setData(setData);
    this.dateChanged();
  },
  switchToNextYear() {
    let currentSelectedYear = Number(this.data.currentSelectedYear);
    let currentYear = Number(this.data.currentYear);

    const setData = {};
    if (
      currentYear == currentSelectedYear ||
      currentSelectedYear + 1 > currentYear
    ) {
      return;
    }
    setData["currentSelectedYear"] = currentSelectedYear + 1;
    if(currentSelectedYear+1==currentYear){
      let selectedMonth=this.data.currentSelectedMonth;
      let currentMonth=Number(this.data.currentMonth);
      if(selectedMonth>currentMonth){
        setData['currentSelectedMonth']=currentMonth;
      }
    }

    this.setData(setData);
    this.dateChanged();
  },
  dateChanged() {
    RewardPagination.clean();
    let month = this.data.currentSelectedMonth;
    if (String(month).length == 1) {
      month = `0${month}`;
    }
    if (this.data.currentYear == this.data.currentSelectedYear) {
      this.setData({
        isLastMonth: true,
      });
    } else {
      if (this.data.isLastMonth) {
        this.setData({
          isLastMonth: false,
        });
      }
    }
    requestRewardRecordTime = `${this.data.currentSelectedYear}-${month}`;
    this.loadRewardRecord();
  },
});
