import { useState } from "react"; import { useNavigate } from "react-router-dom"; import { useQuery } from "@tanstack/react-query"; import { useAuth } from "../context/AuthContext"; import Forbidden from "../components/Forbidden"; import { motion } from "framer-motion"; import Box from "@mui/material/Box"; import useDebounce from "../hooks/useDebounce"; import { usePaginatedQuery } from "../hooks/usePaginatedQuery"; import { formatDate } from "../utils/formatters"; import { warehouseReceiptListOptions, warehouseSupplierListOptions, type WarehouseReceipt, } from "../lib/queries/warehouse"; import { Button, Card, DataTable, Pagination, TextField, Select, DateField, StatusChip, PageHeader, FilterBar, EmptyState, LoadingState, type DataColumn, } from "../ui"; const PER_PAGE = 20; const STATUS_COLOR: Record< string, "warning" | "success" | "error" | "info" | "default" > = { DRAFT: "warning", CONFIRMED: "success", CANCELLED: "error", }; const STATUS_LABEL: Record = { DRAFT: "Návrh", CONFIRMED: "Potvrzeno", CANCELLED: "Zrušeno", }; const PlusIcon = ( ); export default function WarehouseReceipts() { const { hasPermission } = useAuth(); const navigate = useNavigate(); const [page, setPage] = useState(1); const [search, setSearch] = useState(""); const [statusFilter, setStatusFilter] = useState(""); const [supplierId, setSupplierId] = useState(""); const [dateFrom, setDateFrom] = useState(""); const [dateTo, setDateTo] = useState(""); const debouncedSearch = useDebounce(search, 300); const { data: suppliersData } = useQuery( warehouseSupplierListOptions({ perPage: 100 }), ); const suppliers = suppliersData?.data ?? []; const { items, pagination, isPending, isFetching } = usePaginatedQuery( warehouseReceiptListOptions({ search: debouncedSearch || undefined, page, perPage: PER_PAGE, status: statusFilter || undefined, supplier_id: supplierId ? Number(supplierId) : undefined, date_from: dateFrom || undefined, date_to: dateTo || undefined, }), ); if (!hasPermission("warehouse.view")) return ; const canOperate = hasPermission("warehouse.operate"); const resetFilters = () => { setSearch(""); setStatusFilter(""); setSupplierId(""); setDateFrom(""); setDateTo(""); setPage(1); }; const hasActiveFilters = !!(statusFilter || supplierId || dateFrom || dateTo); if (isPending) { return ; } const total = pagination?.total ?? items.length; const subtitle = `${total} ${ total === 1 ? "doklad" : total >= 2 && total <= 4 ? "doklady" : "dokladů" }`; const columns: DataColumn[] = [ { key: "receipt_number", header: "Číslo dokladu", width: "15%", mono: true, bold: true, render: (r) => r.receipt_number || "—", }, { key: "supplier", header: "Dodavatel", width: "22%", render: (r) => r.supplier?.name || "—", }, { key: "status", header: "Stav", width: "13%", render: (r) => ( ), }, { key: "delivery_note_number", header: "Dodací list", width: "18%", mono: true, render: (r) => r.delivery_note_number || "—", }, { key: "created_at", header: "Vytvořeno", width: "18%", mono: true, render: (r) => formatDate(r.created_at), }, { key: "items_count", header: "Položek", width: "14%", mono: true, render: (r) => String(r._count?.items ?? 0), }, ]; return ( navigate("/warehouse/receipts/new")} > Nový doklad ) : undefined } /> { setSearch(e.target.value); setPage(1); }} placeholder="Hledat podle čísla dokladu..." fullWidth /> { setSupplierId(val === "" ? "" : Number(val)); setPage(1); }} options={[ { value: "", label: "Všichni dodavatelé" }, ...suppliers.map((s) => ({ value: String(s.id), label: s.name, })), ]} /> )} { setDateFrom(val); setPage(1); }} label="Od" /> { setDateTo(val); setPage(1); }} label="Do" /> {hasActiveFilters && ( )} columns={columns} rows={items} rowKey={(r) => r.id} onRowClick={(r) => navigate(`/warehouse/receipts/${r.id}`)} empty={ search || hasActiveFilters ? ( ) : ( navigate("/warehouse/receipts/new")} > Vytvořit první doklad ) : undefined } /> ) } /> ); }