import React, { Component } from 'react';
import Taro from '@tarojs/taro';
import { View, Picker, ScrollView } from '@tarojs/components'
import { getPublicReleaseList } from '../../utils/servers/servers';
import dayjs from 'dayjs';
import {
  AtSearchBar,
  AtTabs,
  AtTabsPane,
  AtList,
  AtListItem,
  AtIcon
} from 'taro-ui';
import { clockState } from '../../utils/tools/config';

import './index.less';

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 MyList from './myList';
import MessageNoticebar from "../../components/common/messageNoticebar";

class Examine extends Component {

  constructor(props) {
    super(props);
    this.state = {
      typeList: [
        { title: '审核', type: 1 },
        { title: '查看公出', type: 2 },
        { title: '查看协单', type: 3 }],
      current: 0,
      isPickerRender: false,
      validDates: '',

      list: [],
      pageNo: 1,
      listState: 'LOADING',
      starts: {},
      clockInStarts: '',
      rangeEndVal: '',
      rangeStartVal: '',
      rangeEndMinuteVal: '',
      rangeStartMinuteVal: '',
    }
    this.getPublicReleaseList = this.getPublicReleaseList.bind(this);
    this.getMyList = this.getMyList.bind(this);
    this.onSetPickerTime = this.onSetPickerTime.bind(this);
    this.onPickerHide = this.onPickerHide.bind(this)
  }

  componentDidShow() {
    Taro.eventCenter.on('listOperation', (data) => {
      if (!isNaN(parseInt(data.index))) {
        let arr = this.state.list.concat([]);
        arr[data.index] = {
          ...arr[data.index],
          ...data
        }
        this.setState({
          list: arr
        })
      }
    })
  }

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

  onPullDownRefresh() {
    this.getPublicReleaseList();
  }

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

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

  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);
  }

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

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

  async getMyList(pageNo) {
    const { current } = this.state
    this.setState({
      listState: 'LOADING'
    })
    let data = {
      pageNo: pageNo,
      pageSize: 10,
      type: current === 0
        ? "1" : current === 1
          ? "2" : current === 2 && "3",
      releaseStarts: this.state.rangeStartMinuteVal || undefined,
      releaseEnds: this.state.rangeEndMinuteVal || undefined,
      status: isNaN(parseInt(this.state.starts.id)) ? undefined : String(this.state.starts.id),
      clockIn: this.state.clockInStarts || undefined,
      userName: this.state.searchValue || undefined,
    }
    for (let i in data) {
      if (!data[i]) {
        delete data[i]
      }
    }
    let msg = await getPublicReleaseList(data);
    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 {
        if (msg.data.totalCount === (pageNo === 1 ? msg.data.list : this.state.list.concat(msg.data.list)).length) {
          this.setState({
            listState: 'NO_MORE_DATA'
          })
        }
      }
      this.setState({
        list: pageNo === 1 ? msg.data.list : this.state.list.concat(msg.data.list),
        pageNo: msg.data.pageNo
      })
    } else {
      Taro.showToast({ title: msg.error[0].message, 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
      })
    }
  }

  render() {
    return (
      <View className='indexPage' >
        <MessageNoticebar />
        <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.starts.id}
                  range={clockState} rangeKey='title' mode='selector'
                  onChange={(e) => {
                    this.setState({
                      starts: clockState[e.detail.value],
                    }, () => {
                      this.getPublicReleaseList();
                    })
                  }}>
                  {
                    !this.state.starts.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.starts.title}
                        </View>
                        <View className='iconContent' />
                      </View>
                  }
                </Picker>
                {
                  this.state.starts.title &&
                  <View className='searchSelectContent'>
                    <View className='selectIcon'
                      onClick={(e) => {
                        e.stopPropagation();
                        this.setState({
                          starts: {}
                        }, () => {
                          this.getPublicReleaseList();
                        })
                      }}>
                      <AtIcon value='close-circle' size='10' color='#FFFFFF' />
                    </View>
                  </View>
                }
              </View>
              <View className='searchItem' >
                <Picker
                  value={this.state.clockInStarts}
                  range={['未打卡', '已打卡']}
                  mode='selector'
                  onChange={(e) => {
                    this.setState({
                      clockInStarts: String(e.detail.value)
                    }, () => {
                      this.getPublicReleaseList();
                    })
                  }}>
                  {
                    !this.state.clockInStarts ?
                      <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.clockInStarts === '1'
                              ? '已打卡' : this.state.clockInStarts === '0'
                                ? '未打卡' : ''
                          }
                        </View>
                        <View className='iconContent' />
                      </View>
                  }
                </Picker>
                {
                  this.state.clockInStarts ?
                    <View className='searchSelectContent'>
                      <View className='selectIcon'
                        onClick={(e) => {
                          e.stopPropagation();
                          this.setState({
                            clockInStarts: '',
                          }, () => {
                            this.getPublicReleaseList();
                          })
                        }}>
                        <AtIcon value='close-circle' size='10' color='#FFFFFF' />
                      </View>
                    </View> : null
                }
              </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>
              <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) => {
                  // let a = dayjs(dayjs(v.detail.endTime).second(0).format('YYYY-MM-DD'));
                  // let b = dayjs(dayjs().second(0).format('YYYY-MM-DD'))
                  // if(a.isBefore(b)){
                  //   Taro.showToast({
                  //     title:'结束时间不能小于当前时间',
                  //     icon:'none'
                  //   })
                  //   v.detail.setLv(false);
                  // }
                }}
                onhidepicker={() => {
                  this.onPickerHide()
                }}
                onsetpickertime={(v) => {
                  this.onSetPickerTime(v)
                }}>
              </timePicker>
            </View>
          </ScrollView>
        </View>
        <AtTabs
          current={this.state.current || 0}
          tabList={this.state.typeList}
          onClick={(current) => {
            this.setState({
              pageNo: 1,
              current,
            }, () => {
              this.getPublicReleaseList();
            })
          }}>
          {
            this.state.typeList?.map((item, index) =>
              <AtTabsPane
                key={item.type}
                current={this.state.current}
                index={index}
              >
                <MyList
                  type={item.type}
                  typelist={this.state.typeList}
                  seeView={this.state.current}
                  list={this.state.list}
                  listState={this.state.listState}
                  onRefresh={() => {
                    this.getPublicReleaseList(true);
                  }} />
              </AtTabsPane>
            )
          }
        </AtTabs>
      </View>
    )
  }
}

export default Examine
