import React, { useCallback, useEffect, useMemo, useState } from 'react'
import styles from './index.module.scss'
import { useDispatch, useSelector } from 'react-redux'
import { B2_APIdel, B2_APIgetList } from '@/store/action/B2exhiLog'
import { RootState } from '@/store'
import { MessageFu } from '@/utils/message'
import { B2tableType } from './type'
import { Button } from 'antd'
import MyPopconfirm from '@/components/MyPopconfirm'
import MyTable from '@/components/MyTable'
import { B2tableC } from '@/utils/tableData'
import B2look from './B2look'
function B2exhiLog() {
  const dispatch = useDispatch()

  const [fromData, setFromData] = useState({ pageNum: 1, pageSize: 10 })

  const getListFu = useCallback(() => {
    dispatch(B2_APIgetList(fromData))
  }, [dispatch, fromData])

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

  const { tableInfo } = useSelector((state: RootState) => state.B2exhiLog)

  const delTableFu = useCallback(
    async (id: number) => {
      const res = await B2_APIdel(id)
      if (res.code === 0) {
        MessageFu.success('删除成功!')
        getListFu()
      }
    },
    [getListFu]
  )

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

            <MyPopconfirm txtK='删除' onConfirm={() => delTableFu(item.id)} />
          </>
        )
      }
    ]
  }, [delTableFu])

  // 点击查看
  const [lookId, setLookId] = useState(0)

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

      <MyTable
        yHeight={700}
        list={tableInfo.list}
        columnsTemp={B2tableC}
        lastBtn={tableLastBtn}
        pageNum={fromData.pageNum}
        pageSize={fromData.pageSize}
        total={tableInfo.total}
        onChange={(pageNum, pageSize) => setFromData({ ...fromData, pageNum, pageSize })}
      />

      {/* 点击查看 */}
      {lookId ? <B2look sId={lookId} closeFu={() => setLookId(0)} /> : null}
    </div>
  )
}

const MemoB2exhiLog = React.memo(B2exhiLog)

export default MemoB2exhiLog
