import React, { useState, useEffect } from "react"; import LoadingSpinner from "../LoadingSpinner"; import { env } from "../../src/config/env"; import { authService } from "../../src/services/auth.service"; import { formatCurrency } from "../../utils/formatters"; import NoData from "../NoData"; import { Input } from "../ui/input"; import { Label } from "../ui/label"; import { Button } from "../ui/button"; import { CustomAutocomplete } from "../ui/autocomplete"; import { DateInput } from "../ui/date-input"; import { DataGridPremium, GridColDef } from "@mui/x-data-grid-premium"; import "../../lib/mui-license"; import { Box } from "@mui/material"; interface ProductOrder { date: string; orderId: number; invoice?: any; customerId: number; customer: string; seller: string; productId: number; product: string; package: string; quantity: number; color?: string; local?: string; deliveryType: string; itemId: string; } interface Store { id: string; shortName: string; name: string; } interface ProductFilterOption { id: string; option: string; } const ProductsSoldView: React.FC = () => { const [products, setProducts] = useState([]); const [stores, setStores] = useState([]); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); // Filtros const [selectedStore, setSelectedStore] = useState(""); const [startDate, setStartDate] = useState(""); const [endDate, setEndDate] = useState(""); const [orderId, setOrderId] = useState(""); const [document, setDocument] = useState(""); const [customerName, setCustomerName] = useState(""); const [productFilterType, setProductFilterType] = useState(""); const [productText, setProductText] = useState(""); // Opções de filtro de produto const productFilterOptions: ProductFilterOption[] = [ { id: "ID", option: "Código" }, { id: "EAN", option: "Ean" }, { id: "TEXT", option: "Descrição" }, { id: "PARTNER", option: "Código Fábrica" }, ]; useEffect(() => { fetchStores(); }, []); const fetchStores = async () => { try { const token = authService.getToken(); const apiUrl = env.API_URL.replace(/\/$/, ""); const response = await fetch(`${apiUrl}/lists/store`, { method: "GET", headers: { "Content-Type": "application/json", ...(token && { Authorization: `Basic ${token}` }), }, }); if (response.ok) { const data = await response.json(); setStores(data); } } catch (err) { console.error("Erro ao buscar filiais:", err); } }; const handleSearch = async () => { setLoading(true); setError(null); try { const token = authService.getToken(); const apiUrl = env.API_URL.replace(/\/$/, ""); // Seguindo o padrão do Angular: sellerId sempre 0 let sellerId = 0; if (authService.isManager()) { sellerId = 0; } // Se não selecionou filial, usar '99' como padrão (seguindo o Angular) const store = selectedStore || "99"; const params = new URLSearchParams({ "x-store": store, initialDate: startDate || "", finalDate: endDate || "", document: document || "", name: customerName.toUpperCase() || "", sellerId: sellerId.toString(), idOrder: orderId || "", typeFilterProduct: productFilterType || "", productText: productText || "", }); const response = await fetch( `${apiUrl}/order/products-order?${params.toString()}`, { method: "GET", headers: { Accept: "application/json, text/plain, */*", "Content-Type": "application/json", ...(token && { Authorization: `Basic ${token}` }), }, } ); if (!response.ok) { const errorData = await response.json().catch(() => ({})); throw new Error( errorData.message || `Erro ao buscar produtos vendidos: ${response.statusText}` ); } const data = await response.json(); setProducts(data || []); } catch (err) { console.error("Erro ao buscar produtos vendidos:", err); setError( err instanceof Error ? err.message : "Erro ao buscar produtos vendidos. Tente novamente." ); } finally { setLoading(false); } }; const handleClear = () => { setSelectedStore(""); setStartDate(""); setEndDate(""); setOrderId(""); setDocument(""); setCustomerName(""); setProductFilterType(""); setProductText(""); setProducts([]); setError(null); }; const formatDate = (dateString: string): string => { if (!dateString) return ""; const date = new Date(dateString); return date.toLocaleDateString("pt-BR"); }; // Definir colunas do DataGrid const columns: GridColDef[] = [ { field: "date", headerName: "Data", width: 120, valueFormatter: (value) => formatDate(value as string), }, { field: "orderId", headerName: "N.Pedido", width: 130, headerAlign: "left", }, { field: "customerId", headerName: "Cód.Cliente", width: 120, headerAlign: "left", }, { field: "customer", headerName: "Cliente", width: 250, flex: 1, }, { field: "seller", headerName: "Vendedor", width: 150, }, { field: "productId", headerName: "Cód.Produto", width: 130, headerAlign: "left", }, { field: "product", headerName: "Produto", width: 300, flex: 1, }, { field: "package", headerName: "Embalagem", width: 120, }, { field: "quantity", headerName: "Quantidade", width: 120, headerAlign: "right", align: "right", }, { field: "deliveryType", headerName: "Tipo Entrega", width: 150, }, ]; return (
{/* Header */}

