import React, {
  useCallback,
  useEffect,
  useMemo,
  useRef,
  useState,
} from "react";
import styles from "./index.module.scss";
import { useDispatch, useSelector } from "react-redux";
import { A5_APIdel, A5_APIgetList } from "@/store/action/A5show";
import { A5tableType } from "@/types";
import { Button, Input, Select } from "antd";
import MyPopconfirm from "@/components/MyPopconfirm";
import { MessageFu } from "@/utils/message";
import { RootState } from "@/store";
import MyTable from "@/components/MyTable";
import { A5tableC } from "@/utils/tableData";
import A5edit from "./A5edit";
function A5show() {
  const dispatch = useDispatch();

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

  const getListFu = useCallback(() => {
    dispatch(A5_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: "",
      type: "",
    });
  }, []);

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

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

  // 新增和编辑
  const [editId, setEditId] = useState(0);

  const tableLastBtn = useMemo(() => {
    return [
      {
        title: "操作",
        render: (item: A5tableType) => (
          <>
            <Button size="small" type="text" onClick={() => setEditId(item.id)}>
              编辑
            </Button>
            <MyPopconfirm txtK="删除" onConfirm={() => delTableFu(item.id)} />
          </>
        ),
      },
    ];
  }, [delTableFu]);
  return (
    <div className={styles.A5show}>
      <div className="pageTitle">
        宁博展览{editId > 0 ? " - 编辑" : editId < 0 ? " - 新增" : ""}
      </div>

      {/* 顶部筛选 */}
      <div className="A5top">
        <div className="A5topLeft">
          <div className="A5topRow">
            <span>标题：</span>
            <Input
              key={inputKey}
              maxLength={10}
              showCount
              style={{ width: 300 }}
              placeholder="请输入"
              allowClear
              onChange={(e) => fromKeyChangeFu(e, "searchKey")}
            />
          </div>
          <div className="A5topRow">
            <span>展览类型：</span>
            <Select
              placeholder="请选择"
              style={{ width: 200 }}
              value={fromData.type}
              onChange={(e) =>
                setFromData({ ...fromData, pageNum: 1, type: e })
              }
              options={[
                { value: "", label: "全部" },
                { value: "常设展览", label: "常设展览" },
                { value: "临时展览", label: "临时展览" },
              ]}
            />
          </div>
        </div>
        <div>
          <Button onClick={resetSelectFu}>重置</Button>&emsp;
          <Button type="primary" onClick={() => setEditId(-1)}>
            新增
          </Button>
        </div>
      </div>

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

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

const MemoA5show = React.memo(A5show);

export default MemoA5show;
