import { useState, useEffect, useMemo } from 'react'; import { Plus, Printer, ChevronRight, CalendarDays } from 'lucide-react'; import { format, startOfMonth, endOfMonth, eachDayOfInterval, isToday, getDay } from 'date-fns'; import { ru } from 'date-fns/locale'; import { useDeliveryStore } from '../stores/deliveryStore'; import type { Delivery } from '../types'; import { pickupLocationLabels } from '../types'; import { Button } from '../components/ui/Button'; import { Card } from '../components/ui/Card'; interface DashboardProps { onDateSelect: (date: string) => void; onAddDelivery: () => void; } const Dashboard = ({ onDateSelect, onAddDelivery }: DashboardProps) => { const deliveryCounts = useDeliveryStore(state => state.deliveryCounts); const fetchDeliveryCounts = useDeliveryStore(state => state.fetchDeliveryCounts); const [currentMonth, setCurrentMonth] = useState(new Date()); // Fetch counts on mount useEffect(() => { fetchDeliveryCounts(); }, [fetchDeliveryCounts]); const days = useMemo(() => { const monthStart = startOfMonth(currentMonth); const monthEnd = endOfMonth(currentMonth); return eachDayOfInterval({ start: monthStart, end: monthEnd }); }, [currentMonth]); const getCountForDate = (date: Date) => { const dateStr = format(date, 'dd-MM-yyyy'); return deliveryCounts[dateStr] || 0; }; const handlePrintDay = (date: Date) => { const dateStr = format(date, 'dd-MM-yyyy'); const fetchDeliveriesByDate = useDeliveryStore.getState().fetchDeliveriesByDate; // Fetch and print fetchDeliveriesByDate(dateStr).then(() => { const deliveries = useDeliveryStore.getState().deliveries; printDeliveries(date, deliveries); }); }; const printDeliveries = (date: Date, dayDeliveries: Delivery[]) => { const printWindow = window.open('', '_blank'); if (!printWindow) return; const html = ` Доставки на ${format(date, 'dd MMMM yyyy', { locale: ru })}

Доставки на ${format(date, 'dd MMMM yyyy', { locale: ru })}

${dayDeliveries.map((d: Delivery) => ` `).join('')}
Загрузка Товар Клиент Адрес Телефон Услуги Комментарий
${d.pickupLocation2 ? pickupLocationLabels[d.pickupLocation] + ' + ' + pickupLocationLabels[d.pickupLocation2] : pickupLocationLabels[d.pickupLocation]} ${d.productName}${d.productName2 ? '
+ ' + d.productName2 + '' : ''}
${d.customerName} ул. ${d.street}, д. ${d.house}${d.apartment ? ', кв. ' + d.apartment : ''} ${d.entrance || d.floor ? '
' + (d.entrance ? 'Подъезд ' + d.entrance : '') + (d.entrance && d.floor ? ', ' : '') + (d.floor ? 'этаж ' + d.floor : '') + '' : ''}
${d.phone} ${d.serviceInfo || '-'} ${d.comment || '-'}
`; printWindow.document.write(html); printWindow.document.close(); printWindow?.print(); }; const navigateMonth = (direction: 'prev' | 'next') => { setCurrentMonth(prev => { const newDate = new Date(prev); newDate.setMonth(prev.getMonth() + (direction === 'next' ? 1 : -1)); return newDate; }); }; return (

Панель управления

Выберите дату для просмотра доставок

{format(currentMonth, 'LLLL yyyy', { locale: ru })}

{['Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс'].map(day => (
{day}
))}
{Array.from({ length: (getDay(startOfMonth(currentMonth)) + 6) % 7 }).map((_, i) => (
))} {days.map((day) => { const count = getCountForDate(day); const isTodayDate = isToday(day); return ( ); })}

Ближайшие даты с доставками

{days .filter(day => getCountForDate(day) > 0) .slice(0, 7) .map(day => { const count = getCountForDate(day); return (
{format(day, 'd')}
{format(day, 'MMM', { locale: ru })}
{count} {count === 1 ? 'доставка' : count < 5 ? 'доставки' : 'доставок'}
{format(day, 'EEEE', { locale: ru })}
); })} {days.filter(day => getCountForDate(day) > 0).length === 0 && (

Нет запланированных доставок

)}
); }; export default Dashboard;