import React, {
  useCallback,
  useEffect,
  useMemo,
  useRef,
  useState,
} from "react";
import styles from "./index.module.scss";
import { useDispatch, useSelector } from "react-redux";
import { A2_APIdel, A2_APIgetList } from "@/store/action/A2dynamic";
import { RootState } from "@/store";
import { A2tableType } from "@/types";
import { Button, Input } from "antd";
import MyTable from "@/components/MyTable";
import { A2tableC } from "@/utils/tableData";
import MyPopconfirm from "@/components/MyPopconfirm";
import { MessageFu } from "@/utils/message";
import A2edit from "./A2edit";
function A2dynamic() {
  const dispatch = useDispatch();

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

  const getListFu = useCallback(() => {
    dispatch(A2_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.A2dynamic.tableInfo
  );

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

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

  const tableLastBtn = useMemo(() => {
    return [
      {
        title: "操作",
        render: (item: A2tableType) => (
          <>
            <Button size="small" type="text" onClick={() => setEditId(item.id)}>
              编辑
            </Button>
            <MyPopconfirm txtK="删除" onConfirm={() => delTableFu(item.id)} />
          </>
        ),
      },
    ];
  }, [delTableFu]);

  return (
    <div className={styles.A2dynamic}>
      <div className="pageTitle">
        宁博动态{editId > 0 ? " - 编辑" : editId < 0 ? " - 新增" : ""}
      </div>

      {/* 顶部筛选 */}
      <div className="A2top">
        <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;
          <Button type="primary" onClick={() => setEditId(-1)}>
            新增
          </Button>
        </div>
      </div>

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

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

const MemoA2dynamic = React.memo(A2dynamic);

export default MemoA2dynamic;
