import React, { Component } from 'react';
import Taro from '@tarojs/taro';
import { View, Picker, ScrollView, Button } from '@tarojs/components'
import { getProjectList, techReject } from '../../utils/servers/servers';
import dayjs from 'dayjs';
import {
  AtSearchBar,
  AtTabs,
  AtTabsPane,
  AtIcon,
} from 'taro-ui';
import { dateList } from '../../utils/tools/config';

import './index.less';

import "taro-ui/dist/style/components/search-bar.scss";
import 'taro-ui/dist/style/components/tabs.scss';
import "taro-ui/dist/style/components/flex.scss";
import "taro-ui/dist/style/components/action-sheet.scss";
import "taro-ui/dist/style/components/icon.scss";
import "taro-ui/dist/style/components/list.scss";
import "taro-ui/dist/style/components/button.scss";

import MyList from './myList';

class Project extends Component {

  constructor(props) {
    super(props);
    this.state = {
      typeList: [
        { title: '全部', type: 1 },
        // { title: '我创建', type: 2 },
        // { title: '我参与', type: 3 }
      ],
      current: 0,
      isPickerRender: false,
      list: [],
      pageNum: 1,
      listState: 'LOADING',
      searchType: { title: "按项目名称", desc: "请输入项目名称", id: 0 },
      search: [
        { title: "按项目名称", desc: "请输入项目名称", id: 0 },
        { title: "按负责人", desc: "请输入项目负责人", id: 1 },
        { title: "研发人员", desc: "请输入研发人员名称", id: 2 },
      ]

    }
    this.getProjectList = this.getProjectList.bind(this);
    this.getMyList = this.getMyList.bind(this);
    this.onSetPickerTime = this.onSetPickerTime.bind(this);
    this.onPickerHide = this.onPickerHide.bind(this)
  }

  componentDidShow() {
    this.getProjectList();
  }

  // async componentDidMount() {
  //   await this.getProjectList();
  // }

  onPullDownRefresh() {
    this.getProjectList();
  }

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

  // onTabItemTap(obj) {
  //   if (obj.index === 1) {
  //     this.onTabTap();
  //   }
  // }


  async onTabTap() {
    await this.getMyList(1);
  }

  async getProjectList(lv) {
    await this.getMyList(lv ? this.state.pageNum + 1 : 1);
    Taro.stopPullDownRefresh();
  }

  async getMyList(pageNum) {
    const { current } = this.state
    this.setState({
      listState: 'LOADING'
    })
    let data = {
      pageNum: pageNum,
      pageSize: 10,
      roleType: 0,
      projectYear: this.state.starts,
      startTime: this.state.rangeStartMinuteVal || undefined,
      endTime: this.state.rangeEndMinuteVal || undefined,
      name: this.state.searchType.id == 0 ? this.state.searchValue : undefined,
      headName: this.state.searchType.id == 1 ? this.state.searchValue : undefined,
      staffName: this.state.searchType.id == 2 ? this.state.searchValue : undefined,
    }
    for (let i in data) {
      if (!data[i] && data[i] != 0) {
        delete data[i]
      }
    }
    let msg = await getProjectList(data);
    if (msg.code === 200) {
      if (msg.total === 0) {
        this.setState({
          listState: 'NO_DATA'
        })
      } else if (msg.total === this.state.list.length && pageNum !== 1) {
        !this.state.isOpen && Taro.showToast({ title: '没有更多数据了', icon: 'none' });
        this.setState({
          listState: 'NO_MORE_DATA'
        })
      } else {
        if (msg.total === (pageNum === 1 ? msg.rows : this.state.list.concat(msg.rows)).length) {
          this.setState({
            listState: 'NO_MORE_DATA'
          })
        }
      }
      this.setState({
        list: pageNum === 1 ? msg.rows : this.state.list.concat(msg.rows),
        pageNum: msg.rows.length === 0 ? this.state.pageNum : msg.next - 1
      })
    } else {
      Taro.showToast({ title: msg.msg, icon: 'none' });
      this.setState({
        // listState: msg.error[0].field === '403' ? 'NO_DATA' : 'RELOAD'
      })
    }
    Taro.stopPullDownRefresh();
  }

  onPageScroll(event) {
    let touchMove = event.scrollTop;
    if (touchMove >= 37) {
      this.setState({
        openSearch: true
      })
    } else {
      this.setState({
        openSearch: false
      })
    }
  }

  async onSetPickerTime(val) {
    let data = val.detail;
    await this.setState({
      rangeStartMinuteVal: dayjs(data.selectStartTime).format("YYYY-MM-DD"),
      rangeEndMinuteVal: dayjs(data.selectEndTime).format("YYYY-MM-DD")
    })
    await this.getMyList(1);
  }

  onPickerHide() {
    this.setState({
      isPickerRender: false,
    });
  }

