import React, { useState, useRef, useEffect } from 'react' import ReactDOM from 'react-dom' import { Calendar, Clock, ChevronLeft, ChevronRight, ChevronUp, ChevronDown } from 'lucide-react' import { useTranslation } from '../../i18n' function daysInMonth(year, month) { return new Date(year, month + 1, 0).getDate() } function getWeekday(year, month, day) { return new Date(year, month, day).getDay() } // ── Datum-Only Picker ──────────────────────────────────────────────────────── export function CustomDatePicker({ value, onChange, placeholder, style = {} }) { const { locale, t } = useTranslation() const [open, setOpen] = useState(false) const ref = useRef(null) const dropRef = useRef(null) const parsed = value ? new Date(value + 'T00:00:00') : null const [viewYear, setViewYear] = useState(parsed?.getFullYear() || new Date().getFullYear()) const [viewMonth, setViewMonth] = useState(parsed?.getMonth() ?? new Date().getMonth()) useEffect(() => { const handler = (e) => { if (ref.current?.contains(e.target)) return if (dropRef.current?.contains(e.target)) return setOpen(false) } if (open) document.addEventListener('mousedown', handler) return () => document.removeEventListener('mousedown', handler) }, [open]) useEffect(() => { if (open && parsed) { setViewYear(parsed.getFullYear()); setViewMonth(parsed.getMonth()) } }, [open]) const prevMonth = () => { if (viewMonth === 0) { setViewMonth(11); setViewYear(y => y - 1) } else setViewMonth(m => m - 1) } const nextMonth = () => { if (viewMonth === 11) { setViewMonth(0); setViewYear(y => y + 1) } else setViewMonth(m => m + 1) } const monthLabel = new Date(viewYear, viewMonth).toLocaleDateString(locale, { month: 'long', year: 'numeric' }) const days = daysInMonth(viewYear, viewMonth) const startDay = (getWeekday(viewYear, viewMonth, 1) + 6) % 7 // Mo=0 const weekdays = Array.from({ length: 7 }, (_, i) => new Date(2024, 0, i + 1).toLocaleDateString(locale, { weekday: 'narrow' })) const displayValue = parsed ? parsed.toLocaleDateString(locale, { day: 'numeric', month: 'short', year: 'numeric' }) : null const selectDay = (day) => { const y = String(viewYear) const m = String(viewMonth + 1).padStart(2, '0') const d = String(day).padStart(2, '0') onChange(`${y}-${m}-${d}`) setOpen(false) } const selectedDay = parsed && parsed.getFullYear() === viewYear && parsed.getMonth() === viewMonth ? parsed.getDate() : null const today = new Date() const isToday = (d) => today.getFullYear() === viewYear && today.getMonth() === viewMonth && today.getDate() === d return (