import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import styles from './index.module.scss'
import history, { aduitBtnRoleFu, resubmitBtnRoleFu } from '@/utils/history'
import { Button, DatePicker, Input, Select } from 'antd'
import { useDispatch, useSelector } from 'react-redux'
import { B2fromDataBase } from '../B2register/data'
import { RootState } from '@/store'
import { B3_APIgetlist } from '@/store/action/B3edit'
import { B1listType } from '../B1ledger/data'
import { statusSelect } from '@/utils/select'
import MyTable from '@/components/MyTable'
import { B2tableC } from '@/utils/tableData'
import { B2devFu } from '@/utils/deriveFu'
import A0addGoods from '../A0addGoods'
const { RangePicker } = DatePicker

function B3edit() {
  const dispatch = useDispatch()
  const [formData, setFormData] = useState({ ...B2fromDataBase, type: 'XG' })

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

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

  // 输入框改变
  const timeRef = useRef(-1)
  const txtChangeFu = useCallback(
    (e: React.ChangeEvent<HTMLInputElement>, key: 'num' | 'snapNum' | 'snapName') => {
      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({ ...B2fromDataBase, type: 'DJ' })
  }, [])

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

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

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

              {resubmitBtnRoleFu(item) ? (
                <Button
                  size='small'
                  type='text'
                  onClick={() => setEditId({ orderId: item.id, goodsId: Number(item.goodIds) })}
                >
                  重新提交
                </Button>
              ) : null}
            </>
          )
        }
      }
    ]
  }, [])

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

  // 点击重新提交
  const [editId, setEditId] = useState({ orderId: 0, goodsId: 0 })

  return (
    <div className={styles.B3edit}>
      <div className='pageTitle'>藏品修改</div>
      <div className='B3top'>
        <div className='B3topll' key={inputKey}>
          <Input
            placeholder='申请编号'
            maxLength={30}
            showCount
            allowClear
            onChange={e => txtChangeFu(e, 'num')}
            style={{ width: 200 }}
          />
          &emsp;
          <Input
            placeholder='藏品编号'
            maxLength={30}
            showCount
            allowClear
            onChange={e => txtChangeFu(e, 'snapNum')}
            style={{ width: 200 }}
          />
          &emsp;
          <Input
            placeholder='藏品名称'
            maxLength={30}
            showCount
            allowClear
            onChange={e => txtChangeFu(e, 'snapName')}
            style={{ width: 200 }}
          />
          &emsp;
          <RangePicker
            placeholder={['发起日期起', '发起日期终']}
            style={{ width: 240 }}
            onChange={timeChange}
          />
          &emsp;
          <Select
            allowClear
            style={{ width: 200 }}
            placeholder='申请状态'
            options={statusSelect}
            value={formData.status}
            onChange={e => setFormData({ ...formData, status: e })}
          />
          &emsp;
        </div>
        <div className='B3toprr'>
          <Button onClick={resetSelectFu}>重置</Button>&emsp;
          <Button type='primary' onClick={() => B2devFu(formData, '藏品修改')}>
            数据导出
          </Button>
        </div>
      </div>

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

      {/* 点击重新提交*/}
      {editId.goodsId ? (
        <A0addGoods
          sId={editId.goodsId}
          orderId={editId.orderId}
          closeFu={() => setEditId({ orderId: 0, goodsId: 0 })}
          isEditresubmit={true}
        />
      ) : null}
    </div>
  )
}

const MemoB3edit = React.memo(B3edit)

export default MemoB3edit
