import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import styles from './index.module.scss'
import { B3_4inputKeyArr, B3baseFormData, D3InputKeyType } from '@/pages/B_enterTibet/B3_4page'
import { Button, DatePicker, Input, Select } from 'antd'
import MyPopconfirm from '@/components/MyPopconfirm'
import history from '@/utils/history'
import dayjs from 'dayjs'
import MyTable from '@/components/MyTable'
import { B3tableC } from '@/utils/tableData'
const { RangePicker } = DatePicker

function C6edit() {
  // 没有接入后端 待完善

  const [formData, setFormData] = useState(B3baseFormData)
  const formDataRef = useRef(B3baseFormData)

  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: D3InputKeyType) => {
      setFormData({
        ...formData,
        [key]: txt
      })
    },
    [formData]
  )

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

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

  // 点击删除
  const delTableFu = useCallback(
    async (id: number) => {
      getListFu()
    },
    [getListFu]
  )

  // 点击各种操作按钮
  const btnFu = useCallback((id: number | string, key: string) => {
    history.push(`/edit_add/${key}/${id}`)
  }, [])

  const tableLastBtn = useMemo(() => {
    //   看状态和账号角色显示按钮 待完善
    return [
      {
        title: '操作',
        render: (item: any) => {
          return (
            <>
              <Button size='small' type='text' onClick={() => btnFu(item.id, '2')}>
                编辑
              </Button>
              <Button size='small' type='text' onClick={() => btnFu(item.id, '3')}>
                审批
              </Button>
              <Button size='small' type='text' onClick={() => btnFu(item.id, '4')}>
                查看
              </Button>
              <MyPopconfirm txtK='删除' onConfirm={() => delTableFu(item.id)} />
            </>
          )
        }
      }
    ]
  }, [btnFu, delTableFu])

  return (
    <div className={styles.C6edit}>
      <div className='pageTitle'>藏品编辑</div>
      <div className='C6top'>
        <div className='C6topll'>
          {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='C6toprr'>
          <Button type='primary'>批量导出</Button>&emsp;
          <Button type='primary' onClick={() => btnFu('null', '1')}>
            新增
          </Button>
        </div>
      </div>
      <div className='C6top'>
        <div className='C6topll'>
          <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.status}
              onChange={e => setFormData({ ...formData, status: e })}
              options={[{ value: '', label: '全部' }]}
            />
          </div>

          <div>
            <span>选择角色：</span>
            <Select
              style={{ width: 200 }}
              placeholder='请选择'
              options={[]}
              fieldNames={{ value: 'id', label: 'roleName' }}
              value={formData.userType}
              onChange={e => setFormData({ ...formData, userType: e })}
            />
          </div>
        </div>
        <div className='C6toprr'>
          <Button type='primary' onClick={clickSearch}>
            查询
          </Button>
          &emsp;<Button onClick={resetSelectFu}>重置</Button>
        </div>
      </div>

      {/* 表格 */}
      <MyTable
        yHeight={610}
        list={[{ id: 91 }]}
        columnsTemp={B3tableC}
        lastBtn={tableLastBtn}
        pageNum={formData.pageNum}
        pageSize={formData.pageSize}
        total={0}
        onChange={(pageNum, pageSize) => paginationChange(pageNum, pageSize)}
      />
    </div>
  )
}

const MemoC6edit = React.memo(C6edit)

export default MemoC6edit
