import { useState } from "react"; import { useQuery } from "@tanstack/react-query"; import Box from "@mui/material/Box"; import { useAuth } from "../context/AuthContext"; import Forbidden from "../components/Forbidden"; import { formatDate } from "../utils/attendanceHelpers"; import { formatKm } from "../utils/formatters"; import { tripHistoryOptions, tripVehiclesOptions } from "../lib/queries/trips"; import { Card, DataTable, Select, MonthField, StatusChip, PageHeader, PageEnter, FilterBar, EmptyState, LoadingState, StatCard, type DataColumn, } from "../ui"; interface Trip { id: number; trip_date: string; spz: string; driver_name: string; route_from: string; route_to: string; start_km: number; end_km: number; distance: number; is_business: number | boolean; notes?: string; } const TripsIcon = ( ); const TotalIcon = ( ); const BusinessIcon = ( ); export default function TripsHistory() { const { user, hasPermission } = useAuth(); const [month, setMonth] = useState(() => { const now = new Date(); return `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, "0")}`; }); const [vehicleId, setVehicleId] = useState(""); const { data: vehiclesData = [] } = useQuery(tripVehiclesOptions()); const vehicles = vehiclesData; const { data: tripsData, isPending } = useQuery( tripHistoryOptions({ month, vehicleId: vehicleId ? Number(vehicleId) : undefined, userId: user?.id, }), ); const trips: Trip[] = (tripsData ?? []).map((t) => ({ id: t.id, trip_date: t.trip_date, spz: t.vehicles?.spz ?? "", driver_name: t.users ? `${t.users.first_name} ${t.users.last_name}`.trim() : "", route_from: t.route_from, route_to: t.route_to, start_km: t.start_km, end_km: t.end_km, distance: t.distance ?? t.end_km - t.start_km, is_business: t.is_business, notes: t.notes ?? undefined, })); const totals = trips.reduce( (acc, t) => ({ total: acc.total + (t.distance || 0), business: acc.business + (t.is_business ? t.distance || 0 : 0), count: acc.count + 1, }), { total: 0, business: 0, count: 0 }, ); if (!hasPermission("trips.history")) return ; const getMonthName = (monthStr: string): string => { const [yearStr, monthNum] = monthStr.split("-"); const date = new Date(parseInt(yearStr), parseInt(monthNum) - 1); return date.toLocaleDateString("cs-CZ", { month: "long", year: "numeric" }); }; const columns: DataColumn[] = [ { key: "trip_date", header: "Datum", width: "11%", mono: true, render: (trip) => formatDate(trip.trip_date), }, { key: "vehicle", header: "Vozidlo", width: "10%", render: (trip) => , }, { key: "driver", header: "Řidič", width: "14%", render: (trip) => ( {trip.driver_name} ), }, { key: "route", header: "Trasa", width: "18%", render: (trip) => ( {trip.route_from} → {trip.route_to} ), }, { key: "km", header: "Stav km", width: "13%", mono: true, render: (trip) => ( {formatKm(trip.start_km)} - {formatKm(trip.end_km)} ), }, { key: "distance", header: "Vzdálenost", width: "10%", mono: true, bold: true, render: (trip) => `${formatKm(trip.distance)} km`, }, { key: "type", header: "Typ", width: "10%", render: (trip) => ( ), }, { key: "notes", header: "Poznámka", width: "14%", render: (trip) => ( {trip.notes || "—"} ), }, ]; return ( {/* Filters */} setMonth(val)} />