"use client" import { useState } from "react" import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog" import { Button } from "@/components/ui/button" import { useToast } from "@/components/ui/use-toast" import { RefreshCw, ExternalLink, Copy } from "lucide-react" const statusColors: Record = { PENDING: "bg-yellow-100 text-yellow-800", RECEIVED: "bg-green-100 text-green-800", CONFIRMED: "bg-green-100 text-green-800", OVERDUE: "bg-red-100 text-red-800", REFUNDED: "bg-gray-100 text-gray-800", CANCELLED: "bg-red-100 text-red-800", } const statusLabel: Record = { PENDING: "Pendente", RECEIVED: "Pago", CONFIRMED: "Confirmado", OVERDUE: "Vencido", REFUNDED: "Estornado", CANCELLED: "Cancelado", } type Pedido = { id: string status: string valor_centavos: number metodo_pagamento: string | null asaas_payment_id: string | null asaas_invoice_url: string | null pix_copia_cola: string | null pix_qrcode_url: string | null paid_at: string | null created_at: string clientes: { nome: string email: string telefone: string | null cpf_cnpj: string | null } | null produtos: { nome: string tipo: string validade: string midia: string | null } | null } export function PedidoDetailModal({ pedido: pedidoInicial, open, onClose, }: { pedido: Pedido open: boolean onClose: () => void }) { const { toast } = useToast() const [pedido, setPedido] = useState(pedidoInicial) const [syncing, setSyncing] = useState(false) const fmt = (v: number) => (v / 100).toLocaleString("pt-BR", { style: "currency", currency: "BRL" }) async function syncStatus() { setSyncing(true) try { const res = await fetch(`/api/pedido/${pedido.id}/sync`, { method: "POST" }) const data = await res.json() if (!res.ok) throw new Error(data.error) setPedido((p) => ({ ...p, status: data.status })) toast({ title: `Status atualizado: ${statusLabel[data.status] ?? data.status}` }) } catch (err) { toast({ title: "Erro ao sincronizar", description: err instanceof Error ? err.message : "Tente novamente", variant: "destructive", }) } finally { setSyncing(false) } } function copiar(texto: string, label: string) { navigator.clipboard.writeText(texto) toast({ title: `${label} copiado!` }) } return ( Pedido {pedido.id.slice(0, 8).toUpperCase()} {statusLabel[pedido.status] ?? pedido.status}
{/* Cliente */}
{/* Produto */}
{/* Pagamento */}
{pedido.paid_at && ( )}
{/* Ações */}
{pedido.asaas_invoice_url && ( )} {pedido.pix_copia_cola && ( )} {pedido.asaas_payment_id && ( )}
) } function Section({ title, children }: { title: string; children: React.ReactNode }) { return (

{title}

{children}
) } function Row({ label, value, mono, highlight, }: { label: string value?: string | null mono?: boolean highlight?: boolean }) { return (
{label} {value ?? "—"}
) }