import React, { Component } from 'react';
import Taro from '@tarojs/taro';
import { View } from '@tarojs/components'
import { getPublicReleaseList } from '../../utils/servers/servers';
import { AtTabs, AtTabsPane } from 'taro-ui';

import './index.less';

import 'taro-ui/dist/style/components/tabs.scss';

import MyList from './myList';
import MessageNoticebar from "../../components/common/messageNoticebar";

class Examine extends Component {

  constructor(props) {
    super(props);
    this.state={
      current:0,

      list: [],
      pageNo: 1,
      listState: 'LOADING',

      examinelist: [],
      examinePageNo: 1,
      examineListState: 'LOADING',
    }
    this.getPublicReleaseList= this.getPublicReleaseList.bind(this);
    this.getMyList= this.getMyList.bind(this);
    this.getExamineList= this.getExamineList.bind(this);
  }

  componentDidShow(){

  }

  async componentDidMount() {
    await this.getPublicReleaseList();
  }

  onPullDownRefresh(){
    this.getPublicReleaseList();
  }

  onReachBottom(){
    this.getPublicReleaseList(true);
  }

  async getPublicReleaseList (lv){
    if(this.state.current === 0){
      await this.getMyList(lv ? this.state.pageNo + 1 : 1)
    } else if(this.state.current === 1){
      await this.getExamineList(lv ? this.state.examinePageNo + 1 : 1)
    }
  }

  async getMyList(pageNo){
    this.setState({
      listState: 'LOADING'
    })
    let msg = await getPublicReleaseList({
      pageNo: pageNo,
      pageSize: 10,
      type:0,
    });
    if(msg.error.length === 0){
      if(msg.data.totalCount === 0){
        this.setState({
          listState: 'NO_DATA'
        })
      }else if(msg.data.totalCount === this.state.list.length && pageNo !== 1){
        Taro.showToast({title:'没有更多数据了',icon:'none'});
        this.setState({
          listState: 'NO_MORE_DATA'
        })
      }else{
        this.setState({
          list:pageNo === 1 ? msg.data.list : this.state.list.concat(msg.data.list),
          pageNo: msg.data.pageNo
        },()=>{
          if(msg.data.totalCount === this.state.list.length){
            this.setState({
              listState: 'NO_MORE_DATA'
            })
          }
        })
      }
    }else{
      Taro.showToast({title:msg.error[0].message,icon:'none'});
      this.setState({
        listState: msg.error[0].field === '403' ? 'NO_DATA' : 'RELOAD'
      })
    }
    Taro.stopPullDownRefresh();
  }

  async getExamineList(pageNo){
    this.setState({
      examineListState: 'LOADING'
    })
    let msg = await getPublicReleaseList({
      pageNo: pageNo,
      pageSize: 10,
      type:1,
    });
    if(msg.error.length === 0){
      if(msg.data.totalCount === 0){
        this.setState({
          examineListState: 'NO_DATA'
        })
      }else if(msg.data.totalCount === this.state.examinelist.length && pageNo !== 1){
        Taro.showToast({title:'没有更多数据了',icon:'none'});
        this.setState({
          examineListState: 'NO_MORE_DATA'
        })
      }else{
        this.setState({
          examinelist:pageNo === 1 ? msg.data.list : this.state.examinelist.concat(msg.data.list),
          examinePageNo: msg.data.pageNo
        },()=>{
          if(msg.data.totalCount === this.state.examinelist.length){
            this.setState({
              examineListState: 'NO_MORE_DATA'
            })
          }
        })
      }
    }else{
      Taro.showToast({title:msg.error[0].message,icon:'none'});
      this.setState({
        examineListState: msg.error[0].field === '403' ? 'NO_DATA' : 'RELOAD'
      })
    }
    Taro.stopPullDownRefresh();
  }

  render () {
    return (
      <View className='indexPage'>
        <MessageNoticebar/>
        <AtTabs current={this.state.current || 0} tabList={[{ title: '我的' }, { title: '审核' }]} onClick={(current)=>{
          this.setState({
            current
          },()=>{
            if((current === 0 && this.state.pageNo === 1) || (current === 1 && this.state.examinePageNo === 1)){
              this.getPublicReleaseList();
            }

          })
        }}>
          <AtTabsPane current={this.state.current} index={0} >
            <MyList
              type={0}
              list={this.state.list}
              listState={this.state.listState}
              onRefresh={()=>{
                this.getPublicReleaseList(true);
              }}/>
          </AtTabsPane>
          <AtTabsPane current={this.state.current} index={1}>
            <MyList
              type={1}
              list={this.state.examinelist}
              listState={this.state.examineListState}
              onRefresh={()=>{
                this.getPublicReleaseList(true);
              }}/>
          </AtTabsPane>
        </AtTabs>
      </View>
    )
  }
}

export default Examine
