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

const { RangePicker } = DatePicker

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

  const getListFu = useCallback(() => {
    dispatch(C2_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: 'RK' })
  }, [])

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

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

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

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

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

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

      <div className='C2top'>
        <div className='C2topll' 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='C2toprr'>
          <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 MemoC2storageIn = React.memo(C2storageIn)

export default MemoC2storageIn
