"use client"; import { Card, CardContent, CardHeader, CardTitle, } from "@/src/components/ui/card"; import { Button } from "@/src/components/ui/button"; import { Search, RefreshCw, ChevronUp, ChevronDown } from "lucide-react"; import { OrdersTable } from "@/src/components/orders/OrdersTable"; import { ErrorMessage } from "@/src/components/ui/error-message"; import { useStores } from "@/src/hooks/useStores"; import { useOrderSearch } from "@/src/hooks/useOrderSearch"; import { FilterInput } from "@/src/components/orders/FilterInput"; import { FilterSelect } from "@/src/components/orders/FilterSelect"; import { STATUS_OPTIONS } from "@/src/constants/status-options"; import { DELIVERY_STATUS_OPTIONS } from "@/src/constants/delivery-status-options"; import { Spinner } from "@/src/components/ui/spinner"; import { DateRangeFilter } from "@/src/components/orders/DateRangeFilter"; import { MultiFilterSelect } from "@/src/components/orders/MultiFilterSelect"; import { CustomerSearchInput } from "@/src/components/orders/CustomerSearchInput"; import { useCallback, useState, useEffect } from "react"; import { User, useAuthValidation } from "../../../hooks/useAuthValidation"; import { SellerSearchInput } from "@/src/components/orders/SellerSearchInput"; /** * Página de Consulta de Pedidos * Permite ao usuário pesquisar pedidos com base em vários filtros * * @returns Componente da página de consulta de pedidos */ export default function FindOrdersPage() { const { user, isAuthenticated, isLoading: authLoading, error: authError, decodedToken, } = useAuthValidation({ authServiceUrl: process.env.NEXT_PUBLIC_AUTH_SERVICE_URL!, token: "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMzg1IiwidXNlcm5hbWUiOiJKT0VMU09OLlIiLCJyb2xlIjpudWxsLCJpYXQiOjE3NDg4MjQzNDEsImV4cCI6MTc1MzIzMDc0MX0.H4nRp72NTEzHBKij67BgW4NO8Pot9AqPKlaynne694c", autoRedirect: false, }); const [isFiltersExpanded, setIsFiltersExpanded] = useState(true); const { storeOptions, loading: loadingStores, stores } = useStores(true); const [usePreloadClients, setUsePreloadClients] = useState(false); const [status, setStatus] = useState(""); const [leadtime, setLeadtime] = useState(""); const { orders, loading, dateError, hasSearched, selectedOrderId, orderItems, cutitens, loadingItems, itemsError, currentPage, totalPages, currentOrders, indexOfFirstOrder, indexOfLastOrder, searchParams, setSearchParams, handleSearch, handleRowClick, goToNextPage, goToPreviousPage, goToPage, loadMoreOrders, visibleOrdersCount, handleInputChange, handleCustomerId, handleCustomerFilter, handleCustomerSelect, handleMultiInputChange, } = useOrderSearch(8); /** * Limpa todos os filtros de pesquisa * @returns {void} */ const handleClearFilters = useCallback(() => { setSearchParams({}); }, [setSearchParams]); /** * Alterna o modo de pré-carregamento de clientes * Quando ativado, carrega clientes antecipadamente para melhorar o desempenho da busca * @returns {void} */ const togglePreloadMode = useCallback(() => { setUsePreloadClients((prev) => !prev); }, []); return (
setIsFiltersExpanded(!isFiltersExpanded)} > Consulta de Pedidos {isFiltersExpanded && (
handleInputChange("codfilial", value) } placeholder="Selecione a filial" disabled={loadingStores} options={storeOptions} loading={loadingStores} loadingText="Carregando filiais..." className="w-full" aria-label="Filial de Venda" />
handleInputChange("orderId", value)} className="w-full" aria-label="Número do Pedido" />
handleInputChange("invoiceNumber", value) } className="w-full" aria-label="Número da Nota Fiscal" />
handleInputChange("shippimentId", value) } className="w-full" aria-label="Carregamento" />
handleMultiInputChange("type", values) } options={[ { label: "TV1 - Retira Imediata", value: "1" }, { label: "TV7 - Faturamento", value: "7" }, { label: "TV8 - Entrega", value: "8" }, { label: "TV10 - Transferência", value: "10" }, ]} className="w-full" aria-label="Tipo de Venda" />
handleMultiInputChange("deliveryType", values) } options={[ { label: "Retira Imediata", value: "RI" }, { label: "Entrega", value: "EN" }, { label: "Entrega Futura", value: "EF" }, { label: "Retira Posterior", value: "RP" }, ]} className="w-full" aria-label="Tipo de Entrega" />
handleMultiInputChange("status", values) } placeholder=" Situação" options={STATUS_OPTIONS} className="w-full" aria-label="Situação do Pedido" />
handleInputChange("stockId", value) } placeholder="Filial de estoque" options={storeOptions} loading={loadingStores} loadingText="Carregando filiais..." className="w-full" aria-label="Filial de Estoque" />
handleInputChange("sellerName", value)} onSelect={(seller) => { if (seller) { if (seller.code) { handleInputChange("sellerId", seller.code); } else { handleInputChange("sellerId", seller.id.toString()); } handleInputChange("sellerName", seller.name); } else { handleInputChange("sellerId", ""); handleInputChange("sellerName", ""); } }} selectedSeller={searchParams.sellerName ? { id: parseInt(String(searchParams.sellerId || "0"), 10) || 0, name: searchParams.sellerName, code: (searchParams.sellerId || "").toString() } : undefined} aria-label="Buscar Vendedor" />
handleInputChange("createDateIni", value) } onEndChange={(value) => handleInputChange("createDateEnd", value) } aria-label="Período" />
{dateError && }
)}
{loading ? (

Buscando pedidos...

) : hasSearched ? ( ) : null}
); }