import React, {
  useCallback,
  useEffect,
  useMemo,
  useRef,
  useState,
} from "react";
import styles from "./index.module.scss";
import { useDispatch, useSelector } from "react-redux";
import { MessageFu } from "@/utils/message";
import { Button, Input } from "antd";
import MyPopconfirm from "@/components/MyPopconfirm";
import {
  A3_APIdel,
  A3_APIgetCon,
  A3_APIgetList,
  A3_APIsetCon,
} from "@/store/action/A3culture";
import { A3tableType } from "@/types";
import { RootState } from "@/store";
import MyTable from "@/components/MyTable";
import { A3tableC } from "@/utils/tableData";
import A3edit from "./A3edit";
function A3culture() {
  const dispatch = useDispatch();

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

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

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

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

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

  // 离线状态
  const [state, setState] = useState(-1);

  const getStateFu = useCallback(async () => {
    const res = await A3_APIgetCon();
    if (res.code === 0) {
      setState(res.data);
    }
  }, []);

  const setStateFu = useCallback(async () => {
    const res = await A3_APIsetCon(state === 1 ? 0 : 1);
    if (res.code === 0) {
      MessageFu.success("设置商城状态成功!");
      getStateFu();
    }
  }, [getStateFu, state]);

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

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

      {/* 顶部筛选 */}
      <div className="A3top">
        <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={setStateFu}>
            商城状态({state === 1 ? "开" : state === 0 ? "关" : " - "})
          </Button>
          &emsp;
          <Button type="primary" onClick={() => setEditId(-1)}>
            新增
          </Button>
        </div>
      </div>

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

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

const MemoA3culture = React.memo(A3culture);

export default MemoA3culture;
