import React, {
  useCallback,
  useEffect,
  useMemo,
  useRef,
  useState,
} from "react";
import styles from "./index.module.scss";
import { Button, Input } from "antd";
import { useDispatch, useSelector } from "react-redux";
import { A4_APIdel, A4_APIgetList } from "@/store/action/A4exhibit";
import { RootState } from "@/store";
import { A4tableType } from "@/types";
import { MessageFu } from "@/utils/message";
import A4add from "./A4add";
import MyTable from "@/components/MyTable";
import { A4tableC } from "@/utils/tableData";
import MyPopconfirm from "@/components/MyPopconfirm";

function A4exhibit() {
  const dispatch = useDispatch();

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

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

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

  const [inputKey, setInputKey] = useState(1);

  // 标题的输入
  const timeRef = useRef(-1);
  const fromKeyChangeFu = useCallback(
    (e: React.ChangeEvent<HTMLInputElement>, key: "searchKey") => {
      clearTimeout(timeRef.current);
      timeRef.current = window.setTimeout(() => {
        setFromData({ ...fromData, [key]: e.target.value, pageNum: 1 });
      }, 500);
    },
    [fromData]
  );

  // 点击重置
  const resetSelectFu = useCallback(() => {
    setInputKey(Date.now());
    setFromData({
      pageNum: 1,
      pageSize: 10,
      searchKey: "",
    });
  }, []);

  const tableInfo = useSelector(
    (state: RootState) => state.A4exhibit.tableInfo
  );

  // 点击删除
  const delTableFu = useCallback(
    async (id: number) => {
      const res = await A4_APIdel(id);
      if (res.code === 0) {
        MessageFu.success("删除成功!");
        getListFu();
      }
    },
    [getListFu]
  );

  const tableLastBtn = useMemo(() => {
    return [
      {
        title: "操作",
        render: (item: A4tableType) => (
          <>
            <Button size="small" type="text" onClick={() => setAddId(item.id)}>
              编辑
            </Button>

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

  // 点击新增和编辑
  const [addId, setAddId] = useState(0);

  return (
    <div className={styles.A4exhibit}>
      <div className="pageTitle">
        云展管理 {addId ? (addId > 0 ? " - 编辑" : " - 新增") : ""}
      </div>

      {/* 顶部筛选 */}
      <div className="A4top">
        <div>
          <span>搜索项：</span>
          <Input
            key={inputKey}
            maxLength={10}
            showCount
            style={{ width: 300 }}
            placeholder="请输入名称"
            allowClear
            onChange={(e) => fromKeyChangeFu(e, "searchKey")}
          />
        </div>
        <div>
          <Button onClick={resetSelectFu}>重置</Button>
          &emsp;&emsp;
          <Button type="primary" onClick={() => setAddId(-1)}>
            新增
          </Button>
        </div>
      </div>

      {/* 表格主体 */}
      <div className="A4tableBox">
        <MyTable
          yHeight={625}
          list={tableInfo.list}
          columnsTemp={A4tableC}
          lastBtn={tableLastBtn}
          pageNum={fromData.pageNum}
          pageSize={fromData.pageSize}
          total={tableInfo.total}
          onChange={(pageNum, pageSize) =>
            setFromData({ ...fromData, pageNum, pageSize })
          }
        />
      </div>

      {/* 新增和编辑 */}
      {addId ? (
        <A4add
          addId={addId}
          closeFu={() => setAddId(0)}
          addTableFu={() => resetSelectFu()}
          editTableFu={() => getListFu()}
        />
      ) : null}
    </div>
  );
}

const MemoA4exhibit = React.memo(A4exhibit);

export default MemoA4exhibit;
