import { useState } from "react"; import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; import { useNavigate } from "react-router-dom"; import { Box, Typography, Button, Paper, Chip, IconButton, Tooltip, } from "@mui/material"; import { DataGrid, GridColDef, GridRenderCellParams, GridToolbar, } from "@mui/x-data-grid"; import { Add as AddIcon, Visibility as ViewIcon, PlayArrow as StartIcon, Cancel as CancelIcon, } from "@mui/icons-material"; import { inventoryService } from "../services/warehouseService"; import { InventoryCountDto, InventoryStatus } from "../types"; import dayjs from "dayjs"; export default function InventoryListPage() { const navigate = useNavigate(); const queryClient = useQueryClient(); const [statusFilter] = useState( undefined ); const { data: inventories = [], isLoading } = useQuery({ queryKey: ["inventory-counts", statusFilter], queryFn: () => inventoryService.getAll(statusFilter), }); const cancelMutation = useMutation({ mutationFn: (id: number) => inventoryService.cancel(id), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["inventory-counts"] }); }, }); const handleCreate = () => { navigate("/warehouse/inventory/new"); }; const handleView = (id: number) => { navigate(`/warehouse/inventory/${id}`); }; const handleStart = (id: number) => { navigate(`/warehouse/inventory/${id}/count`); }; const getStatusChip = (status: InventoryStatus) => { switch (status) { case InventoryStatus.Draft: return ; case InventoryStatus.InProgress: return ; case InventoryStatus.Completed: return ; case InventoryStatus.Confirmed: return ; case InventoryStatus.Cancelled: return ; default: return ; } }; const columns: GridColDef[] = [ { field: "code", headerName: "Codice", width: 120 }, { field: "description", headerName: "Descrizione", flex: 1, minWidth: 200 }, { field: "inventoryDate", headerName: "Data Inventario", width: 150, valueFormatter: (value) => value ? dayjs(value).format("DD/MM/YYYY") : "", }, { field: "warehouseName", headerName: "Magazzino", width: 180 }, { field: "categoryName", headerName: "Categoria", width: 150 }, { field: "status", headerName: "Stato", width: 120, renderCell: (params: GridRenderCellParams) => getStatusChip(params.row.status), }, { field: "progress", headerName: "Progresso", width: 150, valueGetter: (_value, row) => { if (!row.lineCount) return "0%"; const percentage = Math.round( (row.countedLineCount / row.lineCount) * 100 ); return `${row.countedLineCount}/${row.lineCount} (${percentage}%)`; }, }, { field: "actions", headerName: "Azioni", width: 180, sortable: false, renderCell: (params: GridRenderCellParams) => ( handleView(params.row.id)}> {params.row.status === InventoryStatus.Draft && ( handleStart(params.row.id)} > )} {params.row.status === InventoryStatus.InProgress && ( handleStart(params.row.id)} > )} {params.row.status === InventoryStatus.Draft && ( { if (confirm("Sei sicuro di voler annullare questo inventario?")) { cancelMutation.mutate(params.row.id); } }} > )} ), }, ]; return ( Inventari Fisici ); }