"use client"; import { RiChat3Line, RiDeleteBin5Line, RiFileList2Line, RiPencilLine, } from "@remixicon/react"; import Image from "next/image"; import { useMemo } from "react"; import { Card, CardContent, CardFooter, CardHeader, } from "@/components/ui/card"; import { Progress } from "@/components/ui/progress"; import { Tooltip, TooltipContent, TooltipTrigger, } from "@/components/ui/tooltip"; import { cn } from "@/lib/utils/ui"; import MoneyValues from "../money-values"; interface CardItemProps { name: string; brand: string; status: string; closingDay: string; dueDay: string; limit: number | null; limitInUse?: number | null; limitAvailable?: number | null; contaName: string; logo?: string | null; note?: string | null; onEdit?: () => void; onInvoice?: () => void; onRemove?: () => void; } const BRAND_ASSETS: Record = { visa: "/bandeiras/visa.svg", mastercard: "/bandeiras/mastercard.svg", amex: "/bandeiras/amex.svg", american: "/bandeiras/amex.svg", elo: "/bandeiras/elo.svg", hipercard: "/bandeiras/hipercard.svg", hiper: "/bandeiras/hipercard.svg", }; const resolveBrandAsset = (brand: string) => { const normalized = brand.trim().toLowerCase(); const match = ( Object.keys(BRAND_ASSETS) as Array ).find((entry) => normalized.includes(entry)); return match ? BRAND_ASSETS[match] : null; }; const formatDay = (value: string) => value.padStart(2, "0"); export function CardItem({ name, brand, status, closingDay, dueDay, limit, limitInUse, limitAvailable, contaName: _contaName, logo, note, onEdit, onInvoice, onRemove, }: CardItemProps) { void _contaName; const limitTotal = limit ?? null; const used = limitInUse ?? (limitTotal !== null && limitAvailable !== null ? Math.max(limitTotal - limitAvailable, 0) : limitTotal !== null ? 0 : null); const available = limitAvailable ?? (limitTotal !== null && used !== null ? Math.max(limitTotal - used, 0) : null); const usagePercent = limitTotal && limitTotal > 0 && used !== null ? Math.min(Math.max((used / limitTotal) * 100, 0), 100) : 0; const logoPath = useMemo(() => { if (!logo) { return null; } if ( logo.startsWith("http://") || logo.startsWith("https://") || logo.startsWith("data:") ) { return logo; } return logo.startsWith("/") ? logo : `/logos/${logo}`; }, [logo]); const brandAsset = useMemo(() => resolveBrandAsset(brand), [brand]); const isInactive = useMemo( () => status?.toLowerCase() === "inativo", [status], ); const metrics = useMemo(() => { if (limitTotal === null) return null; return [ { label: "Limite Total", value: limitTotal }, { label: "Em uso", value: used }, { label: "Disponível", value: available }, ]; }, [available, limitTotal, used]); const actions = useMemo( () => [ { label: "editar", icon: , onClick: onEdit, className: "text-primary", }, { label: "ver fatura", icon: , onClick: onInvoice, className: "text-primary", }, { label: "remover", icon: , onClick: onRemove, className: "text-destructive", }, ], [onEdit, onInvoice, onRemove], ); return (
{logoPath ? (
{`Logo
) : null}

{name}

{note ? ( {note} ) : null}
{status ? ( {status} ) : null}
{brandAsset ? (
{`Bandeira
) : ( {brand} )}
Fecha dia{" "} {formatDay(closingDay)} Vence dia{" "} {formatDay(dueDay)}
{metrics ? ( <>

{metrics[0].label}

{metrics[1].label}

{metrics[2].label}
) : (

Ainda não há limite registrado para este cartão.

)}
{actions.map(({ label, icon, onClick, className }) => ( ))}
); }