import { useState } from 'react' import { useQuery } from '@tanstack/react-query' import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer } from 'recharts' import client from '../../../api/client' import { FilterBar, FilterDateInput } from '../shared/FilterBar' import { Panel, DataTable, THead, TH, TR, TD, StatusBadge, ChartTooltip } from '../shared/TablePrimitives' import DrillDownModal from '../shared/DrillDownModal' import EmptyState from '../shared/EmptyState' import SkeletonTable from '../shared/SkeletonTable' import ExportButton from '../shared/ExportButton' import { fmtEUR, fmtNum, fmtDate, fmtTime, fmtDuration } from '../shared/reportDesignTokens' function today() { return new Date().toISOString().slice(0, 10) } function monthAgo() { const d = new Date(); d.setDate(d.getDate() - 30); return d.toISOString().slice(0, 10) } export default function WorkDaySummary() { const [from, setFrom] = useState(monthAgo()) const [to, setTo] = useState(today()) const [drillId, setDrillId] = useState(null) const { data, isLoading, isError, refetch } = useQuery({ queryKey: ['business-days', from, to], queryFn: () => client.get('/api/reports/business-days', { params: { from: from + 'T00:00:00', to: to + 'T23:59:59' } }).then(r => r.data), staleTime: 60 * 1000, }) const { data: drillData } = useQuery({ queryKey: ['business-day-orders', drillId], queryFn: () => client.get('/api/reports/orders/history', { params: { business_day_id: drillId, page_size: 200 } }).then(r => r.data), enabled: !!drillId, staleTime: 60 * 1000, }) const days = data?.business_days || [] const drillDay = drillId ? days.find(d => d.id === drillId) : null const drillOrders = Array.isArray(drillData) ? drillData : [] const chartData = [...days].reverse().map(d => ({ date: fmtDate(d.opened_at), revenue: d.revenue, })) if (isLoading) return
if (isError) return (
Αδυναμία φόρτωσης δεδομένων
) return (
}>
{chartData.length > 0 && (
'€' + v} /> fmtEUR(v)} />} />
)}
{days.length === 0 ? ( ) : ( Εργάσιμη Μέρα Άνοιξε Έκλεισε Διάρκεια Παραγγελίες Έσοδα Ακυρώσεις Σερβιτόροι Κατάσταση {days.map(d => ( setDrillId(d.id)} striped> {fmtDate(d.opened_at)} {fmtTime(d.opened_at)} {d.closed_at ? fmtTime(d.closed_at) : '—'} {fmtDuration(d.opened_at, d.closed_at)} {fmtNum(d.order_count)} {fmtEUR(d.revenue)} {fmtNum(d.cancellation_count)} {fmtNum(d.waiter_count)} ))} )}
{drillDay && ( setDrillId(null)} > #ΤραπέζιΆνοιξεΈκλεισεΣύνολοΚατάσταση {drillOrders.map(o => { const total = (o.items || []).filter(i => ['active', 'paid'].includes(i.status)).reduce((s, i) => s + i.unit_price * i.quantity, 0) return ( #{o.id} {o.table_id} {fmtDate(o.opened_at)} {fmtTime(o.opened_at)} {o.closed_at ? fmtTime(o.closed_at) : '—'} {fmtEUR(total)} ) })} )}
) }