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 { D1EditInfoType } from './data'
import { D1_APIgetList, D1_APIdel, D1_APIexportExcel } from '@/store/action/D1feedback'
import { RootState } from '@/store'
import { MessageFu } from '@/utils/message'
import { D1tableType } from '@/types'
import MyPopconfirm from '@/components/MyPopconfirm'
import MyTable from '@/components/MyTable'
import { D1tableC } from '@/utils/tableData'
import D1look from './D1look'
// import { exportExcelFile } from '@/utils/xlsxExport'

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

function D1feedback() {
  const dispatch = useDispatch()

  const [pageData, setPageData] = useState(pageDataBase)

  const getListFu = useCallback(() => {
    dispatch(D1_APIgetList({ ...pageData }))
  }, [dispatch, pageData])

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

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

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

  const delTableFu = useCallback(
    async (id: number) => {
      const res = await D1_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]
  )

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

  // 导出excel前端实现
  // const exportExcelFu = useCallback(() => {
  //   const listData = tableInfo.list.map(item => {
  //     return {
  //       提交时间: item.createTime,
  //       姓名: item.name,
  //       邮箱: item.email,
  //       内容: item.content
  //     }
  //   })
  //   console.log(listData.flat(), 'listData')
  //   exportExcelFile(listData.flat(), [150, 60, 150, 200], '意见反馈记录')
  // }, [tableInfo.list])

  // 导出excel后端实现
  const exportExcelFu2 = useCallback(async () => {
    const res: any = await D1_APIexportExcel()
    const downloadUrl = window.URL.createObjectURL(res)
    const link = document.createElement('a')
    link.href = downloadUrl
    link.download = '意见反馈.xlsx'
    document.body.appendChild(link)
    link.click()
  }, [])

  //新增、编辑
  const [editInfo, setEditInfo] = useState<D1EditInfoType>({
    id: 0,
    txt: ''
  })

  return (
    <div className={styles.D1feedback}>
      <div className='pageTitle'>意见反馈记录</div>

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

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

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

const MemoD1feedback = React.memo(D1feedback)

export default MemoD1feedback