  render() {
    const { searchType, search } = this.state
    return (
      <View className='indexPage' >
        <View className='searchContent'>
          <View className='searchTop'>
            <Picker
              value={searchType.id}
              range={search} rangeKey='title' mode='selector'
              onChange={(e) => {
                this.setState({
                  searchType: search[e.detail.value],
                })
              }}>
              <View className='shortValuecontent'>
                <View className='selectTitle'>{searchType.title}</View>
                <View className='iconContent'><AtIcon value='chevron-down' size='10' color='#FFFFFF' /></View>
              </View>
            </Picker>
            <AtSearchBar
              showActionButton
              placeholder={searchType.desc}
              value={this.state.searchValue}
              onActionClick={() => {
                this.getProjectList();
              }}
              onChange={(value) => {
                this.setState({
                  searchValue: value
                })
              }}
              onClear={() => {
                this.setState({
                  searchValue: ''
                })
              }}
            />
          </View>
          <ScrollView className={this.state.openSearch ? 'searchBottomLOL' : ''} scrollX style={{ width: '100%' }}>
            <View className='searchBottom'>
              <View className='searchItem' style={{ paddingLeft: '5px' }}>
                <Picker
                  value={this.state.starts}
                  range={dateList} mode='selector'
                  onChange={(e) => {
                    this.setState({
                      starts: dateList[e.detail.value],
                    }, () => {
                      this.getProjectList();
                    })
                  }}>
                  {
                    !this.state.starts ?
                      <View className='shortValuecontent'>
                        <View className='selectTitle'>年份</View>
                        <View className='iconContent'><AtIcon value='chevron-down' size='10' color='#FFFFFF' /></View>
                      </View> :
                      <View className='shortValuecontent'>
                        <View className='selectValue'>
                          {this.state.starts}年
                        </View>
                        <View className='iconContent' />
                      </View>
                  }
                </Picker>
                {
                  this.state.starts &&
                  <View className='searchSelectContent'>
                    <View className='selectIcon'
                      onClick={(e) => {
                        e.stopPropagation();
                        this.setState({
                          starts: ""
                        }, () => {
                          this.getProjectList();
                        })
                      }}>
                      <AtIcon value='close-circle' size='10' color='#FFFFFF' />
                    </View>
                  </View>
                }
              </View>
              <View className='searchItem' onClick={() => {
                this.setState({
                  isPickerRender: true
                })
              }}>
                <View>
                  {
                    !this.state.rangeStartMinuteVal ?
                      <View className='valuecontent'>
                        <View className='selectTitle'>开始及结束时间</View>
                        <View className='iconContent' ><AtIcon value='chevron-down' size='10' color='#FFFFFF' /></View>
                      </View> :
                      <View className='valuecontent'>
                        <View className='selectValue' >
                          {this.state.rangeStartMinuteVal + "~" + this.state.rangeEndMinuteVal}
                        </View>
                        <View className='iconContent' />
                      </View>
                  }
                </View>
                {
                  this.state.rangeStartMinuteVal &&
                  <View className='searchSelectContent'>
                    <View className='selectIcon'
                      onClick={(e) => {
                        e.stopPropagation();
                        this.setState({
                          rangeStartMinuteVal: '',
                          rangeEndMinuteVal: '',
                        }, () => {
                          this.getProjectList();
                        })
                      }}>
                      <AtIcon value='close-circle' size='10' color='#FFFFFF' />
                    </View>
                  </View>
                }
              </View>
            </View>
          </ScrollView>
        </View>
        <AtTabs
          swipeable={false}
          current={this.state.current || 0}
          tabList={this.state.typeList}
          onClick={(current) => {
            this.setState({
              list: [],
              pageNum: 1,
              current,
            }, () => {
              this.getProjectList();
            })
          }}>
          {
            this.state.typeList?.map((item, index) =>
              <AtTabsPane
                current={this.state.current}
                index={index}
              >
                <MyList
                  title={item.title}
                  typelist={this.state.typeList}
                  seeView={this.state.current}
                  list={this.state.list}
                  listState={this.state.listState}
                  onRefresh={() => {
                    this.getProjectList(true);
                  }}
                />
              </AtTabsPane>
            )
          }
        </AtTabs>
        <timePicker
          config={{
            endDate: true,
            column: "minute",
            dateLimit: false,
            limitStartTime: dayjs().subtract(3, 'year').format('YYYY-MM-DD '),
            limitEndTime: dayjs().add(3, 'year').format('YYYY-MM-DD ')
          }}
          isPartition
          pickerShow={this.state.isPickerRender}
          onconditionaljudgment={(v) => {
          }}
          onhidepicker={() => {
            this.onPickerHide()
          }}
          onsetpickertime={(v) => {
            this.onSetPickerTime(v)
          }}>
        </timePicker>
      </View>
    )
  }
}

export default Project
