import classNames from "classnames";
import { useCallback, useEffect, useMemo, useState } from "react";
import { Button, Form, Input, Select, Table } from "antd";
import { PageContainer } from "@/components";
import style from "./index.module.scss";
import { debounce } from "lodash";
import { getManageFileListAPi } from "@/api";

const DEFAULT_PARAMS = {
  pageNum: 1,
  pageSize: 20,
  searchKey: "",
};

const ManagementReportPage = () => {
  const [params, setParams] = useState({
    ...DEFAULT_PARAMS,
  });
  const [loading, setLoading] = useState(false);
  const [total, setTotal] = useState(0);
  const [list, setList] = useState<[]>([]);

  const debounceSearch = useMemo(
    () =>
      debounce((changedVal: unknown, vals: any) => {
        setParams({ ...params, ...vals });
      }, 500),
    [params]
  );

  const paginationChange = useCallback(
    () => (pageNum: number, pageSize: number) => {
      setParams({ ...params, pageNum, pageSize });
    },
    [params]
  );

  const getList = useCallback(async () => {
    setLoading(true);
    try {
      const data = await getManageFileListAPi(params);
      setList(data.records);
      setTotal(data.total);
    } finally {
      setLoading(false);
    }
  }, [params]);

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

  return (
    <PageContainer title="附件管理">
      <div className={style.filter}>
        <Form layout="inline" className="inline-form">
          <Form.Item label="搜索">
            <Input placeholder="请输入考核名称" className="w160" />
          </Form.Item>
          <Form.Item label="归档状态">
            <div className="w160">
              <Select />
            </div>
          </Form.Item>
          <Form.Item>
            <Button type="primary" onClick={getList}>
              查询
            </Button>
          </Form.Item>
        </Form>
      </div>

      <div className={style.table}>
        <Table
          loading={loading}
          className={classNames("cus-table large")}
          dataSource={list}
          rowKey="id"
          scroll={{ x: "max-content" }}
          columns={[
            {
              title: "资料名称",
              dataIndex: "name",
              key: "name",
              align: "center",
              minWidth: 100,
            },
            {
              title: "关联考核",
              dataIndex: "a",
              key: "a",
              align: "center",
              minWidth: 100,
            },
            {
              title: "考核周期",
              dataIndex: "description",
              key: "description",
              align: "center",
              minWidth: 100,
            },
            {
              title: "关联指标",
              dataIndex: "b",
              key: "b",
              align: "center",
              minWidth: 100,
            },
            {
              title: "附件名称",
              dataIndex: "b",
              key: "b",
              align: "center",
              minWidth: 100,
            },
            {
              title: "责任部门",
              dataIndex: "b",
              key: "b",
              align: "center",
              minWidth: 100,
            },
            {
              title: "上传用户",
              dataIndex: "b",
              key: "b",
              align: "center",
              minWidth: 100,
            },
            {
              title: "归档状态",
              dataIndex: "b",
              key: "b",
              align: "center",
              minWidth: 100,
            },
            {
              title: "操作",
              key: "h",
              align: "center",
              fixed: "right",
              render: (item: any) => {
                return (
                  <>
                    <Button type="link">下载</Button>
                    <Button type="text" size="small">
                      归档
                    </Button>
                  </>
                );
              },
            },
          ]}
          pagination={{
            showQuickJumper: true,
            position: ["bottomCenter"],
            showSizeChanger: true,
            current: params.pageNum,
            pageSize: params.pageSize,
            total,
            onChange: paginationChange(),
          }}
        />
      </div>
    </PageContainer>
  );
};

export default ManagementReportPage;
