import Taro, { Component } from '@tarojs/taro';
import './index.scss'
import { getAgentList } from '@services/agent'

import AgentItem from './item'
import ListView from '@components/ListView'
let pageIndex = 1
export default class Index extends Component {
  config = {
    navigationBarTitleText: '我的邀请'
  }
  state = {
    list: [],
    hasMore: true,
    isEmpty: false,
    recordId:null,
    pageIndex:1
  }
  loadList(pageNumber) {
    // const {
    //   // id = '369b7d7e5bc6e6b91f2e8f5e775035e2',
    //   id,
    // } = this.$router.params
    const payload = { pageSize: 10, pageNumber }
    return getAgentList(payload) 
  }
  onPullDownRefresh = async (rest) => {
    const { records: list, total } = await this.loadList(1);
    this.setState({
      list,
      isEmpty: total == 0,
      hasMore: list.length >= total ? false : true,
      pageIndex:1
    })
    rest && rest()
  }

  onScrollToLower = async (fn) => {
    const { list } = this.state;
    pageIndex++
    console.log(pageIndex)
    const { records, total } = await this.loadList(this.state.pageIndex+1);
    const newList = list.concat(records)
    this.setState({
      list: newList,
      hasMore: newList.length >= total ? false : true,
      pageIndex:this.state.pageIndex+1
    });
    fn && fn();
  }

  render() {
    const { hasMore, isEmpty, list } = this.state
    return (
      <ListView
        needInit
        isEmpty={isEmpty}
        emptyText="暂无更多邀请数据哦~"
        hasMore={hasMore}
        style={{ height: '100vh' }}
        onPullDownRefresh={fn => this.onPullDownRefresh(fn)}
        onScrollToLower={fn => this.onScrollToLower(fn)}
      >
        {list.map(item => <AgentItem key={item.uvId} data={item} />)}
      </ListView>
    );
  }
}