import { useFoStore } from '@/app/store'; import { DatePickerRange, SelectBox } from '@common/business_components/ui'; import ListDiv from '@common/business_components/ui/list_div'; import { Table, TableCell, TableHead, TableRow } from '@common/components/ui'; import { useCallback, useEffect, useRef, useState } from 'react'; type FacilityTermModalHeaderProps = { fcltyCd : string; fcltyName: string; tabName: string; searchYear: string; searchList: T[]; ref: React.RefObject>; queryClick:(returnType:ReturnType) => void; } export type ReturnType = { startDate: Date; endDate: Date; tabName: string; filteredList: T[]; } export default function FacilityTermModalHeader({ searchList, fcltyName, tabName, searchYear, queryClick, ref }: FacilityTermModalHeaderProps) { const {Common} = useFoStore((state) => state); const { selectFlowPanel } = Common; const [startDate, setStartDate] = useState(new Date(Number(searchYear), 0, 1)); const [endDate, setEndDate] = useState(new Date(Number(searchYear), 11, 31)); const startPickerRef = useRef(null); const endPickerRef = useRef(null); const [flowPanel, setFlowPanel] = useState(selectFlowPanel[0]?.val ?? '00'); const [copySearchList, setCopySearchList] = useState(searchList); const [standSearchList, setStandSearchList] = useState(searchList); const isDateInRanges = (date: Date) => { const standYear = parseInt(searchYear); const dateRanges:{start:Date, end:Date}[] = []; if (tabName.includes("_summer")) { dateRanges.push({ start: new Date(standYear, 5, 1), end: new Date(standYear, 7, 31) }); } else if (tabName.includes("_winter")) { dateRanges.push({ start: new Date(standYear, 0, 1), end: new Date(standYear, 1, 28) }); } else { dateRanges.push({ start: new Date(standYear, 0, 1), end: new Date(standYear, 11, 31) }); } return dateRanges.some(range => { const start = new Date(range.start); const end = new Date(range.end); return date >= start && date <= end; }); }; useEffect(() => { let startDate = new Date(Number(searchYear), 0, 1); let endDate = new Date(Number(searchYear), 11, 31); if(tabName.includes("_summer")) { startDate = new Date(Number(searchYear), 5, 1); endDate = new Date(Number(searchYear), 7, 31); setStartDate(startDate); setEndDate(endDate); } else if (tabName.includes("_winter")) { startDate = new Date(Number(searchYear), 0, 1); endDate = new Date(Number(searchYear), 1, 28); setStartDate(startDate); setEndDate(endDate); } else { startDate = new Date(Number(searchYear), 0, 1); endDate = new Date(Number(searchYear), 11, 31); setStartDate(startDate); setEndDate(endDate); } let filteredData: T[] = []; if(tabName.includes("_rain")) { filteredData = searchList.filter((item) => { const itemDate = new Date((item as T & { msrDt: string }).msrDt); const itemDateOnly = new Date(itemDate.getFullYear(), itemDate.getMonth(), itemDate.getDate()); const startDateOnly = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate()); const endDateOnly = new Date(endDate.getFullYear(), endDate.getMonth(), endDate.getDate()); const rainEffectDay = (item as T & { rainEffectDay: number }).rainEffectDay; if(flowPanel === '2'){ const infoRnDay = (item as T & { infoRnDay: number }).infoRnDay; return (itemDateOnly >= startDateOnly && itemDateOnly <= endDateOnly) && infoRnDay >= 3 && infoRnDay < 10; } else if(flowPanel === '3'){ const infoRnDay = (item as T & { infoRnDay: number }).infoRnDay; return (itemDateOnly >= startDateOnly && itemDateOnly <= endDateOnly) && infoRnDay >= 10 && infoRnDay < 20; } else if(flowPanel === '4'){ const infoRnDay = (item as T & { infoRnDay: number }).infoRnDay; return (itemDateOnly >= startDateOnly && itemDateOnly <= endDateOnly) && infoRnDay >= 20 && infoRnDay < 30; } else if(flowPanel === '5'){ const infoRnDay = (item as T & { infoRnDay: number }).infoRnDay; return (itemDateOnly >= startDateOnly && itemDateOnly <= endDateOnly) && infoRnDay >= 30 && infoRnDay < 40; } else if(flowPanel === '6'){ const infoRnDay = (item as T & { infoRnDay: number }).infoRnDay; return (itemDateOnly >= startDateOnly && itemDateOnly <= endDateOnly) && infoRnDay >= 40 && infoRnDay < 50; } else if(flowPanel === '7'){ const infoRnDay = (item as T & { infoRnDay: number }).infoRnDay; return (itemDateOnly >= startDateOnly && itemDateOnly <= endDateOnly) && infoRnDay >= 50 && infoRnDay < 60; } else if(flowPanel === '8'){ const infoRnDay = (item as T & { infoRnDay: number }).infoRnDay; return (itemDateOnly >= startDateOnly && itemDateOnly <= endDateOnly) && infoRnDay >= 60; } else { return (itemDateOnly >= startDateOnly && itemDateOnly <= endDateOnly) && (rainEffectDay ===1); } }); } else if(tabName.includes("_chuncheon")) { filteredData = searchList.filter((item) => { const itemDate = new Date((item as T & { msrDt: string }).msrDt); const itemDateOnly = new Date(itemDate.getFullYear(), itemDate.getMonth(), itemDate.getDate()); const startDateOnly = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate()); const endDateOnly = new Date(endDate.getFullYear(), endDate.getMonth(), endDate.getDate()); const rainEffectDay = (item as T & { rainEffectDay: number }).rainEffectDay; return (itemDateOnly >= startDateOnly && itemDateOnly <= endDateOnly) && rainEffectDay==0; }); } else { filteredData = searchList.filter((item) => { const itemDate = new Date((item as T & { msrDt: string }).msrDt); const itemDateOnly = new Date(itemDate.getFullYear(), itemDate.getMonth(), itemDate.getDate()); const startDateOnly = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate()); const endDateOnly = new Date(endDate.getFullYear(), endDate.getMonth(), endDate.getDate()); return itemDateOnly >= startDateOnly && itemDateOnly <= endDateOnly; }); } setCopySearchList(filteredData); setStandSearchList(filteredData); }, [searchList, searchYear, tabName, flowPanel]); const handleStartDateSelect = (day: Date | undefined) => { if(day && day > endDate) { alert('종료일은 시작일 이전일 수 없습니다.'); return; } const selectedDate = day ?? startDate; setStartDate(selectedDate); const button = startPickerRef.current?.querySelector('button'); if (button) { setTimeout(() => button.click(), 0); } const filteredData = standSearchList.filter((item) => { const itemDate = new Date((item as T & { msrDt: string }).msrDt); const itemDateOnly = new Date(itemDate.getFullYear(), itemDate.getMonth(), itemDate.getDate()); const startDateOnly = new Date(selectedDate.getFullYear(), selectedDate.getMonth(), selectedDate.getDate()); const endDateOnly = new Date(endDate.getFullYear(), endDate.getMonth(), endDate.getDate()); return itemDateOnly >= startDateOnly && itemDateOnly <= endDateOnly; }); setCopySearchList(filteredData); }; const handleEndDateSelect = (day: Date | undefined) => { if(day && day < startDate) { alert('종료일은 시작일 이후일 수 없습니다.'); return; } const selectedDate = day ?? endDate; setEndDate(selectedDate); const button = endPickerRef.current?.querySelector('button'); if (button) { setTimeout(() => button.click(), 0); } const filteredData = standSearchList.filter((item) => { const itemDate = new Date((item as T & { msrDt: string }).msrDt); const itemDateOnly = new Date(itemDate.getFullYear(), itemDate.getMonth(), itemDate.getDate()); const startDateOnly = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate()); const endDateOnly = new Date(selectedDate.getFullYear(), selectedDate.getMonth(), selectedDate.getDate()); return itemDateOnly >= startDateOnly && itemDateOnly <= endDateOnly; }); setCopySearchList(filteredData); }; const fncSetFilteredList = useCallback(() => { queryClick({ startDate: startDate, endDate: endDate, tabName: tabName, filteredList: copySearchList }); }, [queryClick, startDate, endDate, tabName, copySearchList]); useEffect(() => { fncSetFilteredList(); }, [fncSetFilteredList]); useEffect(() => { if(ref && ref.current) { const customRef = ref.current; customRef.startDate = startDate; customRef.endDate = endDate; customRef.tabName = tabName; customRef.filteredList = copySearchList; } }, [startDate, endDate, tabName, copySearchList, ref]); return (
{'시설명'} {tabName === "tab_term_rain" ? {fcltyName} : {fcltyName}} {tabName === "tab_term_rain" && ( <> {'강수구분'}
setFlowPanel(val.toString())} className="mx-3 w-[150px]" label="" selectClass="w-full p-1 border rounded bg-white" />
)} {'년/월'}
handleStartDateSelect(date as Date)} className="w-full p-1 border rounded bg-white" disabled={(date) => !isDateInRanges(date)} defaultMonth={startDate} />

~

handleEndDateSelect(date as Date)} className="w-full p-1 border rounded bg-white" disabled={(date) => !isDateInRanges(date)} defaultMonth={endDate} />
); }