/**
 * 获取部门信息
 */
import { ref } from 'vue';
import { formatDeptTree } from '@/views/disaster/utils/formatDeptTree';
import type { DeptTreeItem } from '@/types/dept/type';
import type { SysDictDataDetail } from '@/api/dict';
import { getAllDepartments } from '@/api/auth/dept';
import { queryDictTypeDetail } from '@/api/dict';
import { DICT_CODE } from '@/constant/dict';

export const useDeptInfoHook = () => {
  const firstLevelDepts = ref<DeptTreeItem[]>([]); // 第一部门的列表
  const disasterLevelDepts = ref<SysDictDataDetail[]>([]); // 获取灾害防范定制的一级部门
  const originalDisasterLevelDepts = ref<SysDictDataDetail[]>([]); // 获取灾害防范定制的一级部门
  const isFilterData = ref<boolean>(false);
  const getFirstLevelDepts = async () => {
    const res = await getAllDepartments();
    firstLevelDepts.value = formatDeptTree(res);
  };
  const getDisasterLevelDepts = async () => {
    const res = await queryDictTypeDetail(DICT_CODE.DISASTER_DEPARTMENT);
    originalDisasterLevelDepts.value = res.sysDictDataList;
  };
  const handleFilterMethod = (query: string) => {
    const filtered = originalDisasterLevelDepts.value.filter((item) =>
      item.itemValue.toLowerCase().includes(query.toLowerCase()),
    );
    disasterLevelDepts.value = filtered;
    isFilterData.value = filtered.length > 0;
  };
  return {
    getFirstLevelDepts,
    firstLevelDepts,
    disasterLevelDepts,
    originalDisasterLevelDepts,
    getDisasterLevelDepts,
    handleFilterMethod,
    isFilterData
  };
};
