// packageH/member/upgrade_code/used_log/used_log.js
const App = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    codeId: null,
    usedLog: [],
    usedLogPage: 1, //* 使用记录 页数
    usedLogLoading: false, //* 使用记录是否加载中
    usedLogLoadFinished: false, //* 使用记录是否加载完成
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (!options.code_id) {
      wx.showToast({
        title: "升级码不存在",
        icon: "none",
        duration: 1500,
        success: (result) => {
          wx.redirectTo({
            url: "/packageH/member/upgrade_code/index/index",
          });
        },
      });
    }
    wx.setNavigationBarTitle({
      title: '升级码使用记录',
    });
    this.setData(
      {
        codeId: options.code_id,
      },
      () => {
        this.getUsedLog();
      }
    );
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getUsedLog();
  },
  getUsedLog() {
    if (
      this.data.usedLogLoading === true ||
      this.data.usedLogLoadFinished === true
    ) {
      return;
    }
    this.setData({
      usedLogLoading: true,
    });
    let url = App.getNetAddresss(
      "plugin.upgrade-code.Frontend.controllers.code.logs"
    );
    App._getNetWork({
      url,
      data: {
        id: this.data.codeId,
        page: this.data.usedLogPage,
      },
      success: ({ data: { data: response, result, msg } }) => {
        wx.stopPullDownRefresh();
        if (result === 0) {
          wx.showToast({
            title: msg,
            duration: 1500,
            mask: false,
            success: (result) => {
              wx.navigateBack({
                delta: 1,
              });
            },
          });
          return;
        }
        const setData = {
          usedLogLoading: false,
          usedLogPage: this.data.usedLogPage + 1,
        };
        let usedLog = this.data.usedLog;
        if (
          response.last_page == response.current_page ||
          response.data.length < response.per_page
        ) {
          setData["usedLogLoadFinished"] = true;
        }
        usedLog = usedLog.concat(response.data);
        setData["usedLog"] = usedLog;
        this.setData(setData);
      },
    });
  },
});
