// pages/member/rankingListSecond/rankingListSecond.js
var app = getApp();
const URL_MEMBER = 'plugin.mryt.api.home.get-member-list';
const vip_MEMBER = 'plugin.mryt.api.home.get-sales-member-list';
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    title: '',
    url: '',
    member_arr: [],
    total_num: 0,
    loading: false,
    allLoaded: false,
    goload: true,
    isLoadMore: true,
    page: 1,
    total_page: 0
  },

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

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

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

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

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

  /**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      wx.showToast({
        title: '没有更多了',
        icon: 'none'
      });
    }

  },

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

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {},
  getData() {
    let urlStr ;
    if(this.data.is_type == 'vip'){
      urlStr = app.getNetAddresss(vip_MEMBER);
      urlStr += '&page=' + this.data.page ;
    }else{
      urlStr = app.getNetAddresss(URL_MEMBER);
      urlStr += '&page=' + this.data.page + '&member_type=' + this.options.type + '&level=' + this.options.level;
    }
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        let resdata = res.data;
        if (resdata.result === 1) {
          this.setData({
            member_arr: resdata.data.member.data,
            total_page: resdata.data.member.last_page,
            title: resdata.data.level_name,
            total_num: resdata.data.member.total ? resdata.data.member.total : 0
          });
        } else {
          wx.showToast({
            title: resdata.msg,
            icon: 'none'
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },

  getMoreData() {
    if (this.data.page == this.data.total_page) {
      return;
    }
    if (this.data.page >= this.data.total_page) {
      this.setData({
        loading: true,
        allLoaded: true
      });
      return;
    } else {
      let pages = this.data.page + 1;
      this.setData({
        page: pages
      });
      let urlStr = app.getNetAddresss(URL_MEMBER);
      urlStr += '&page=' + this.data.page + '&type=' + this.options.type + '&level=' + this.options.level;
      app._getNetWork({
        url: urlStr,
        success: (res) => {
          let resdata = res.data;
          if (resdata.result === 1) {
            this.setData({
              loading: false,
              allLoaded: false
            });
            let listAat = this.data.member_arr.concat(resdata.data.member.data);
            this.setData({
              member_arr: listAat
            });
          } else {
            let pegeOne = this.data.page - 1;
            this.setData({
              page: pegeOne,
              loading: true,
              allLoaded: true,
              isLoadMore: false
            });
          }
        },
        fail: function(res) {
          console.log(res);
        }
      });
    }
  },
  goBack() {
    wx.navigateBack({
      delta: 1
    });

  }

});
