/**
 * 应急物资公用hook
 */
import { ref } from 'vue';
import type { QueryUserInfoByUserNameRes } from '@/types/person-group/type';
import type { SysDictDataDetail } from '@/api/dict';
import { queryDictTypeDetail } from '@/api/dict';
import { queryUserInfoByUserName } from '@/api/system/person-group';
import { DICT_CODE } from '@/constant/dict';
import { EMERGENCY_SUPPLY_STATUS_OPTIONS } from './constant';
export const useEmergencySuppliesHook = () => {
  // 应急物资类型
  const emergencySupplyDice = ref<SysDictDataDetail[]>([]);
  const getEmergencySupplyDict = async () => {
    const emergencySupplyRes = await queryDictTypeDetail(DICT_CODE.EMERGENCY_SUPPLY);
    emergencySupplyDice.value = emergencySupplyRes.sysDictDataList;
  };
  const getEmergencySupply = (emergencySupplyType: string) => {
    return emergencySupplyDice.value.find((item) => item.itemCode === emergencySupplyType)?.itemValue;
  };
  const getEmergencyStatus = (status: number) => {
    return EMERGENCY_SUPPLY_STATUS_OPTIONS.find((item) => item.value === status)?.label;
  };
  // 园区
  const parkDice = ref<SysDictDataDetail[]>([]);
  const getParkDict = async () => {
    const parkRes = await queryDictTypeDetail(DICT_CODE.PARK);
    parkDice.value = parkRes.sysDictDataList;
  };
  const getPark = (park: string) => {
    return parkDice.value.find((item) => item.itemCode === park)?.itemValue;
  };
  // 地点
  const locationDice = ref<SysDictDataDetail[]>([]);
  const getLocationDict = async (type: 'zhangjiang_park' | 'dachang_park' | 'all') => {
    const locationZJRes = await queryDictTypeDetail(DICT_CODE.LOCATION_ZJ);
    const locationDCRes = await queryDictTypeDetail(DICT_CODE.LOCATION_DC);
    if (type === 'all') {
      locationDice.value = [...locationZJRes.sysDictDataList, ...locationDCRes.sysDictDataList];
    } else if (type === 'zhangjiang_park') {
      locationDice.value = locationZJRes.sysDictDataList;
    } else if (type === 'dachang_park') {
      locationDice.value = locationDCRes.sysDictDataList;
    }
  };
  const handleChangePark = (type: 'zhangjiang_park' | 'dachang_park') => {
    getLocationDict(type);
  };
  const getLocation = (location: string) => {
    return locationDice.value.find((item) => item.itemCode === location)?.itemValue;
  };
  const userOptions = ref<QueryUserInfoByUserNameRes[]>([]);
  const loading = ref<boolean>(false);
  const remoteMethod = async (query: string) => {
    if (!query) {
      userOptions.value = [];
      return;
    }
    loading.value = true;
    userOptions.value = await queryUserInfoByUserName(query);
    loading.value = false;
  };
  return {
    emergencySupplyDice,
    getEmergencySupplyDict,
    getEmergencySupply,
    getEmergencyStatus,
    parkDice,
    getParkDict,
    getPark,
    handleChangePark,
    locationDice,
    getLocationDict,
    getLocation,
    userOptions,
    loading,
    remoteMethod,
  };
};
