"use client"; import React, { useState, useEffect, useRef } from 'react'; import { Calendar as CalendarIcon, Clock, ChevronLeft, ChevronRight, ChevronDown, Check, Sparkles, X, Zap } from 'lucide-react'; interface DateTimePickerProps { value: string; // ISO format: YYYY-MM-DDTHH:mm onChange: (value: string) => void; required?: boolean; label?: string; className?: string; } const MONTH_NAMES = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]; const WEEKDAY_NAMES = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"]; export function DateTimePicker({ value, onChange, required, label, className }: DateTimePickerProps) { const [isOpen, setIsOpen] = useState(false); const containerRef = useRef(null); // Parse incoming value or fallback to current time const parseValue = (isoStr: string) => { if (!isoStr) { const now = new Date(); return { year: now.getFullYear(), month: now.getMonth(), day: now.getDate(), displayHour: now.getHours() % 12 || 12, minutes: Math.ceil(now.getMinutes() / 5) * 5 % 60, period: (now.getHours() >= 12 ? 'PM' : 'AM') as 'AM' | 'PM' }; } const year = parseInt(isoStr.slice(0, 4), 10) || new Date().getFullYear(); const month = (parseInt(isoStr.slice(5, 7), 10) - 1) || 0; const day = parseInt(isoStr.slice(8, 10), 10) || 1; const hours = parseInt(isoStr.slice(11, 13), 10) || 0; const minutes = parseInt(isoStr.slice(14, 16), 10) || 0; const period: 'AM' | 'PM' = hours >= 12 ? 'PM' : 'AM'; const displayHour = hours % 12 || 12; return { year, month, day, displayHour, minutes, period }; }; const initial = parseValue(value); const [viewYear, setViewYear] = useState(initial.year); const [viewMonth, setViewMonth] = useState(initial.month); const [selectedDay, setSelectedDay] = useState(initial.day); const [selectedHour, setSelectedHour] = useState(initial.displayHour); const [selectedMinute, setSelectedMinute] = useState(initial.minutes); const [selectedPeriod, setSelectedPeriod] = useState<'AM' | 'PM'>(initial.period); // Sync state when incoming prop changes useEffect(() => { if (value) { const p = parseValue(value); setViewYear(p.year); setViewMonth(p.month); setSelectedDay(p.day); setSelectedHour(p.displayHour); setSelectedMinute(p.minutes); setSelectedPeriod(p.period); } }, [value]); // Close on click outside useEffect(() => { const handleClickOutside = (e: MouseEvent) => { if (containerRef.current && !containerRef.current.contains(e.target as Node)) { setIsOpen(false); } }; if (isOpen) { document.addEventListener('mousedown', handleClickOutside); } return () => document.removeEventListener('mousedown', handleClickOutside); }, [isOpen]); // Build ISO string helper const buildIso = (y: number, m: number, d: number, hDisplay: number, min: number, p: 'AM' | 'PM') => { let h24 = hDisplay; if (p === 'PM' && h24 < 12) h24 += 12; if (p === 'AM' && h24 === 12) h24 = 0; const pad = (n: number) => String(n).padStart(2, '0'); return `${y}-${pad(m + 1)}-${pad(d)}T${pad(h24)}:${pad(min)}`; }; const handleApplyDate = (y: number, m: number, d: number, h: number, min: number, p: 'AM' | 'PM') => { const iso = buildIso(y, m, d, h, min, p); onChange(iso); }; const handleSelectDay = (day: number) => { setSelectedDay(day); handleApplyDate(viewYear, viewMonth, day, selectedHour, selectedMinute, selectedPeriod); }; const handleHourChange = (newHour: number) => { setSelectedHour(newHour); handleApplyDate(viewYear, viewMonth, selectedDay, newHour, selectedMinute, selectedPeriod); }; const handleMinuteChange = (newMinute: number) => { setSelectedMinute(newMinute); handleApplyDate(viewYear, viewMonth, selectedDay, selectedHour, newMinute, selectedPeriod); }; const handlePeriodChange = (newPeriod: 'AM' | 'PM') => { setSelectedPeriod(newPeriod); handleApplyDate(viewYear, viewMonth, selectedDay, selectedHour, selectedMinute, newPeriod); }; // Month Navigation const handlePrevMonth = () => { if (viewMonth === 0) { setViewMonth(11); setViewYear(v => v - 1); } else { setViewMonth(v => v - 1); } }; const handleNextMonth = () => { if (viewMonth === 11) { setViewMonth(0); setViewYear(v => v + 1); } else { setViewMonth(v => v + 1); } }; // Quick Presets const applyPreset = (minutesToAdd: number) => { const d = new Date(Date.now() + minutesToAdd * 60000); const iso = new Date(d.getTime() - (d.getTimezoneOffset() * 60000)).toISOString().slice(0, 16); onChange(iso); }; const applyTomorrowPreset = (hour: number) => { const d = new Date(); d.setDate(d.getDate() + 1); d.setHours(hour, 0, 0, 0); const iso = new Date(d.getTime() - (d.getTimezoneOffset() * 60000)).toISOString().slice(0, 16); onChange(iso); }; // Calendar Grid Calculation const daysInCurrentMonth = new Date(viewYear, viewMonth + 1, 0).getDate(); const firstDayWeekday = new Date(viewYear, viewMonth, 1).getDay(); const daysInPrevMonth = new Date(viewYear, viewMonth, 0).getDate(); // Check if day is today const now = new Date(); const isCurrentMonthThisMonth = now.getFullYear() === viewYear && now.getMonth() === viewMonth; const todayDate = now.getDate(); // Format display string for trigger input const formatDisplay = () => { if (!value) return "Select target run date & time..."; try { const d = new Date(value); return d.toLocaleString(undefined, { weekday: 'short', month: 'short', day: 'numeric', year: 'numeric', hour: '2-digit', minute: '2-digit' }); } catch { return value; } }; return (
{label && ( )} {/* Custom Interactive Trigger Box */} {/* Floating Glassmorphic Calendar & Time Popover (Time BESIDE Calendar) */} {isOpen && (
e.stopPropagation()} className="absolute left-0 top-full mt-2 z-[160] w-[330px] sm:w-[490px] max-w-[95vw] bg-[#0f1117]/95 backdrop-blur-2xl border border-white/[0.09] rounded-2xl shadow-[0_25px_60px_-15px_rgba(0,0,0,0.85)] p-3.5 animate-in fade-in zoom-in-95 duration-150 overflow-hidden select-none" > {/* Glowing Top Accent Beam */}
{/* Quick Presets Bar */}
{/* Main Content: Calendar (Left) & Time Controls (Right - BESIDE Calendar) */}
{/* LEFT: Calendar Days Grid */}
{/* Month & Year Navigation Header */}
{MONTH_NAMES[viewMonth]} {viewYear}
{/* Weekday Row */}
{WEEKDAY_NAMES.map((w, idx) => ( {w} ))}
{/* Calendar Days */}
{Array.from({ length: firstDayWeekday }).map((_, idx) => { const d = daysInPrevMonth - firstDayWeekday + idx + 1; return (
{d}
); })} {Array.from({ length: daysInCurrentMonth }).map((_, idx) => { const day = idx + 1; const isSelected = selectedDay === day && viewMonth === (parseValue(value).month) && viewYear === (parseValue(value).year); const isToday = isCurrentMonthThisMonth && day === todayDate; return ( ); })}
{/* RIGHT: Time Selector Controls (Beside Calendar!) */}
Time {String(selectedHour).padStart(2, '0')}:{String(selectedMinute).padStart(2, '0')} {selectedPeriod}
{/* Hour & Minute Selectors */}
{/* AM / PM Segmented Switch */}
{(['AM', 'PM'] as const).map((p) => ( ))}
{/* Quick Times Shortcuts */}
{/* Action Bar Footer */}
)}
); }