// packageF/bonusPoolsIndex/bonusPoolsIndex/bonusPoolsIndex.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tapSelect: 0,
    active: '0',
    page: 1,
    current_page: 1,
    last_page: 1,

    isLoadMore: true, //判断是否要加载更多的标志
  },
  init() {
    this.setData({
      page: 1,
      current_page: 1,
      last_page: 1,
      isLoadMore: true
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '奖金池奖励',
    })
    let language = wx.getStorageSync("langIndex");
    let yz_basic_info = wx.getStorageSync('yz_basic_info');
    this.setData({
      language: language.en,
      love:yz_basic_info.love
    });
    this.init();
    this.getData();
  },
  toTeam() {
    wx.navigateTo({
      url: '/packageF/bonusPoolsIndex/bonusPoolsTeam/bonusPoolsTeam',
    })
  },
  bindChange(e) {
    let index = e.detail.index;
    this.setData({
      tapSelect: 0,
      active: index,
    })
    this.init();
    this.getData();
  },
  tapSelectInd(e) {
    let ind = e.currentTarget.dataset.ind;
    this.setData({
      tapSelect: ind
    })
    this.init();
    this.getData();
  },
  getData() {
    let json = {
      page: this.data.page,
      award_type: this.data.active,
      award_status: this.data.tapSelect == 2 ? '-1' : (this.data.tapSelect == 0 ? '0' : '1'),
    }
    let urlStr = app.getNetAddresss("plugin.xzhh-bonus-pool.frontend.controllers.center.index");
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          this.setData({
            info: res.data.info,
            list: res.data.list.data,
            current_page: res.data.list.current_page,
            last_page: res.data.list.last_page
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getMore() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.current_page >= this.data.last_page) {
      return;
    } else {
      let json = {
        award_type: this.data.active,
        award_status: this.data.tapSelect == 2 ? '-1' : (this.data.tapSelect == 0 ? '0' : '1'),
        page: this.data.page + 1
      }
      let urlStr = app.getNetAddresss("plugin.xzhh-bonus-pool.frontend.controllers.center.index");
      app._getNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          this.data.isLoadMore = true;
          let res = resdata.data
          if (res.result == 1) {
            this.setData({
              page: this.data.page + 1,
              list: this.data.list.concat(res.data.list.data),
              current_page: res.data.list.current_page,
              last_page: res.data.list.last_page
            })
          } else {
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          this.data.isLoadMore = true;
          console.log(res);
        }
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

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

  }
})