import { Modal, Spin, Table, Tree } from "antd";
import React, { useState } from "react";
import { GetAllEnumsByName } from "@/api";
import { ModalProps } from "./props";
import { useRequest } from "umi";

/**
 * 岗位级别弹窗
 * @returns
 */
export default function LevelModal(props: ModalProps) {
  const [open, setOpen] = useState(props.defaultOpen ?? false);
  const [selectedRows, setSelectedRows] = useState<any[]>([]);

  const { data, loading } = useRequest(GetAllEnumsByName, {
    defaultParams: [
      {
        filters: [
          { name: "EnterpriseId" },
          { name: "Enum", value: "JobLevel" },
        ],
      },
    ],
  });

  const handleCancel = () => {
    props.onClose?.();
    setOpen(false);
  };

  const handleOk = () => {
    props.onChange?.(selectedRows);
    setOpen(false);
  };

  const handleSelect = (_keys: React.Key[], rows: any[]) => {
    setSelectedRows(rows);
  };

  return (
    <Modal
      title="请选择岗位级别"
      width={440}
      open={open}
      onCancel={handleCancel}
      onOk={handleOk}
    >
      <Table
        loading={loading}
        rowSelection={{
          type: "radio",
          selectedRowKeys: selectedRows.map((item) => item?.id),
          onChange: handleSelect,
        }}
        rowKey={"id"}
        columns={[
          { title: "名称", dataIndex: "name" },
          { title: "值", dataIndex: "value" },
        ]}
        dataSource={data?.result || []}
      />
    </Modal>
  );
}
