"use client"; import { useState } from "react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Badge } from "@/components/ui/badge"; import { Heart, Coffee, Pizza, Loader2, Copy, Check } from "lucide-react"; import { Dialog, DialogContent, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import QRCode from "react-qr-code"; import { DonationTier, DonationResponse } from "@/types/donation"; interface DonationCardProps extends DonationTier {} export function DonationCard({ amount, title, description, icon, badge, highlighted, }: DonationCardProps) { const [customAmount, setCustomAmount] = useState(amount || 10); const [loading, setLoading] = useState(false); const [showPixModal, setShowPixModal] = useState(false); const [pixData, setPixData] = useState<{ code: string; qrCode: string; } | null>(null); const [copied, setCopied] = useState(false); const icons = { coffee: Coffee, pizza: Pizza, heart: Heart, }; const Icon = icons[icon]; const handleDonate = async () => { setLoading(true); try { const response = await fetch("/api/donations/create", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ amount: customAmount }), }); const data: DonationResponse = await response.json(); if (data.success && data.pixCode && data.pixQrCode) { setPixData({ code: data.pixCode, qrCode: data.pixQrCode, }); setShowPixModal(true); } else { console.error("Erro ao criar doação:", data.error); alert("Erro ao processar doação. Tente novamente."); } } catch (error) { console.error("Donation error:", error); alert("Erro ao processar doação. Tente novamente."); } finally { setLoading(false); } }; const handleCopyPixCode = async () => { if (pixData?.code) { try { await navigator.clipboard.writeText(pixData.code); setCopied(true); setTimeout(() => setCopied(false), 2000); } catch (error) { console.error("Erro ao copiar código PIX:", error); } } }; return ( <>
{badge && ( {badge} )}

{title}

{description}

{amount ? (
R$ {amount.toFixed(2)}
) : ( setCustomAmount(Number(e.target.value))} min={5} max={10000} step={5} className="text-center text-2xl font-bold" placeholder="R$ 10,00" /> )}
Escaneie o QR Code para Pagar {pixData && (

Ou copie o código PIX:

O pagamento expira em 30 minutos

)}
); }