Consulta vendas por produto

{/* Filtros */}
{/* Filial de venda */}
({ value: store.id, label: store.shortName, }))} value={selectedStore} onValueChange={setSelectedStore} placeholder="Selecione a filial de venda..." />
{/* Número do pedido */}
setOrderId(e.target.value)} placeholder="Informe o número do pedido" />
{/* Data do pedido - Início */}
setStartDate(e.target.value)} />
{/* Data do pedido - Fim */}
setEndDate(e.target.value)} />
{/* CPF/CNPJ */}
setDocument(e.target.value)} placeholder="Informe o CPF ou CNPJ do cliente" />
{/* Nome do cliente */}
setCustomerName(e.target.value)} placeholder="Informe o nome ou razão social do cliente" />
{/* Tipo de pesquisa do produto */}
({ value: option.id, label: option.option, }))} value={productFilterType} onValueChange={setProductFilterType} placeholder="Selecione o tipo de pesquisa..." />
{/* Produto */}
setProductText(e.target.value)} placeholder="Informe o texto para pesquisa do produto" />
{/* Botões de Ação */}
{/* Tabela de Produtos Vendidos */} {error && (

{error}

)} {loading && (
)} {!loading && products.length > 0 && (

Produtos encontrados: {products.length}

`${row.orderId}-${row.itemId}`} disableRowSelectionOnClick hideFooter sx={{ border: "none", "& .MuiDataGrid-columnHeaders": { backgroundColor: "#f8fafc", borderBottom: "1px solid #e2e8f0", "& .MuiDataGrid-columnHeader": { fontSize: "9px", fontWeight: 900, color: "#94a3b8", textTransform: "uppercase", letterSpacing: "0.2em", padding: "12px 16px", "& .MuiDataGrid-columnHeaderTitle": { fontWeight: 900, }, }, }, "& .MuiDataGrid-row": { "&:hover": { backgroundColor: "#f8fafc", }, }, "& .MuiDataGrid-cell": { fontSize: "12px", color: "#475569", padding: "16px", borderBottom: "1px solid #f1f5f9", "&:focus": { outline: "none", }, "&:focus-within": { outline: "none", }, }, "& .MuiDataGrid-cell[data-field='orderId']": { fontWeight: 700, color: "#0f172a", }, "& .MuiDataGrid-cell[data-field='productId']": { fontWeight: 700, color: "#0f172a", }, "& .MuiDataGrid-cell[data-field='quantity']": { fontWeight: 700, color: "#0f172a", }, "& .MuiDataGrid-cell[data-field='date']": { color: "#64748b", }, "& .MuiDataGrid-cell[data-field='customerId']": { color: "#64748b", }, "& .MuiDataGrid-cell[data-field='customer']": { color: "#64748b", }, "& .MuiDataGrid-virtualScroller": { overflowY: "auto", }, "& .MuiDataGrid-virtualScrollerContent": { height: "auto !important", }, }} />
)} {!loading && products.length === 0 && !error && (
)}
); }; export default ProductsSoldView;