// packageH/starMusic/starMusicRewardsRecord/starMusicRewardsRecord.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    screenShow: false,

    labelId: 0,
    labels: [
      { id: 1, name: "星主奖励" },
      { id: 2, name: "V星主奖励" },
      { id: 3, name: "星拼补贴" },
      { id: 4, name: "V星拼补贴" },
      { id: 5, name: "新星奖励" },
      { id: 6, name: "乐拼奖励" },
    ],
    listData: [],
    page: 1, //分页数，当前页数
    isLoadMore: true, //判断是否要加载更多的标志
    total_page: 0, //总页数
  },

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

  setScreenShow() {
    this.setData({
      screenShow: !this.data.screenShow,
    });
  },

  setLabelId(evt) {
    let id = evt.currentTarget.dataset.id;
    if (this.data.labelId == id) return;
    this.setData({
      labelId: id,
      screenShow: false,
    });
    this.getData();
  },

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

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

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

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

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this._getMoreData();
    } else {
      console.log("没有更多数据");
    }
  },

  getData() {
    this.data.page = 1;
    this.data.isLoadMore = true;
    let reward_type_id = this.data.labelId;
    let json = {};
    if (reward_type_id != 0) json.reward_type_id = reward_type_id;
    let urlStr = app.getNetAddresss(
      "plugin.star-spell.frontend.index.rewardRecord"
    );
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        this.data.total_page = res.data.last_page;
        if (!this.data.total_page) {
          this.data.total_page = 0;
        }
        this.setData({
          listData: res.data.data,
        });
      },
    });
  },

  //加载更多数据
  _getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      // that.loading = true;
      return;
    } else {
      this.data.page += 1;
      let reward_type_id = this.data.labelId;
      let json = { page: this.data.page };
      if (reward_type_id != 0) json.reward_type_id = reward_type_id;
      let urlStr = app.getNetAddresss(
        "plugin.star-spell.frontend.index.rewardRecord"
      );
      app._postNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          let res = resdata.data;
          this.data.isLoadMore = true;
          if (res.result === 1) {
            let _team = res.data.data;
            let listData = this.data.listData.concat(_team);
            this.setData({ listData });
          } else {
            this.data.page = this.data.page - 1;
            this.data.isLoadMore = false;
          }
        },
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
