import { getData } from "@/components/lib/api"; import { formatDisplayData } from '@/components/lib/utils/formatDisplayValue'; import useStore from '../../../store/useStore'; import GisApp from "@common/business_components/ui/map/lib/gis.core"; import useSearchModalStore from '../../../store/useSearchModalStore'; import useBasicTab from './useBasicTab'; import useOperationTab from './useOperationTab'; export default function useSearchModal() { const { activeTab, workType, tableData, detailTableData, curPage, var1, var2, yearFilter, todayYear, todayMonth, todayDate, setIsSearchModalOpen, setShowTable, setTableData, setDetailTableData, setTableCount, setDetailTableCount, setCurPage, setIsLoading, setIsResetPage, setIsResetDetailPage, setIsResetSelection, setIsResetDetailSelection, getDateString } = useSearchModalStore(); const { fetchBasicFilteredData } = useBasicTab(); const { OPERATION_TABLE_SIZE } = useOperationTab(); const { BIZ_TY_CD } = useStore(); const TABS = [ { val: "basic", text: "기본정보" }, { val: "operation", text: "운영정보" }, { val: "report", text: "하수도정비기본계획보고서" }, ]; // 검색 API 호출 함수 const fetchFilteredData = async () => { setIsLoading(true); // 로딩 시작 setIsResetSelection(true); // 선택 초기화 setIsResetDetailSelection(true); // 선택 초기화 try { if (activeTab === 'basic') { fetchBasicFilteredData(); } else if (activeTab === 'operation') { const RequestDTO: Record = { page: `${curPage - 1}`, size: `${OPERATION_TABLE_SIZE}`, }; if (var1) RequestDTO.bizInfoNm = var1; if (var2 != '0' && var2) RequestDTO.bizTypeCd = var2; // 수행기간 범위 const startDate = getDateString( yearFilter.start, '', '', "19000101", ); const endDate = getDateString( yearFilter.end, '', '', `${todayYear}${todayMonth}${todayDate}`, ); RequestDTO.fromflfmtEndYmd = startDate; RequestDTO.toflfmtEndYmd = endDate; // JSON.parse 사용으로 문자열 -> 배열 변환 RequestDTO.workTypes = JSON.parse(workType.replace(/'/g, '"')); const params = new URLSearchParams(RequestDTO).toString(); const res = await getData(`v1/bizInfo?${params}`); const mappedContent = (res.content || []).map((item: any, index: number) => { const formattedItem = formatDisplayData(item); const matched = BIZ_TY_CD.find(code => code.lwpoCd === item.bizTypeCd); return { ...formattedItem, rowNumber: (curPage - 1) * OPERATION_TABLE_SIZE + index + 1, // 1부터 시작하는 일련번호 추가 bizTypeCd: matched ? matched.lwpoCdNm : item.bizTypeCd, }; }); setTableData(mappedContent || []); setTableCount(res.totalElements || 0); setDetailTableData([]); setDetailTableCount(0); } setShowTable(true); setIsResetSelection(false); } catch (err) { console.error('API ERROR:', err); setTableData([]); setIsResetSelection(false); } finally { setIsLoading(false); // 로딩 종료 setIsResetSelection(false); // setIsResetDetailSelection(false); } }; // 조회 버튼 클릭 시 1페이지로 이동 및 데이터 초기화 const handleSearch = () => { setCurPage(1); // 페이지를 1로 초기화 fetchFilteredData(); setIsResetPage(true); // 페이지를 초기화 setIsResetDetailPage(true); // 디테일 페이지를 초기화 setIsResetSelection(false); // 선택 초기화 setIsResetDetailSelection(false); // 선택 초기화 setShowTable(true); }; // 여러 시설 보기 const viewMultipleFeature = async () => { if (activeTab === 'basic') { if (!(tableData.length === 0)) { setIsLoading(true); const row = tableData[0]; const layerNm = row.lyrTypeNm; const fids = tableData.map((item) => item.fid); const core = GisApp.Module.layer as any; await core.showMultipleSearchResultsFromGeoJSON(layerNm, fids); setIsLoading(false); setIsSearchModalOpen(false); } } else if (activeTab === 'operation') { if (!(detailTableData.length === 0)) { setIsLoading(true); const row = detailTableData[0]; const layerNm = row.lyrTypeNm; const fids = detailTableData.map((item) => item.fid); const core = GisApp.Module.layer as any; await core.showMultipleSearchResultsFromGeoJSON(layerNm, fids); setIsLoading(false); setIsSearchModalOpen(false); } } } return { TABS, getDateString, handleSearch, viewMultipleFeature, fetchFilteredData }; };