// packageH/circleCommunity/circleExtend/circleExtend.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeName: '0',
    activeReward: '0',

    page: 1, //分页数，当前页数
    isLoadMore: true, //判断是否要加载更多的标志
    total_page: 0, //总页数
    listData: [],
    networkLoading: false,

    member: {}, //用户信息
    incomeCount: 0, //累计金额

    rewardCount: 0, //打赏金额
    myRewardCount: 0, //收到打赏金额		

    unsettlement: 0, //未结算佣金
    settlement: 0, //已结算佣金

  },

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

  bindActiveNameChange(evt) {
    console.log(evt.detail);
    this.setData({
      activeName: evt.detail.index,
      networkLoading: false
    });
    this.getListData();
  },

  bindActiveRewardChange(evt) {
    this.setData({
      activeReward: evt.detail.index,
      networkLoading: false
    });
    this.getListData();
  },

  getDetails() {
    let urlStr = app.getNetAddresss('plugin.circle.frontend.circle.get-circle');
    app._postNetWork({
      url: urlStr,
      data: {
        circle_id: this.data.circleId
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        this.setData({
          circleInfo: res.data
        });
        console.log(res);
      }
    });
  },

  initData() {
    this.setData({
      page: 1,
      total_page: 0,
      isLoadMore: true,
      networkLoading: false
    });
  },
  getListData() {
    this.initData();
    let urlStr = '';
    let {
      activeName,
      activeReward
    } = this.data;
    let json = {};
    if (activeName == 0) {
      urlStr = app.getNetAddresss('plugin.circle.frontend.circle-pay.circle-pay-log-list');
    } else if (activeName == 1) {
      urlStr = app.getNetAddresss('plugin.circle.frontend.circle-pay.reward-log');
      json.reward_type = activeReward;
      if (activeReward == 0) {
        json.reward_type = 2;
      }
    } else if (activeName == 2) {
      urlStr = app.getNetAddresss('plugin.circle.frontend.circle-pay.get-video-reward');
    }

    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.list.last_page;
        if (!this.data.total_page) {
          this.data.total_page = 0;
        }
        if (activeName == 0) {
          this.setData({
            member: res.data.member,
            incomeCount: res.data.incomeCount
          });
        }
        if (activeName == 1 && activeReward == 0) {
          this.setData({
            rewardCount: res.data.rewardCount,
            myRewardCount: res.data.myRewardCount
          });
        }
        if (activeName == 2) {
          this.setData({
            unsettlement: res.data.unsettlement,
            settlement: res.data.settlement
          });
        }
        this.setData({
          listData: res.data.list.data,
          networkLoading: true
        });
      }
    });
  },

  //加载更多数据
  _getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      // that.loading = true;
      return;
    } else {
      this.data.page += 1;
      let urlStr = app.getNetAddresss('plugin.circle.frontend.circle-member.get-circle-memberList');
      let json = {
        page: this.data.page
      };
      let {
        activeName,
        activeReward
      } = this.data;
      if (activeName == 0) {
        urlStr = app.getNetAddresss('plugin.circle.frontend.circle-pay.circle-pay-log-list');
      } else if (activeName == 1) {
        urlStr = app.getNetAddresss('plugin.circle.frontend.circle-pay.reward-log');
        json.reward_type = activeReward;
        if (activeReward == 0) {
          json.reward_type = 2;
        }
      } else if (activeName == 2) {
        urlStr = app.getNetAddresss('plugin.circle.frontend.circle-pay.get-video-reward');
      }
      app._postNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          let res = resdata.data;
          this.data.isLoadMore = true;
          if (res.result === 1) {
            let listData = this.data.listData.concat(res.data.list.data);
            this.setData({
              listData
            });
          } else {
            this.data.page = this.data.page - 1;
            this.data.isLoadMore = false;
          }
        }
      });
    }
  },

  //获取圈子设置
  getCircleSet() {
    let urlStr = app.getNetAddresss("plugin.circle.frontend.circle.get-set");
    app._getNetWork({
      url: urlStr,
      data: {},
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        wx.setNavigationBarTitle({
          title: res.data.plugin_name,
        });
      },
      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('没有更多数据');
    }
  },

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

  }
});