'use client'; import { useState } from 'react'; import { motion } from 'framer-motion'; import { Calendar as CalendarIcon, ChevronLeft, ChevronRight } from 'lucide-react'; export default function BookingCalendar({ property, onDateSelect }) { const [currentMonth, setCurrentMonth] = useState(new Date()); const [selectedRange, setSelectedRange] = useState({ start: null, end: null }); const [bookedDates, setBookedDates] = useState(property.bookings || []); const daysInMonth = new Date( currentMonth.getFullYear(), currentMonth.getMonth() + 1, 0 ).getDate(); const firstDayOfMonth = new Date( currentMonth.getFullYear(), currentMonth.getMonth(), 1 ).getDay(); const monthNames = [ 'يناير', 'فبراير', 'مارس', 'إبريل', 'مايو', 'يونيو', 'يوليو', 'أغسطس', 'سبتمبر', 'أكتوبر', 'نوفمبر', 'ديسمبر' ]; const isDateBooked = (date) => { const dateStr = date.toISOString().split('T')[0]; return bookedDates.some(booking => { const start = new Date(booking.startDate); const end = new Date(booking.endDate); const current = new Date(date); return current >= start && current <= end; }); }; const isInSelectedRange = (date) => { if (!selectedRange.start || !selectedRange.end) return false; const dateStr = date.toISOString().split('T')[0]; return dateStr >= selectedRange.start && dateStr <= selectedRange.end; }; const handleDateClick = (date) => { if (isDateBooked(date)) return; const dateStr = date.toISOString().split('T')[0]; if (!selectedRange.start || (selectedRange.start && selectedRange.end)) { setSelectedRange({ start: dateStr, end: null }); } else { if (dateStr > selectedRange.start) { setSelectedRange({ ...selectedRange, end: dateStr }); onDateSelect?.({ start: selectedRange.start, end: dateStr }); } else { setSelectedRange({ start: dateStr, end: null }); } } }; const changeMonth = (direction) => { setCurrentMonth(new Date( currentMonth.getFullYear(), currentMonth.getMonth() + direction, 1 )); }; const renderDays = () => { const days = []; const totalDays = daysInMonth + firstDayOfMonth; for (let i = 0; i < totalDays; i++) { if (i < firstDayOfMonth) { days.push(
); } else { const dayNumber = i - firstDayOfMonth + 1; const date = new Date( currentMonth.getFullYear(), currentMonth.getMonth(), dayNumber ); const isBooked = isDateBooked(date); const isSelected = isInSelectedRange(date); const isToday = date.toDateString() === new Date().toDateString(); days.push(