import React, {
  useCallback,
  useEffect,
  useMemo,
  useRef,
  useState,
} from "react";
import styles from "./index.module.scss";
import { useDispatch, useSelector } from "react-redux";
import { A7_APIdel, A7_APIgetList } from "@/store/action/A7volunteer";
import { RootState } from "@/store";
import { MessageFu } from "@/utils/message";
import { A7tableType } from "@/types";
import { Button, Input } from "antd";
import MyPopconfirm from "@/components/MyPopconfirm";
import MyTable from "@/components/MyTable";
import { A7tableC } from "@/utils/tableData";
import A7edit from "./A7edit";

const topButArr = ["志愿者资讯", "志愿者风采"];

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

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

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

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

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

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

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

  return (
    <div className={styles.A7volunteer}>
      <div className="pageTitle">
        志愿者之家{editId > 0 ? " - 编辑" : editId < 0 ? " - 新增" : ""}
      </div>

      {/* 顶部筛选 */}
      <div className="A7top">
        <div>
          {topButArr.map((v) => (
            <Button
              onClick={() => setFromData({ ...fromData, type: v, pageNum: 1 })}
              key={v}
              type={fromData.type === v ? "primary" : "default"}
            >
              {v}
            </Button>
          ))}
          &emsp;&emsp;
          <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="A7tableBox">
        <MyTable
          yHeight={625}
          list={tableInfo.list}
          columnsTemp={A7tableC}
          lastBtn={tableLastBtn}
          pageNum={fromData.pageNum}
          pageSize={fromData.pageSize}
          total={tableInfo.total}
          onChange={(pageNum, pageSize) =>
            setFromData({ ...fromData, pageNum, pageSize })
          }
        />
      </div>

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

const MemoA7volunteer = React.memo(A7volunteer);

export default MemoA7volunteer;
