import React, { useCallback, useEffect, useMemo, useState } from 'react'
import styles from './index.module.scss'
import { Button, Input } from 'antd'
import { useDispatch, useSelector } from 'react-redux'
import { A1EditInfoType } from './data'
import { A5_APIdel, A5_APIgetList, A5_APIpublish } from '@/store/action/A5activity'
import { RootState } from '@/store'
import { MessageFu } from '@/utils/message'
import { A5tableType } from '@/types/api/A5activity'
import MyPopconfirm from '@/components/MyPopconfirm'
import MyTable from '@/components/MyTable'
import { A4tableC } from '@/utils/tableData'
import A5add from './A5add'
import { handleCopyClick } from '@/utils/copyTxt'
import { editPreview } from '@/store/action/layout'

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

function A5activity() {
  const dispatch = useDispatch()

  const [pageData, setPageData] = useState(pageDataBase)
  const [title, setTitle] = useState('')
  const getListFu = useCallback(
    (title?: string) => {
      // status: -1 全部 0 未发布 1 已发布
      dispatch(A5_APIgetList({ ...pageData, status: -1, title }))
    },
    [dispatch, pageData]
  )

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

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

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

  const delTableFu = useCallback(
    async (id: number) => {
      const res = await A5_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 publishFu = useCallback(
    async (id: number) => {
      const res = await A5_APIpublish(id)
      if (res.code === 0) {
        MessageFu.success('发布成功!')
        getListFu()
      }
    },
    [getListFu]
  )

  const tableLastBtn = useMemo(() => {
    return [
      {
        title: '操作',
        render: (item: A5tableType) => (
          <>
            <Button
              size='small'
              type='text'
              onClick={() =>
                handleCopyClick(
                  item.status === 1
                    ? `https://sit-kelamayi.4dage.com/mini/#/allDetailsShow?id=${item.activityId}&type=activity&isFrom=weixin`
                    : `https://sit-kelamayi.4dage.com/mini/#/allDetailsShow?id=${item.activityId}&type=activity&preview=1&isFromPage=indexPage/activity`
                )
              }
            >
              复制地址
            </Button>
            {item.status === 0 && (
              <Button
                size='small'
                type='text'
                onClick={() =>
                  dispatch(
                    editPreview({
                      isOpenPreview: true,
                      src: `/allDetailsShow?id=${item.activityId}&type=activity&preview=1&isFromPage=indexPage/activity`
                    })
                  )
                }
              >
                预览
              </Button>
            )}
            <Button
              size='small'
              type='text'
              onClick={() => setEditInfo({ id: item.activityId, txt: '编辑' })}
            >
              编辑
            </Button>
            {item.status === 0 && (
              <Button size='small' type='text' onClick={() => publishFu(item.activityId)}>
                发布
              </Button>
            )}
            <MyPopconfirm txtK='删除' onConfirm={() => delTableFu(item.activityId)} />
          </>
        )
      }
    ]
  }, [delTableFu, dispatch, publishFu])

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

  return (
    <div className={styles.A5activity}>
      <div className='pageTitle'>克博活动 {editInfo.id ? ` - ${editInfo.txt}` : ''}</div>

      {/* 顶部筛选 */}
      <div className='A5top'>
        <div className='A5topLeft'>
          <Input placeholder='请输入标题' onChange={e => setTitle(e.target.value)} value={title} />
          <Button type='primary' onClick={() => getListFu(title)}>
            搜索
          </Button>
          <Button
            type='primary'
            onClick={() => {
              resetSelectFu()
              setTitle('')
            }}
          >
            重置
          </Button>
        </div>
        <Button type='primary' onClick={() => setEditInfo({ id: -1, txt: '新增' })}>
          新增
        </Button>
      </div>

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

      {/* 新增 / 编辑 */}
      {editInfo.id ? (
        <A5add
          editInfo={editInfo}
          closeFu={() => setEditInfo({ id: 0, txt: '新增' })}
          addTableFu={resetSelectFu}
          editTableFu={getListFu}
        />
      ) : null}
    </div>
  )
}

const MemoA5activity = React.memo(A5activity)

export default MemoA5activity
