import React, { useCallback, useEffect, useMemo, useState } from 'react'
import styles from './index.module.scss'
import { Button } from 'antd'
import { useDispatch, useSelector } from 'react-redux'
import { B1EditInfoType } from './data'
import { B_APIgetList, B_APIdel, B_APIexportExcel } from '@/store/action/Breserve'
import { RootState } from '@/store'
import { B1tableType } from '@/types'
import MyPopconfirm from '@/components/MyPopconfirm'
import MyTable from '@/components/MyTable'
import { B1tableC } from '@/utils/tableData'
import B1look from './B1look'
import { MessageFu } from '@/utils/message'
// import { exportExcelFile } from '@/utils/xlsxExport'

const pageDataBase = {
  pageNum: 1,
  pageSize: 10
}

function B1reserve1() {
  const dispatch = useDispatch()

  const [pageData, setPageData] = useState(pageDataBase)

  const getListFu = useCallback(() => {
    // status: -1 全部 0 未发布 1 已发布
    dispatch(B_APIgetList({ ...pageData, type: 1 }))
  }, [dispatch, pageData])

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

  // 点击重置
  const resetSelectFu = useCallback(() => {
    setPageData({ ...pageDataBase })
  }, [])

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

  const delTableFu = useCallback(
    async (id: number) => {
      const res = await B_APIdel(id)
      if (res.code === 0) {
        MessageFu.success('删除成功!')
        if (tableInfo.list.length === 1 && pageData.pageNum > 1) {
          setPageData({ ...pageData, pageNum: pageData.pageNum - 1 })
        }
        getListFu()
      }
    },
    [getListFu, pageData, tableInfo.list.length]
  )

  // 导出excel前端实现
  // const exportExcelFu = useCallback(() => {
  //   const listData = tableInfo.list.map(item => {
  //     const list = item.visitors.map(visitor => {
  //       return {
  //         申请时间: item.createTime,
  //         申请人姓名: item.userName,
  //         联系电话: item.phone,
  //         人数: String(item.pcs),
  //         预约日期: item.appointmentTime,
  //         预约时段: item.time,
  //         参观者姓名: visitor.name,
  //         参观者身份证号: visitor.idCard,
  //         参观者联系电话: visitor.phone
  //       }
  //     })
  //     return list
  //   })
  //   console.log(listData.flat(), 'listData')
  //   exportExcelFile(listData.flat(), [150, 50, 100, 100, 100, 140, 50, 140, 130], '展馆预约记录')
  // }, [tableInfo.list])

  // 导出excel后端实现
  const exportExcelFu2 = useCallback(async () => {
    const res: any = await B_APIexportExcel(1)
    const downloadUrl = window.URL.createObjectURL(res)
    const link = document.createElement('a')
    link.href = downloadUrl
    link.download = '展馆预约表.xlsx'
    document.body.appendChild(link)
    link.click()
  }, [])

  const tableLastBtn = useMemo(() => {
    return [
      {
        title: '操作',
        render: (item: B1tableType) => (
          <>
            <Button
              size='small'
              type='text'
              onClick={() => setEditInfo({ id: item.id, txt: '查看' })}
            >
              查看
            </Button>
            <MyPopconfirm txtK='删除' onConfirm={() => delTableFu(item.id)} />
          </>
        )
      }
    ]
  }, [delTableFu])

  //查看
  const [editInfo, setEditInfo] = useState<B1EditInfoType>({
    id: 0,
    txt: ''
  })

  return (
    <div className={styles.B1reserve1}>
      <div className='pageTitle'>展馆预约记录</div>

      {/* 顶部筛选 */}
      <div className='B1top'>
        <Button type='primary' onClick={exportExcelFu2}>
          导出
        </Button>
      </div>

      {/* 表格主体 */}
      <div className='B1tableBox'>
        <MyTable
          myKey='id'
          yHeight={625}
          list={tableInfo.list}
          columnsTemp={B1tableC}
          lastBtn={tableLastBtn}
          pageNum={pageData.pageNum}
          pageSize={pageData.pageSize}
          total={tableInfo.total}
          onChange={(pageNum, pageSize) => setPageData({ ...pageData, pageNum, pageSize })}
        />
      </div>

      {/* 查看 */}
      {editInfo.id ? (
        <B1look
          editInfo={editInfo}
          closeFu={() => setEditInfo({ id: 0, txt: '导出' })}
          addTableFu={resetSelectFu}
          editTableFu={getListFu}
        />
      ) : null}
    </div>
  )
}

const MemoB1reserve1 = React.memo(B1reserve1)

export default MemoB1reserve1
