// packageI/commissionExtraIndex/commissionExtraIndex.js
// packageH/recommend_reward/recommend_reward.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 2, // 绑定当前选中标签的标识符
    info: {}, //用户信息
    listData: [], //列表数据
    // more
    isLoadMore: true, // 判断是否要加载更多的标识
    page: 1, //当前的页数
    total_page: 0 // 总共有多少页
  },

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

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

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.page >= this.data.total_page) {
      this.setData({
        isLoadMore: false
      });
      return;
    } else if (this.data.isLoadMore) {
      let pages = this.data.page + 1;
      this.setData({
        page: pages,
      });
      this.getMoreData();
    }
  },

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

  },

  change(e) {
    this.setData({
      active: e.detail.name,
      page: 1,
      total_page: 0,
      listData: [],
      isLoadMore: true,
    });
    this.getdata(false);
  },

  // 获取列表
  getdata(_first) {
    wx.showLoading({
      title: '加载中',
    });
    let _json = {};
    if (this.data.active != 2) {
      _json.status = this.data.active;
    }
    let urlStr = app.getNetAddresss('plugin.commission-extra.frontend.index');
    app._postNetWork({
      url: urlStr,
      data: _json,
      success: (resdata) => {
        let res = resdata.data;
        wx.hideLoading();
        if (res.result == 1) {
          if (_first) {
            wx.setNavigationBarTitle({
              title: res.data.plugin_name
            });
            let info = {};
            info["plugin_name"] = res.data.plugin_name;
            info["nickname"] = res.data.nickname;
            info["avatar"] = res.data.avatar;
            info["total_unsettle"] = res.data.total_unsettle;
            info["total_settle"] = res.data.total_settle;
            this.setData({
              info: info
            });
          }

          this.setData({
            listData: res.data.data, //数据列表
            total_page: res.data.last_page,
          });
        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: 'none'
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },

  // 获取更多数据
  getMoreData() {
    wx.showLoading({
      title: '加载中',
    });
    let json = {
      page: this.data.page,
      status: this.data.active == 2 ? null : this.data.active,
    };
    this.data.isLoadMore = false;
    let urlStr = app.getNetAddresss('plugin.commission-extra.frontend.index');
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        let res = resdata.data;
        wx.hideLoading();
        if (res.result == 1) {
          let nextarr = res.data.data;
          this.setData({
            listData: this.data.listData.concat(nextarr),
            isLoadMore: true,
          });
        } else {
          let pages = this.data.page - 1;
          this.setData({
            page: pages,
            isLoadMore: true,
          });
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: 'none'
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  }
});