import { request } from "@cqsjjb/jjb-common-lib/http";
import { useEffect, useState } from "react";
import BasicSelect from "../../Basic";

/**
 * 人员下拉组件（港务局版本）
 */
function PersonnelSelect(props) {
  const {
    params = {},
    placeholder = "人员",
    isNeedCorpInfoId = false,
    isNeedDepartmentId = true,
    isNeedPostId = false,
    noMain = "",
    ...restProps
  } = props;

  const [data, setData] = useState([]);

  const getData = async () => {
    setData([]);
    // 根据参数决定是否发送请求
    if (isNeedCorpInfoId && !params.corpinfoId)
      return;
    if (isNeedDepartmentId && !params.departmentId)
      return;
    if (isNeedPostId && !params.postId)
      return;

    const { data } = await request("/basicInfo/user/listAll", "get", { ...params, noMain });
    setData(data);
  };

  useEffect(() => {
    getData();
  }, [JSON.stringify(params), isNeedCorpInfoId, isNeedDepartmentId, isNeedPostId, noMain]);

  return (
    <BasicSelect data={data} placeholder={placeholder} {...restProps} />
  );
}

PersonnelSelect.displayName = "PersonnelSelect";

export default PersonnelSelect;
