// packageH/stock/promotion_center/promotion_center.js
const App = getApp();

function getAwardStatusText(status) {
  switch (status) {
  case 1:
    return "未结算";
  case 2:
    return "已结算";
  case 3:
    return "失效";
  }
}

Page({
  /**
   * 页面的初始数据
   */
  data: {
    currentShowType: 1,
    awardData: [],
    loading: false,
    isMoreData: true,
    page: 1,
    agent: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    this.getAgent();
    wx.setNavigationBarTitle({
      title: "推广中心",
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.loading === false || this.data.isMoreData === true) {
      this.getAwardData();
    }
  },
  onShareAppMessage() {},
  onShareTimeline() {},
  getAgent() {
    let urlStr = App.getNetAddresss("plugin.agency.api.agency.index");
    App._postNetWork({
      url: urlStr,
      success: ({ data: { data, result, msg } }) => {
        if (result === 0) {
          wx.hideLoading();
          wx.showToast({
            title: msg,
            success: (result) => {
              wx.navigateBack({
                delta: 1,
              });
            },
          });
          return;
        }
        this.setData({
          agent: data,
        });
        wx.hideLoading();
        this.getAwardData();
      },
    });
  },

  switchTabs(event) {
    let type = parseInt(event.detail.name);
    let hiddenNormalList = true;
    if ([1, 3, 4].includes(type)) {
      hiddenNormalList = false;
    }
    this.setData(
      {
        awardData: [],
        loading: false,
        isMoreData: true,
        page: 1,
        currentShowType: type,
        hiddenNormalList,
      },
      () => {
        this.getAwardData();
      }
    );
  },
  getAwardData() {
    if (this.data.loading === true || this.data.isMoreData === false) {
      return;
    }
    this.setData({
      loading: true,
    });
    wx.showLoading({
      title: "加载中",
      mask: true,
    });
    let urlStr = App.getNetAddresss("plugin.agency.api.award.get-list");
    urlStr +=
      "&agency_type=" + this.data.currentShowType + "&page=" + this.data.page;
    App._postNetWork({
      url: urlStr,
      success: ({ data: { data, result, msg } }) => {
        if (result === 0) {
          wx.hideLoading();
          wx.showToast({
            title: msg,
            success: () => {
              wx.navigateBack({
                delta: 1,
              });
            },
          });
          return;
        }
        const setData = {};
        if (
          data.current_page == data.last_page ||
          data.data.length === 0 ||
          data.data.length < per_page
        ) {
          setData["isMoreData"] = false;
        }
        let awardData = this.data.awardData;
        let awards = data.data;
        for (const item of awards) {
          item["award_status_text"] = getAwardStatusText(item["award_status"]);
        }
        awardData.push(...awards);
        setData["awardData"] = awardData;
        setData["loading"] = false;
        awardData["page"] = this.data.page + 1;
        this.setData(setData);

        wx.hideLoading();
      },
    });
  },
});
