const util = require('../../utils/util.js');
const app = getApp();
Page({
  data: {
    type: 0,
    roleType:'',
    pageIndex: 1,
    pageSize: 10,
    info:{},
    list:[],
    total: 0,
    loadMore: true
  },
  onLoad(options) {
    this.setData({type:0, roleType:options.roleType})
    this.getList(false);
    this.stat();
  },
  setTabbar(e){
    let index = e.currentTarget.dataset.index;
    this.setData({ type: index, pageIndex: 1, list: [] });
    this.getList(true);
  },
  stat() {
    let that = this;
    util.ajax({
      func: `v2/${that.data.roleType}/invitee/stat`,
      load: false
    }, res => {
      if (res.code == 0) {
        that.setData({ info: res.data });
      }
    });
  },
  getList(load){
    let that = this;
    util.ajax({
      func:`v2/${that.data.roleType}/invitee/list`,
      data:{"pageSize":that.data.pageSize,"pageIndex":that.data.pageIndex,"type": that.data.type},
      load: load
    },function(res){
      if (res.code == 0) {
        let loadMore = true, newData = res.data.list || [], oldData = !load ? newData : [...that.data.list, ...newData];
        if (newData.length < that.data.pageSize && oldData.length > that.data.pageSize) loadMore = false;
        that.setData({ list: oldData, loadMore, total: res.data.total });
      } else
        util.showTips(res.reason);
    });
  },
  binderror(e) {
    var list = this.data.list, index = e.currentTarget.dataset.index;
    list[index].avatar = '/images/default_logo.jpg'
    this.setData({ list });
  },
  onPullDownRefresh() {
    var self = this;
    wx.showNavigationBarLoading();
    setTimeout(function () {
      self.setData({ pageIndex: 1 });
      self.getList(false);
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    }, 1000);
  },
  onReachBottom() {
    var self = this;
    if (self.data.loadMore == false || self.data.total <= self.data.pageSize) return false;

    setTimeout(function () {
      self.setData({ pageIndex: self.data.pageIndex + 1 });
      self.getList(true);
    }, 1000);
  },
  navigatorURl(e){
    util.navigator(e.currentTarget.dataset.url);
    console.log(e.currentTarget.dataset.url)
  },
})