import classNames from "classnames";
import { useCallback, useEffect, useMemo, useState } from "react";
import { Button, Form, Input, Select, Table } from "antd";
import { useNavigate } from "react-router-dom";
import { PageContainer } from "@/components";
import {
  ASSESSMENT_TYPE_OPTIONS,
  PUBLISH_STATUS_MAP,
  PUBLISH_STATUS_OPTIONS,
} from "../../../constants";
import style from "../Form/index.module.scss";
import { debounce } from "lodash";
import { getManageEvaluationListApi } from "@/api";
import { IManageFormItem, IManageFormListParams } from "@/types";

const DEFAULT_PARAMS: IManageFormListParams = {
  deptStatus: undefined,
  searchKey: "",
  status: undefined,
  type: undefined,
  pageNum: 1,
  pageSize: 20,
};

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

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

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

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

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

  return (
    <PageContainer title="考核评定">
      <div className={style.filter}>
        <Form
          layout="inline"
          className="inline-form"
          onValuesChange={debounceSearch}
        >
          <Form.Item label="搜索" name="searchKey">
            <Input placeholder="请输入考核名称" className="w160" />
          </Form.Item>
          <Form.Item label="考核类别">
            <div className="w160">
              <Form.Item noStyle name="type">
                <Select
                  allowClear
                  placeholder="请选择"
                  options={ASSESSMENT_TYPE_OPTIONS}
                />
              </Form.Item>
            </div>
          </Form.Item>
          <Form.Item label="发布状态">
            <div className="w160">
              <Form.Item noStyle name="status">
                <Select
                  allowClear
                  placeholder="请选择"
                  options={PUBLISH_STATUS_OPTIONS}
                />
              </Form.Item>
            </div>
          </Form.Item>
          <Form.Item>
            <Button type="primary" onClick={getList}>
              查询
            </Button>
          </Form.Item>
        </Form>
      </div>

      <div className={style.table}>
        <Table
          className={classNames("cus-table large")}
          dataSource={list}
          rowKey="id"
          scroll={{ x: "max-content" }}
          columns={[
            {
              title: "名称",
              dataIndex: "name",
              align: "center",
              minWidth: 100,
            },
            {
              title: "类别",
              align: "center",
              minWidth: 100,
              render: (item: IManageFormItem) => {
                return ASSESSMENT_TYPE_OPTIONS.find(
                  (i) => i.value === item.type
                )?.label;
              },
            },
            {
              title: "说明",
              dataIndex: "remark",
              align: "center",
              minWidth: 100,
            },
            {
              title: "考核周期",
              align: "center",
              minWidth: 100,
              render: (item: IManageFormItem) =>
                `${item.dateStart}-${item.dateEnd}`,
            },
            {
              title: "发布状态",
              key: "c",
              align: "center",
              minWidth: 100,
              render: (item: IManageFormItem) => (
                <p
                  style={{
                    color: PUBLISH_STATUS_MAP[item.publishStatus].color,
                  }}
                >
                  {PUBLISH_STATUS_MAP[item.publishStatus].label}
                </p>
              ),
            },
            {
              title: "操作",
              key: "h",
              align: "center",
              fixed: "right",
              render: (item: IManageFormItem) => {
                return (
                  <Button
                    type="link"
                    onClick={() =>
                      navigate(`/management/evaluation/detail/${item.id}`)
                    }
                  >
                    查看
                  </Button>
                );
              },
            },
          ]}
          pagination={{
            showQuickJumper: true,
            position: ["bottomCenter"],
            showSizeChanger: true,
            current: params.pageNum,
            pageSize: params.pageSize,
            total,
            onChange: paginationChange(),
          }}
        />
      </div>
    </PageContainer>
  );
};

export default ManagementEvaluationPage;
