"use client"; import { useCallback, useState } from "react"; import { useQueries } from "@tanstack/react-query"; import { useTabStore as useGridTabStore } from "@rsone/components"; import { statisticsApi } from "@/entities/statistics/api/statistics-service"; import type { VerificationResultReportItem } from "@/entities/statistics/model/statistics-interface"; import { useAlertStore, useMenuStore } from "@/shared/lib"; import { findMenuItemByUrlPath, resolveInputPageNavigationFromStatsCd, } from "@/shared/lib/stats-cd-input-navigation"; import { useStatisticsInputFocusStore } from "@/store/statistics-input-focus-store"; import { useTabStore } from "@/store/tab-store"; import dayjs from "dayjs"; import { Badge, type BadgeProps, LoadingWithAnimation, Tabs, TabsContent, TabsList, TabsTrigger, } from "@common/components"; type VerificationReportTabKey = "NV" | "IMR" | "HB"; const VERIFICATION_REPORT_TABS: { value: VerificationReportTabKey; label: string }[] = [ { value: "NV", label: "일반" }, { value: "IMR", label: "I-MR" }, { value: "HB", label: "HB" }, ]; const TYPE_CODE_COLOR_MAP: Record = { NV: "gray", IMR: "red", HB: "blue", }; const TYPE_CODE_NAME_MAP: Record = { NV: "일반", IMR: "I-MR", HB: "HB", }; type VerificationSelectionReportProps = { onClose?: () => void; }; const VerificationSelectionReport = ({ onClose, }: VerificationSelectionReportProps) => { const menuList = useTabStore((s) => s.menuList); const addTab = useTabStore((s) => s.addTab); const setCurrentMenuInfo = useMenuStore((s) => s.setCurrentMenuInfo); const addTabForGrid = useGridTabStore((s) => s.addTab); const setPendingInputTab = useStatisticsInputFocusStore( (s) => s.setPendingInputTab, ); const alert = useAlertStore((s) => s.setMessage); const [activeTab, setActiveTab] = useState("NV"); const statsYr = String(dayjs().year() - 1); const baseRequest = { statsYr, pbcpAprvYn: "N" as const, page: 1, size: 10000, prcsYnList: "0", }; const [nvQuery, imrQuery, hbQuery] = useQueries({ queries: [ { queryKey: [ "verificationResultList", statsYr, "NV", baseRequest.pbcpAprvYn, baseRequest.page, baseRequest.size, baseRequest.prcsYnList, ], queryFn: () => statisticsApi.getVerificationResultList({ ...baseRequest, vrfcMthdCd: "NV", }), }, { queryKey: [ "verificationResultList", statsYr, "IMR", baseRequest.pbcpAprvYn, baseRequest.page, baseRequest.size, baseRequest.prcsYnList, ], queryFn: () => statisticsApi.getVerificationResultList({ ...baseRequest, vrfcMthdCd: "IMR", }), }, { queryKey: [ "verificationResultList", statsYr, "HB", baseRequest.pbcpAprvYn, baseRequest.page, baseRequest.size, baseRequest.prcsYnList, ], queryFn: () => statisticsApi.getVerificationResultList({ ...baseRequest, vrfcMthdCd: "HB", }), }, ], }); const isLoading = nvQuery.isLoading || imrQuery.isLoading || hbQuery.isLoading; const error = nvQuery.error ?? imrQuery.error ?? hbQuery.error; const listsByTab: Record = { NV: nvQuery.data?.data?.verifyResultList ?? [], IMR: imrQuery.data?.data?.verifyResultList ?? [], HB: hbQuery.data?.data?.verifyResultList ?? [], }; const openInputFromStatsCd = useCallback( (item: VerificationResultReportItem) => { const nav = resolveInputPageNavigationFromStatsCd(item.statsCd); if (!nav) { alert( `입력 화면으로 연결되지 않는 통계코드입니다: ${item.statsCd}`, ); return; } if (!menuList) { alert( "메뉴 정보를 불러오는 중입니다. 잠시 후 다시 시도해 주세요.", ); return; } const menu = findMenuItemByUrlPath(menuList, nav.urlPath); if (!menu) { alert( "해당 입력 메뉴를 찾을 수 없습니다. 메뉴 권한을 확인해 주세요.", ); return; } setPendingInputTab(nav.urlPath, nav.tabKey, { statsYr: item.statsYr, sewerBzmnCd: item.sewerBzmnCd, ctpvNm: item.ctpvNm, sggNm: item.sggNm ?? "", }); setCurrentMenuInfo(menu); const ok = addTab(menu); if (ok) { addTabForGrid(menu); } else { alert(`최대 추가 할 수 있는 화면 수(10개)를 \n초과 하였습니다. 기존 화면을 닫은 후 이용해주세요.`); } }, [ menuList, addTab, addTabForGrid, setCurrentMenuInfo, setPendingInputTab, alert, ], ); const renderResultList = ( typeCode: VerificationReportTabKey, items: VerificationResultReportItem[], ) => (
{items.length === 0 ? (
항목이 없습니다.
) : (
{items.map((item) => (
openInputFromStatsCd(item)} onKeyDown={(e) => { if (e.key === "Enter" || e.key === " ") { e.preventDefault(); openInputFromStatsCd(item); } }} >
{item.statsNm} {TYPE_CODE_NAME_MAP[item.vrfcMthdCd] ?? item.vrfcMthdNm}

{item.rsnExpln}

{item.admdstNm} 현재: {item.nowVlCn ?? "-"} / 비교:{" "} {item.compVlCn ?? "-"}
))}
)}
); return (

이상치 선별 보고서

onClose?.()} onKeyDown={(e) => { if (e.key === "Enter" || e.key === " ") { e.preventDefault(); onClose?.(); } }} >
{isLoading && (
)} {error && (
데이터를 불러올 수 없습니다.
)} {!isLoading && !error && (
setActiveTab(value as VerificationReportTabKey) } className="flex min-h-0 flex-1 flex-col h-full" variant="default" > {VERIFICATION_REPORT_TABS.map((tab) => ( {tab.label} ({listsByTab[tab.value].length}) ))} {VERIFICATION_REPORT_TABS.map((tab) => ( {activeTab === tab.value ? renderResultList( tab.value, listsByTab[tab.value], ) : null} ))}
)}
); }; export default VerificationSelectionReport;