"use client"; import { CheckCircle2, Clock, MessageSquare, ShoppingCart, XCircle, } from "lucide-react"; import { useEffect, useState } from "react"; import { Avatar, AvatarFallback } from "@/components/ui/avatar"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Skeleton } from "@/components/ui/skeleton"; import { IntentionDetailModal } from "./IntentionDetailModal"; interface PurchaseIntention { intention_id: number; buyer_id: number; payment_method: string; status: "pending" | "accepted" | "rejected" | "expired" | "completed"; buyer_message: string | null; seller_response: string | null; created_at: string; custom_offer_amount: number | null; custom_offer_currency: string | null; custom_offer_created_at: string | null; buyer?: { user_id: number; username: string; email: string; }; } interface MyAdsIntentionListProps { listingId: number; sellerId: number; } function formatTimeAgo(date: Date): string { const seconds = Math.floor((Date.now() - date.getTime()) / 1000); if (seconds < 60) return "há alguns segundos"; const minutes = Math.floor(seconds / 60); if (minutes < 60) return `há ${minutes} ${minutes === 1 ? "minuto" : "minutos"}`; const hours = Math.floor(minutes / 60); if (hours < 24) return `há ${hours} ${hours === 1 ? "hora" : "horas"}`; const days = Math.floor(hours / 24); return `há ${days} ${days === 1 ? "dia" : "dias"}`; } function getStatusBadge(status: string) { switch (status) { case "pending": return ( Pendente ); case "accepted": return ( Aceita ); case "rejected": return ( Rejeitada ); default: return {status}; } } function getPaymentMethodLabel(method: string): string { switch (method) { case "pix": return "PIX"; case "tibia_coins": return "Tibia Coins"; case "gold_coins": return "Gold Coins"; case "trade": return "Troca"; default: return method.toUpperCase(); } } function formatCustomOffer(intention: PurchaseIntention): string | null { if ( intention.custom_offer_amount === null || intention.custom_offer_amount === undefined || !intention.custom_offer_currency ) { return null; } const amount = intention.custom_offer_amount; switch (intention.custom_offer_currency) { case "pix": return `R$ ${amount.toFixed(2)}`; case "tibia_coins": return `${Math.round(amount).toLocaleString("pt-BR")} TC`; case "gold_coins": return `${Math.round(amount).toLocaleString("pt-BR")} GP`; default: return amount.toLocaleString("pt-BR"); } } export function MyAdsIntentionList({ listingId, sellerId, }: MyAdsIntentionListProps) { const [intentions, setIntentions] = useState([]); const [loading, setLoading] = useState(true); const [selectedIntention, setSelectedIntention] = useState(null); const [filter, setFilter] = useState< "all" | "pending" | "accepted" | "rejected" >("all"); useEffect(() => { loadIntentions(); }, [loadIntentions]); const loadIntentions = async () => { setLoading(true); try { const params = new URLSearchParams(); if (filter !== "all") { params.set("status", filter); } const res = await fetch( `/api/listings/${listingId}/intentions?${params.toString()}`, ); if (res.ok) { const data = await res.json(); if (data.success) { setIntentions(data.intentions || []); } } } catch (error) { console.error("Erro ao carregar intenções:", error); } finally { setLoading(false); } }; const pendingCount = intentions.filter((i) => i.status === "pending").length; if (loading) { return (
{Array.from({ length: 3 }).map((_, i) => ( ))}
); } if (intentions.length === 0) { return (
Nenhuma intenção de compra encontrada
); } return (
{/* Filtros */}
{/* Lista de intenções */}
{intentions.map((intention) => (
setSelectedIntention(intention)} >
{intention.buyer?.username.charAt(0).toUpperCase() || "?"}
{intention.buyer?.username || "Usuário"} {getStatusBadge(intention.status)}
{getPaymentMethodLabel(intention.payment_method)} • {formatTimeAgo(new Date(intention.created_at))}
{formatCustomOffer(intention) && (
Oferta: {formatCustomOffer(intention)} {intention.custom_offer_created_at && ( {new Date( intention.custom_offer_created_at, ).toLocaleString("pt-BR")} )}
)} {intention.buyer_message && (

{intention.buyer_message}

)}
))}
{/* Modal de detalhes */} {selectedIntention && ( { if (!open) setSelectedIntention(null); }} intention={selectedIntention} listingId={listingId} onUpdate={loadIntentions} /> )}
); }