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

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/icon.scss";
import "taro-ui/dist/style/components/modal.scss";

import MyList from './myList';

class Examine extends Component {

  constructor(props) {
    super(props);
    this.state = {
      typeList: [
        { title: '全部', type: 0 },
        { title: '待审核', type: 1 },
        { title: '已审核', type: 2 },
        { title: '已驳回', type: 3 }],
      seltype: {},
      current: 0,
      tlist: [{ title: '我的打卡', type: 0 }],
      tabs: 0,
      list: [],
      pageNum: 1,
      listState: 'LOADING',
      isPickerRender: false,
      rangeEndMinuteVal: '',
      rangeStartMinuteVal: '',

    }
    this.getPublicReleaseList = this.getPublicReleaseList.bind(this);
    this.getMyList = this.getMyList.bind(this);
  }

  componentDidShow() {
    let roles = Taro.getStorageSync('roles');
    // 项目经理  技术总监
    if (roles.includes("manage") || roles.includes("Inspector")) {
      this.setState({
        tlist: [{ title: '我的打卡', type: 0 }, { title: '审核', type: 1 }]
      })
    }
    this.getPublicReleaseList();
  }

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

  onPullDownRefresh() {
    this.getPublicReleaseList();
  }

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

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

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

  async getPublicReleaseList(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 = {
      processStatus: this.state.current != 0 ? this.state.current : "",
      pageNum: pageNum,
      pageSize: 10,
      roleType: this.state.tabs,
      startTime: this.state.rangeStartMinuteVal || undefined,
      endTime: this.state.rangeEndMinuteVal || undefined,
      projectName: this.state.searchValue || undefined,
    }
    for (let i in data) {
      if (!data[i] && data[i] != 0) {
        delete data[i]
      }
    }
    let msg = await listRecord(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();
  }

  onPickerHide() {
    this.setState({
      isPickerRender: 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);
  }


  render() {
    return (
      <View className='indexPage' >
        <View className='searchContent'>
          <View className='searchTop'>
            <AtSearchBar
              showActionButton
              placeholder='请输项目名称'
              value={this.state.searchValue}
              onActionClick={() => {
                this.getPublicReleaseList();
              }}
              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.seltype.type}
                  range={this.state.typeList} rangeKey='title' mode='selector'
                  onChange={(e) => {
                    this.setState({
                      seltype: this.state.typeList[e.detail.value],
                      current: this.state.typeList[e.detail.value].type,
                    }, () => {
                      this.getPublicReleaseList();
                    })
                  }}>
                  {
                    !this.state.seltype.title ?
                      <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.seltype.title}
                        </View>
                        <View className='iconContent' />
                      </View>
                  }
                </Picker>
                {
                  this.state.seltype.title &&
                  <View className='searchSelectContent'>
                    <View className='selectIcon'
                      onClick={(e) => {
                        e.stopPropagation();
                        this.setState({
                          seltype: {},
                          current: 0,
                        }, () => {
                          this.getPublicReleaseList();
                        })
                      }}>
                      <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' style={{ width: '200px' }}>
                        <View className='selectTitle' style={{ paddingLeft: '50px' }}>开始及结束时间</View>
                        <View className='iconContent' style={{ paddingLeft: '20px' }}><AtIcon value='chevron-down' size='10' color='#FFFFFF' /></View>
                      </View> :
                      <View className='valuecontent' style={{ width: '200px' }}>
                        <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.getPublicReleaseList();
                        })
                      }}>
                      <AtIcon value='close-circle' size='10' color='#FFFFFF' />
                    </View>
                  </View>
                }
              </View>
            </View>
          </ScrollView>

          <AtTabs
            swipeable={false}
            current={this.state.tabs || 0}
            tabList={this.state.tlist}
            onClick={(tabs) => {
              this.setState({
                list: [],
                pageNum: 1,
                tabs: this.state.tlist[tabs].type,
              }, () => {
                this.getPublicReleaseList();
              })
            }}
          >

            {
              this.state.typeList?.map((item, index) =>
                <AtTabsPane
                  current={this.state.tabs}
                  index={index}
                >
                  <MyList
                    type={this.state.tabs}
                    title={item.title}
                    typelist={this.state.typeList}
                    seeView={this.state.tabs}
                    list={this.state.list}
                    listState={this.state.listState}
                    onRefresh={() => {
                      this.getProjectList(true);
                    }}
                  />
                </AtTabsPane>
              )
            }

          </AtTabs>
        </View>

        <timePicker
          config={{
            endDate: true,
            column: "day",
            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 Examine
