'use client'; import { useState } from 'react'; import Link from 'next/link'; import toast from 'react-hot-toast'; import { CheckCircleIcon } from '@heroicons/react/24/outline'; import Button from '@/components/ui/Button'; import { ticketsApi } from '@/lib/api'; import { useLanguage } from '@/context/LanguageContext'; import { pyg } from './helpers'; interface PayActionsProps { ticketId: string; /** Amount owed, used in the confirm copy. */ amount: number; currency?: string; /** Where the money is going — event name or account label. */ destination: string; locale: string; /** Called after the payment is successfully marked as sent. */ onPaid?: () => void; size?: 'sm' | 'md' | 'lg'; /** Stack the two buttons full-width (cards) vs inline (rows). */ layout?: 'stack' | 'inline'; className?: string; /** * The booking's spot was released after the hold threshold passed. Hides the * "Pay now" link (money was already sent) and labels the retry "Rebook". */ onHold?: boolean; } /** * The two unpaid-ticket actions used across Overview, Tickets and Payments: * • "Pay now" -> opens the existing manual-payment page (TPago / bank details) * • "I've paid" -> short confirm step, then marks the payment as sent * (moves the ticket to "Awaiting approval"). */ export default function PayActions({ ticketId, amount, currency = 'PYG', destination, locale, onPaid, size = 'sm', layout = 'stack', className, onHold = false, }: PayActionsProps) { const { t } = useLanguage(); const [confirming, setConfirming] = useState(false); const [marking, setMarking] = useState(false); const handleConfirm = async () => { setMarking(true); try { await ticketsApi.markPaymentSent(ticketId); toast.success(t('dashboard.payment.receivedConfirmSoon')); setConfirming(false); onPaid?.(); } catch (error: any) { // Idempotent backend: if already marked, treat as success. if (error?.message?.includes('already')) { setConfirming(false); onPaid?.(); return; } toast.error( error?.message || (locale === 'es' ? 'No se pudo marcar el pago' : 'Could not mark payment') ); } finally { setMarking(false); } }; const containerClass = layout === 'stack' ? 'flex flex-col gap-2' : 'flex flex-wrap gap-2'; const btnWidth = layout === 'stack' ? 'w-full' : ''; return ( <>
{onHold ? (locale === 'es' ? `Intentaremos reservar tu lugar de nuevo para ${destination}.` : `We'll try to re-reserve your spot for ${destination}.`) : (locale === 'es' ? `¿Ya enviaste los ${pyg(amount, currency)} para ${destination}?` : `Did you already send the ${pyg(amount, currency)} for ${destination}?`)}