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

const baseFormData: TypeD4Form = {
  pageSize: 10,
  pageNum: 1,
  num: '',
  name: '',
  deptName: '',
  userName: '',
  startTime: '',
  endTime: '',
  status: null,
  userType: null,
  storageInId: null
}

// 没有接入后端 待完善

function D6putsStor() {
  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: D3InputKeyType) => {
      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 delTableFu = useCallback(
    async (id: number) => {
      getListFu()
    },
    [getListFu]
  )

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

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

  return (
    <div className={styles.D6putsStor}>
      <div className='pageTitle'>出库</div>

      <div className='D4top'>
        <div className='D4topll'>
          {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='D4toprr'>
          <Button type='primary'>批量导出</Button>&emsp;
          <Button type='primary' onClick={() => history.push('/putsStor_edit/1/null')}>
            新增
          </Button>
        </div>
      </div>
      <div className='D4top'>
        <div className='D4topll'>
          <div>
            <span>发起日期范围：</span>
            <RangePicker
              value={
                formData.startTime ? [dayjs(formData.startTime), dayjs(formData.endTime)] : null
              }
              onChange={timeChange}
            />
          </div>

          <div>
            <span>申请状态：</span>
            <Select
              placeholder='请选择'
              style={{ width: 150 }}
              value={formData.status}
              onChange={e => setFormData({ ...formData, status: e })}
              options={[{ value: '', label: '全部' }]}
            />
          </div>

          <div>
            <span>选择角色：</span>
            <Select
              style={{ width: 150 }}
              placeholder='请选择'
              options={[]}
              fieldNames={{ value: 'id', label: 'roleName' }}
              value={formData.userType}
              onChange={e => setFormData({ ...formData, userType: e })}
            />
          </div>

          <div>
            <span>出库库房：</span>
            {/* 拿库房设置的列表 */}
            <Select
              placeholder='请选择'
              style={{ width: 150 }}
              value={formData.storageInId}
              onChange={e => setFormData({ ...formData, storageInId: e })}
              options={[{ value: '', label: '全部' }]}
            />
          </div>
        </div>
        <div className='D4toprr'>
          <Button type='primary' onClick={clickSearch}>
            查询
          </Button>
          &emsp;<Button onClick={resetSelectFu}>重置</Button>
        </div>
      </div>

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

const MemoD6putsStor = React.memo(D6putsStor)

export default MemoD6putsStor
