const util = require('../../utils/util.js');
const app = getApp();
Page({
  data: {
    type: 0,
    id: '',
    roleType:'',
    tabIndex:0,
    queryData: {
      pageIndex: 1,
      pageSize: 10,
    },
    info:{},
    list:[],
    total: 0,
    loadMore: true
  },
  onLoad(options) {
    this.setData({type: options.type, id: options.id, roleType: options.roleType, tabIndex:options.type == 1 ? 0 : 1})
    this.getList(false);
    this.info();
  },
  setTabbar(e){
    let index = e.currentTarget.dataset.index;
    this.data.queryData.pageIndex = 1;
    this.setData({ tabIndex: index, queryData: this.data.queryData, list: [] });
    this.getList(true);
  },
  info() {
    let that = this;
    util.ajax({
      func: `v2/${this.data.roleType}/invitee/info`,
      data: {id:that.data.id},
      load: false
    }, res => {
      if (res.code == 0) {
        that.setData({ info: res.data });
      }
    });
  },
  getList(load){
    let that = this, url = '';
    if(this.data.type == 1 && this.data.tabIndex == 0){
      url = `v2/${this.data.roleType}/indirectMember/list`;
    }else if(this.data.tabIndex == 1){
      url = 'v2/user/coin/list';
    }else if(this.data.tabIndex == 2){
      url = 'v2/user/coin/transition/list';
    }
    util.ajax({
      func:url,
      data:{pageSize:that.data.queryData.pageSize,pageIndex:that.data.queryData.pageIndex, id: that.data.id, level:(that.data.type*1+1),transitionType:0},
      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.queryData.pageSize && oldData.length > that.data.queryData.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.data.queryData.pageIndex = 1;
      self.setData({ queryData: self.data.queryData });
      self.getList(false);
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    }, 1000);
  },
  onReachBottom() {
    var self = this;
    if (self.data.loadMore == false || self.data.total <= self.data.queryData.pageSize) return false;

    setTimeout(function () {
      self.data.queryData.pageIndex = self.data.queryData.pageIndex + 1;
      self.setData({ queryData: self.data.queryData });
      self.getList(true);
    }, 1000);
  },
})