import React, { useCallback, useEffect, useRef, useState } from "react";
import styles from "./index.module.scss";
import { useDispatch, useSelector } from "react-redux";
import { A3_APIgetList } from "@/store/action/A3record";
import { RootState } from "@/store";
import { Button, DatePicker, Input, Select } from "antd";
import MyTable from "@/components/MyTable";
import { A3tableC } from "@/utils/tableData";
import { A3select } from "./data";
const { RangePicker } = DatePicker;

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

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

  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 timeChange = useCallback(
    (date: any, dateString: any) => {
      let startTime = "";
      let endTime = "";
      if (dateString[0] && dateString[1]) {
        startTime = dateString[0] + " 00:00:00";
        endTime = dateString[1] + " 23:59:59";
      }
      setFromData({ ...fromData, startTime, endTime, pageNum: 1 });
    },
    [fromData]
  );

  // 点击重置
  const resetSelectFu = useCallback(() => {
    setInputKey(Date.now());
    setFromData({
      pageNum: 1,
      pageSize: 10,
      searchKey: "",
      startTime: "",
      endTime: "",
      type: "",
    });
  }, []);

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

  return (
    <div className={styles.A3record}>
      <div className="pageTitle">积分记录</div>

      {/* 顶部筛选 */}
      <div className="A3top">
        <div>
          <div className="A3row">
            <span>日期：</span>
            <RangePicker onChange={timeChange} key={inputKey} />
          </div>
          <div className="A3row">
            <span>用户名：</span>
            <Input
              key={inputKey}
              maxLength={10}
              showCount
              style={{ width: 240 }}
              placeholder="请输入"
              allowClear
              onChange={(e) => fromKeyChangeFu(e, "searchKey")}
            />
          </div>
          <div className="A3row">
            <span>类型：</span>
            <Select
              style={{ width: 120 }}
              value={fromData.type}
              onChange={(e) =>
                setFromData({ ...fromData, type: e, pageNum: 1 })
              }
              options={A3select}
            />
          </div>
        </div>
        <div>
          <Button onClick={resetSelectFu}>重置</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>
    </div>
  );
}

const MemoA3record = React.memo(A3record);

export default MemoA3record;
