import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import styles from './index.module.scss'
import { Button, DatePicker, Input, Select } from 'antd'
import { useDispatch, useSelector } from 'react-redux'
import { C2fromDataBase } from '../C2storageIn/data'
import { C3_APIgetlist } from '@/store/action/C3storageMove'
import { RootState } from '@/store'
import { B1listType } from '../B1ledger/data'
import history, { aduitBtnRoleFu, resubmitBtnRoleFu } from '@/utils/history'
import { statusSelect } from '@/utils/select'
import { C2devFu } from '@/utils/deriveFu'
import MyTable from '@/components/MyTable'
import { C2tableCFu } from '@/utils/tableData'

const { RangePicker } = DatePicker

function C3storageMove() {
  const dispatch = useDispatch()
  const [formData, setFormData] = useState({ ...C2fromDataBase, type: 'YK' })

  const getListFu = useCallback(() => {
    dispatch(C3_APIgetlist(formData))
  }, [dispatch, formData])

  useEffect(() => {
    getListFu()
  }, [getListFu])

  // 输入框改变
  const timeRef = useRef(-1)
  const txtChangeFu = useCallback(
    (e: React.ChangeEvent<HTMLInputElement>, key: 'num') => {
      clearTimeout(timeRef.current)
      timeRef.current = window.setTimeout(() => {
        setFormData({
          ...formData,
          [key]: e.target.value,
          pageNum: 1
        })
      }, 500)
    },
    [formData]
  )

  // 时间选择器改变
  const timeChange = useCallback(
    (date: any, dateString: any) => {
      let startTime = ''
      let endTime = ''
      if (dateString[0] && dateString[1]) {
        startTime = dateString[0] + ' 00:00:00'
        endTime = dateString[1] + ' 23:59:59'
      }
      setFormData({ ...formData, startTime, endTime, pageNum: 1 })
    },
    [formData]
  )

  // 点击重置
  const [inputKey, setInputKey] = useState(1)
  const resetSelectFu = useCallback(() => {
    // 把2个输入框和时间选择器清空
    setInputKey(Date.now())
    setFormData({ ...C2fromDataBase, type: 'YK' })
  }, [])

  // 从仓库拿数据
  const tableInfo = useSelector((state: RootState) => state.C3storageMove.tableInfo)

  const tableLastBtn = useMemo(() => {
    return [
      {
        title: '操作',
        render: (item: B1listType) => {
          return (
            <>
              <Button
                size='small'
                type='text'
                onClick={() => history.push(`/storageMove_look/3/${item.id}`)}
              >
                查看
              </Button>

              {aduitBtnRoleFu(item) ? (
                <Button
                  size='small'
                  type='text'
                  onClick={() => history.push(`/storageMove_look/4/${item.id}`)}
                >
                  审批
                </Button>
              ) : null}

              {resubmitBtnRoleFu(item) ? (
                <Button
                  size='small'
                  type='text'
                  onClick={() => history.push(`/storageMove_look/2/${item.id}`)}
                >
                  重新提交
                </Button>
              ) : null}
            </>
          )
        }
      }
    ]
  }, [])

  // 页码变化
  const paginationChange = useCallback(
    (pageNum: number, pageSize: number) => {
      setFormData({ ...formData, pageNum, pageSize })
    },
    [formData]
  )

  return (
    <div className={styles.C3storageMove}>
      <div className='pageTitle'>藏品移库</div>

      <div className='C3top'>
        <div className='C3topll' key={inputKey}>
          <RangePicker
            placeholder={['移库日期起', '移库日期终']}
            style={{ width: 240 }}
            onChange={timeChange}
          />
          &emsp;
          <Input
            placeholder='移库单编号'
            maxLength={30}
            showCount
            allowClear
            onChange={e => txtChangeFu(e, 'num')}
            style={{ width: 200 }}
          />
          &emsp;
          <Select
            allowClear
            style={{ width: 200 }}
            placeholder='申请状态'
            options={statusSelect}
            value={formData.status}
            onChange={e => setFormData({ ...formData, status: e })}
          />
          &emsp;
        </div>
        <div className='C3toprr'>
          <Button onClick={resetSelectFu}>重置</Button>&emsp;
          <Button type='primary' onClick={() => C2devFu(formData, '移库')}>
            数据导出
          </Button>
        </div>
      </div>

      {/* 表格 */}
      <MyTable
        yHeight={655}
        list={tableInfo.list}
        columnsTemp={C2tableCFu('移库')}
        lastBtn={tableLastBtn}
        pageNum={formData.pageNum}
        pageSize={formData.pageSize}
        total={tableInfo.total}
        onChange={(pageNum, pageSize) => paginationChange(pageNum, pageSize)}
      />
    </div>
  )
}

const MemoC3storageMove = React.memo(C3storageMove)

export default MemoC3storageMove
