import React, { useCallback, useEffect, useMemo, useState } from 'react'
import styles from './index.module.scss'
import { useDispatch, useSelector } from 'react-redux'
import { A6_APIdel, A6_APIgetList } from '@/store/action/A6record'
import { RootState } from '@/store'
import { MessageFu } from '@/utils/message'
import { A6tableType } from './type'
import { Button } from 'antd'
import MyPopconfirm from '@/components/MyPopconfirm'
import MyTable from '@/components/MyTable'
import { A6tableC } from '@/utils/tableData'
import A6auth from './A6auth'
function A6record() {
  const dispatch = useDispatch()

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

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

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

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

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

  const tableLastBtn = useMemo(() => {
    return [
      {
        title: '操作',
        render: (item: A6tableType) => (
          <>
            {item.status === 0 ? (
              <Button
                size='small'
                type='text'
                onClick={() =>
                  setAuthId({
                    id: item.id,
                    status: item.status
                  })
                }
              >
                审核
              </Button>
            ) : null}

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

  // 点击审核
  const [authId, setAuthId] = useState({
    id: 0,
    status: 0
  })

  return (
    <div className={styles.A6record}>
      <div className='pageTitle'>课程预约记录</div>

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

      {/* 点击审核 */}
      {authId.id ? (
        <A6auth
          sId={authId.id}
          status={authId.status}
          closeFu={() => setAuthId({ id: 0, status: 0 })}
          editTableFu={getListFu}
        />
      ) : null}
    </div>
  )
}

const MemoA6record = React.memo(A6record)

export default MemoA6record
