import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import styles from './index.module.scss'
import { C1InputKeyArr1 } from '../C1ledger/data'
import { Button, Cascader, Input, Select } from 'antd'
import { C8baseFormData } from './data'
import { openGoodsInfoFu } from '@/utils/history'
import classNames from 'classnames'
import { C1InputKeyType } from '../C1ledger/type'
import MyTable from '@/components/MyTable'
import { C1tableC } from '@/utils/tableData'
import Y1cathet from '@/pages/Y_goodsDetails/Y1cathet'
import MyPopconfirm from '@/components/MyPopconfirm'

function C8recycleBin() {
  const [formData, setFormData] = useState(C8baseFormData)
  const formDataRef = useRef(C8baseFormData)
  useEffect(() => {
    formDataRef.current = formData
  }, [formData])

  // 点击搜索的 时间戳
  const [timeKey, setTimeKey] = useState(0)

  // 点击搜索
  const clickSearch = useCallback(() => {
    setFormData({ ...formData, pageNum: 1 })
    setTimeout(() => {
      setTimeKey(Date.now())
    }, 50)
  }, [formData])

  // 封装发送请求的函数
  const getListFu = useCallback(() => {
    console.log('发送请求', formDataRef.current)
  }, [])

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

  // 输入框的改变
  const txtChangeFu = useCallback(
    (txt: string, key: C1InputKeyType) => {
      setFormData({
        ...formData,
        [key]: txt
      })
    },
    [formData]
  )

  // 点击重置
  const resetSelectFu = useCallback(() => {
    setFormData(C8baseFormData)
    setTimeout(() => {
      setTimeKey(Date.now())
    }, 50)
  }, [])

  // 页码变化
  const paginationChange = useCallback(
    (pageNum: number, pageSize: number) => {
      setFormData({ ...formData, pageNum, pageSize })
      setTimeout(() => {
        setTimeKey(Date.now())
      }, 50)
    },
    [formData]
  )
  // 待完善 各种参数 接口

  const regainFu = useCallback((id: number) => {}, [])

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

  // 打开侧边栏
  const [cathet, setCathet] = useState(0)

  const startBtn = useMemo(() => {
    return [
      {
        title: '藏品编号',
        render: (item: any) => {
          return (
            <span
              onClick={() => setCathet(item.id)}
              className={classNames('D1GtNum', item.id === cathet ? 'D1GtNumAc' : '')}
            >
              {item.num}
            </span>
          )
        }
      }
    ]
  }, [cathet])

  return (
    <div className={styles.C8recycleBin}>
      <div className='pageTitle'>回收站</div>
      <div className='C8top'>
        <div className='C8topll'>
          {C1InputKeyArr1.map(item => (
            <div key={item.name}>
              <span>{item.name}：</span>

              {item.type === '下拉框' ? <Select placeholder='全部' /> : null}
              {item.type === '输入框' ? (
                <Input
                  placeholder='请输入'
                  maxLength={30}
                  value={formData[item.key as 'aaaa']}
                  onChange={e => txtChangeFu(e.target.value, item.key)}
                />
              ) : null}

              {item.type === '级联' ? (
                <Cascader
                  changeOnSelect
                  options={[]}
                  placeholder='全部'
                  // fieldNames={{ label: 'name', value: 'id', children: 'children' }}
                  allowClear={false}
                />
              ) : null}
            </div>
          ))}
        </div>
        <div className='C8toprr'>
          <Button type='primary' onClick={clickSearch}>
            查询
          </Button>
          &emsp;
          <Button onClick={resetSelectFu}>重置</Button>
        </div>
      </div>

      {/* 表格 */}
      <MyTable
        yHeight={586}
        list={[{ id: 66, thumb: '', num: 'xxx' }]}
        columnsTemp={[...C1tableC, ['txt', '删除日期', 'xxxx']]}
        lastBtn={tableLastBtn}
        startBtn={startBtn}
        pageNum={formData.pageNum}
        pageSize={formData.pageSize}
        total={0}
        onChange={(pageNum, pageSize) => paginationChange(pageNum, pageSize)}
      />

      {/* 打开侧边栏 */}
      <Y1cathet sId={cathet} closeFu={() => setCathet(0)} />
    </div>
  )
}

const MemoC8recycleBin = React.memo(C8recycleBin)

export default MemoC8recycleBin
