import MyTable from '@/components/MyTable'
import { Y44tableC } from '@/utils/tableData'
import { Button, DatePicker, Input, Select } from 'antd'
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { TypeY44form } from './type'
import { B3_4inputKeyArr } from '@/pages/B_enterTibet/B3_4page'
import dayjs from 'dayjs'
const { RangePicker } = DatePicker

type InputKeyType = 'num' | 'name' | 'deptName' | 'userName'

const baseFormData: TypeY44form = {
  pageSize: 10,
  pageNum: 1,
  num: '',
  name: '',
  deptName: '',
  userName: '',
  startTime: '',
  endTime: '',
  status: '',
  // 待完善 type字段
  type: ''
}
// 待完善 没有接入后端

type Props = {
  isLook?: boolean
  sId: number
}

function Y44com({ isLook, sId }: Props) {
  const [formData, setFormData] = useState(baseFormData)
  const formDataRef = useRef(baseFormData)

  useEffect(() => {
    formDataRef.current = formData
  }, [formData])

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

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

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

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

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

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

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

  // 点击查看 新窗口打开
  const lookPage = useCallback((id: number) => {
    // 根据业务类型打开不同的新页面 待完善
    window.open('/#/entering_edit/4/1/1', '_blank')
  }, [])

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

  return (
    <div className='Y44com'>
      {isLook ? (
        <>
          <div className='Y44top'>
            <div className='Y44topll'>
              {B3_4inputKeyArr.map(item => (
                <div key={item.name}>
                  <span>{item.name}：</span>
                  <Input
                    placeholder={`请输入${item.name}`}
                    maxLength={30}
                    value={formData[item.key]}
                    onChange={e => txtChangeFu(e.target.value, item.key)}
                  />
                </div>
              ))}
            </div>
            <div className='Y44toprr'></div>
          </div>
          <div className='Y44top'>
            <div className='Y44topll'>
              <div>
                <span>发起日期范围：</span>
                <RangePicker
                  value={
                    formData.startTime ? [dayjs(formData.startTime), dayjs(formData.endTime)] : null
                  }
                  onChange={timeChange}
                />
              </div>

              <div>
                <span>业务类型：</span>
                <Select
                  placeholder='请选择'
                  style={{ width: 200 }}
                  value={formData.type}
                  onChange={e => setFormData({ ...formData, type: e })}
                  options={[{ value: '', label: '全部' }]}
                />
              </div>
            </div>
            <div className='Y44toprr'>
              <Button type='primary' onClick={clickSearch}>
                查询
              </Button>
              &emsp;<Button onClick={resetSelectFu}>重置</Button>
            </div>
          </div>
        </>
      ) : null}

      {/* 表格 */}
      <MyTable
        classKey='Y44comTable'
        yHeight={isLook ? 600 : 666}
        list={[{ id: 1, xxxx: '藏品入馆' }]}
        columnsTemp={Y44tableC}
        lastBtn={tableLastBtn}
        pagingInfo={false}
      />
    </div>
  )
}

const MemoY44com = React.memo(Y44com)

export default MemoY44com
