import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import styles from './index.module.scss'
import { useDispatch, useSelector } from 'react-redux'
import { A1_APIgetList } from '@/store/action/A1record'
import { RootState } from '@/store'
import { Button, Input, Select } from 'antd'
import { A1select1 } from './data'
import MyTable from '@/components/MyTable'
import { A1tableC } from '@/utils/tableData'
import A1look from './A1look'

export const A1baseFromData = {
  pageNum: 1,
  pageSize: 10,
  searchKey: '',
  type: ''
}

function A1record() {
  const dispatch = useDispatch()

  const [fromData, setFromData] = useState(A1baseFromData)

  const getListFu = useCallback(() => {
    dispatch(A1_APIgetList(fromData))
  }, [dispatch, fromData])

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

  const tableInfo = useSelector((state: RootState) => state.A1record.tableInfo)

  // 作品名称的输入
  const timeRef = useRef(-1)
  const [inputKey, setInputKey] = useState(0)
  const fromKeyChangeFu = useCallback(
    (e: React.ChangeEvent<HTMLInputElement>, key: 'searchKey') => {
      clearTimeout(timeRef.current)
      timeRef.current = window.setTimeout(() => {
        setFromData({ ...fromData, [key]: e.target.value, pageNum: 1 })
      }, 500)
    },
    [fromData]
  )

  // 下拉框的改变
  const selectChangeFu = useCallback(
    (value: any, key: 'type') => {
      setFromData({ ...fromData, [key]: value, pageNum: 1 })
    },
    [fromData]
  )

  // 点击重置
  const resetSelectFu = useCallback(() => {
    setInputKey(Date.now())
    setFromData({ ...A1baseFromData })
  }, [])

  // 点击查看
  const [lookId, setLookId] = useState(0)

  const tableLastBtn = useMemo(() => {
    return [
      {
        title: '操作',
        render: (item: any) => {
          return (
            <>
              <Button size='small' type='text' onClick={() => setLookId(item.id)}>
                查看
              </Button>
            </>
          )
        }
      }
    ]
  }, [])

  return (
    <div className={styles.A1record}>
      <div className='pageTitle'>古桥档案{lookId ? ' - 查看' : ''}</div>

      {/* 顶部筛选 */}
      <div className='A1top'>
        <div className='A1topLeft'>
          <div>
            <Input
              key={inputKey}
              maxLength={30}
              showCount
              style={{ width: 400 }}
              placeholder='请输入古桥名称/建造年代/详细地址'
              allowClear
              onChange={e => fromKeyChangeFu(e, 'searchKey')}
            />
          </div>

          <div>
            <Select
              allowClear
              style={{ width: 150 }}
              placeholder='结构类型'
              value={fromData.type || null}
              onChange={e => selectChangeFu(e, 'type')}
              options={A1select1}
            />
          </div>
        </div>

        <div>
          <Button onClick={resetSelectFu}>重置</Button>
        </div>
      </div>

      {/* 表格主体 */}
      <div className='A1tableBox'>
        <MyTable
          yHeight={645}
          list={tableInfo.list}
          columnsTemp={A1tableC}
          lastBtn={tableLastBtn}
          pageNum={fromData.pageNum}
          pageSize={fromData.pageSize}
          total={tableInfo.total}
          onChange={(pageNum, pageSize) => setFromData({ ...fromData, pageNum, pageSize })}
        />
      </div>

      {lookId ? (
        <A1look
          sId={lookId}
          closeFu={flag => {
            setLookId(0)
            if (flag) getListFu()
          }}
        />
      ) : null}
    </div>
  )
}

const MemoA1record = React.memo(A1record)

export default MemoA1record
