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

/**
 * 部门下拉树组件（港务局版本）
 */
function DepartmentSelectTree(props) {
  const {
    params = {},
    placeholder = "部门",
    isNeedCorpInfoId = false,
    isNeedParentId = false,
    searchType = "current",
    ...restProps
  } = props;

  const [treeData, setTreeData] = useState([]);

  const getData = async () => {
    setTreeData([]);

    if (searchType === "current") {
      // 根据参数决定是否发送请求
      if (isNeedCorpInfoId && !params.eqCorpinfoId)
        return;
      if (isNeedParentId && !params.eqParentId)
        return;
    }

    const { data } = await request(searchType === "current" ? "/basicInfo/department/listTree" : "/basicInfo/department/listAllTree", "post", params);
    setTreeData(data);
  };

  useEffect(() => {
    getData();
  }, [JSON.stringify(params), isNeedCorpInfoId, isNeedParentId, searchType]);

  return (
    <BasicSelectTree treeData={treeData} placeholder={placeholder} childrenKey="childrenList" {...restProps} />
  );
}

DepartmentSelectTree.displayName = "DepartmentSelectTree";

export default DepartmentSelectTree;
