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

// const optionsArr = [
//   { value: "", label: "全部" },
//   { value: "三维文物", label: "三维文物" },
//   { value: "平面文物", label: "平面文物" },
//   { value: "油画", label: "油画" },
//   { value: "电子书", label: "电子书" },
// ];

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

  const [fromData, setFromData] = useState({
    pageNum: 1,
    pageSize: 10,
    searchKey: "",
    dictType: "",
    dictLevel: "",
    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: "",
      dictType: "",
      dictLevel: "",
      type: "",
    });
  }, []);

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

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

  const tableLastBtn = useMemo(() => {
    return [
      {
        title: "操作",
        render: (item: A5tableType) => (
          <>
            <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.A5goods}>
      <div className="pageTitle">
        馆藏管理 {addId ? (addId > 0 ? " - 编辑" : " - 新增") : ""}
      </div>
      {/* 顶部筛选 */}
      <div className="A5top">
        <div className="A5topRow">
          <div>
            <span>搜索项：</span>
            <Input
              key={inputKey}
              maxLength={10}
              showCount
              style={{ width: 300 }}
              placeholder="请输入名称/分类/时代"
              allowClear
              onChange={(e) => fromKeyChangeFu(e, "searchKey")}
            />
          </div>

          <div>
            <span>级别：</span>
            <Select
              placeholder="请选择"
              style={{ width: 150 }}
              value={fromData.dictLevel}
              onChange={(e) =>
                setFromData({ ...fromData, dictLevel: e, pageNum: 1 })
              }
              options={[
                { value: "", label: "全部" },
                { value: "一级", label: "一级" },
                { value: "二级", label: "二级" },
                { value: "三级", label: "三级" },
              ]}
            />
          </div>

          <div>
            <span>类别：</span>
            <Select
              placeholder="请选择"
              style={{ width: 150 }}
              value={fromData.type}
              onChange={(e) =>
                setFromData({ ...fromData, type: e, pageNum: 1 })
              }
              options={[
                { value: "", label: "全部" },
                { value: "3D", label: "三维文物" },
                { value: "2D", label: "二维文物" },
              ]}
            />
          </div>

          {/* <div>
            <span>类别：</span>
            <Select
              placeholder="请选择"
              style={{ width: 160 }}
              value={fromData.dictType}
              onChange={(e) =>
                setFromData({ ...fromData, dictType: e, pageNum: 1 })
              }
              options={optionsArr}
            />
          </div> */}
        </div>
        <div className="A5topRow">
          <Button onClick={resetSelectFu}>重置</Button>
          &emsp;&emsp;
          <Button type="primary" onClick={() => setAddId(-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>
      {/* 新增和编辑 */}
      {addId ? (
        <A5add
          addId={addId}
          closeFu={() => setAddId(0)}
          addTableFu={() => resetSelectFu()}
          editTableFu={() => getListFu()}
        />
      ) : null}
    </div>
  );
}

const MemoA5goods = React.memo(A5goods);

export default MemoA5goods;
