/**
 * 灾害处置公用hook
 */

import { ref } from 'vue';
import type { SysDictDataDetail } from '@/api/dict';
import type {
  disasterReportRecordDetailListResponse,
  DisasterLossDetailQuery,
  DisasterLossDetailResponse,
} from '@/types/disaster-control';
import { queryDictTypeDetail } from '@/api/dict';
import { getLossRecordTableData, getDisasterLossDetail } from '@/api/disaster-control';
import { DICT_CODE } from '@/constant/dict';

export const useDisasterControlHook = () => {
  const safetyLevelDice = ref<SysDictDataDetail[]>([]);
  const priorityDice = ref<SysDictDataDetail[]>([]);
  const lossReportItem = ref<disasterReportRecordDetailListResponse>();
  const getSafetyLevelDict = async () => {
    const res = await queryDictTypeDetail(DICT_CODE.SAFETY_LEVEL);
    safetyLevelDice.value = res.sysDictDataList;
  };
  const getPriorityDict = async () => {
    const res = await queryDictTypeDetail(DICT_CODE.PRIORITY);
    priorityDice.value = res.sysDictDataList;
  };
  const getSafetyLevel = (safetyLevelCode: string | undefined) => {
    if (!safetyLevelCode) return '';
    return safetyLevelDice.value.find((item) => item.itemCode === safetyLevelCode)?.itemValue;
  };
  const getPriority = (priorityCode: string | undefined) => {
    if (!priorityCode) return '';
    return priorityDice.value.find((item) => item.itemCode === priorityCode)?.itemValue;
  };
  const getLossReportItem = async (handleTaskId: number, id: number) => {
    const res = await getLossRecordTableData({
      handleTaskIds: [handleTaskId],
    });
    const disasterReportRecordDetailList = res[0].disasterReportRecordDetailList;
    lossReportItem.value = disasterReportRecordDetailList.find((item) => item.id === id);
    return lossReportItem.value;
  };
  const disasterLossDetailList = ref<DisasterLossDetailResponse>();
  const getDisasterLossDetailList = async (data: DisasterLossDetailQuery) => {
    const res = await getDisasterLossDetail(data);
    disasterLossDetailList.value = res;
  };
  return {
    priorityDice,
    safetyLevelDice,
    getPriorityDict,
    getSafetyLevelDict,
    getSafetyLevel,
    getPriority,
    lossReportItem,
    getLossReportItem,
    getDisasterLossDetailList,
    disasterLossDetailList,
  };
};
