// packageI/teamOddDividend/teamOddDividend.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: {
      rewards_each: "---",
      team_nums: "---",
      is_settle: "---",
      no_settle: "---"
    },
    activeName: 0,
    dataList: [],
    titleArr: [],
    isLoadMore: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.activeName = 0;
    this.data.titleArr = ["全部", "未结算", "已结算", "已失效"];
    this.setData({
      titleArr: this.data.titleArr
    });
    this.setNewArr();
  },

  setNewArr() {
    this.data.dataList = [];
    this.data.titleArr.forEach((val, index) => {
      this.data.dataList.push({
        isLoadMore: true,
        page: 1,
        total_page: 0,
        list: []
      });
    });
    console.log("dataList:::", this.data.dataList);
    this.handleClick();
  },
  handleClick(event) {
    if (event) {
      this.data.activeName = event.detail.name;
    }
    let that = this;
    if (this.data.dataList[that.data.activeName].list.length > 0) {
      this.setData({
        isLoadMore: this.data.dataList[that.data.activeName].isLoadMore
      });
      return;
    }
    let index = this.data.activeName;
    let record_type = ["", "no_settle", "is_settle", "invalid"];

    let urlStr = app.getNetAddresss('plugin.order-quantity-bonus.frontend.controllers.center.index');
    app._postNetWork({
      url: urlStr,
      data: {
        record_type: record_type[index]
      },
      success: (resdata) => {
        wx.hideLoading();
        let res = resdata.data;
        if (res.result == 1) {
          wx.setNavigationBarTitle({
            title: res.data.info.name
          });
          that.setData({
            info: res.data.info,
            ['dataList[' + index + '].isLoadMore']: true,
            ['dataList[' + index + '].total_page']: res.data.list.last_page,
            ['dataList[' + index + '].list']: res.data.list.data,
          });
        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: 'none'
          });
        }
      },
      fail: function (res) {
        wx.hideLoading();
        console.log(res.msg);
      }
    });
  },
  _getMoreData() {
    let that = this;
    let index = this.data.activeName;
    let record_type = ["", "no_settle", "is_settle", "invalid"];
    this.setData({
      ['dataList[' + index + '].isLoadMore']: false
    });
    if (this.data.dataList[index].page >= this.data.dataList[index].total_page) {
      return;
    } else {
      this.data.dataList[index].page = this.data.dataList[index].page + 1;
      let urlStr = app.getNetAddresss('plugin.order-quantity-bonus.frontend.controllers.center.index');
      urlStr += "&page=" + this.data.dataList[index].page;

      app._getNetWork({
        url: urlStr,
        data: {
          record_type: record_type[index]
        },
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            let myData = res.data;
            that.setData({
              ['dataList[' + index + '].isLoadMore']: true,
              ['dataList[' + index + '].list']: that.data.dataList[index].list.concat(myData.data)
            });
          } else {
            that.setData({
              ['dataList[' + index + '].isLoadMore']: false,
              ['dataList[' + index + '].page']: that.data.dataList[index].page - 1
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

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

  }
});