'use client'; import { useState, useEffect } from 'react'; import { useParams } from 'next/navigation'; import Link from 'next/link'; import { useLanguage } from '@/context/LanguageContext'; import { ticketsApi, Ticket } from '@/lib/api'; import Card from '@/components/ui/Card'; import Button from '@/components/ui/Button'; import { CheckCircleIcon, ClockIcon, XCircleIcon, TicketIcon, ArrowPathIcon, ArrowDownTrayIcon, } from '@heroicons/react/24/outline'; export default function BookingSuccessPage() { const params = useParams(); const { t, locale } = useLanguage(); const [ticket, setTicket] = useState(null); const [loading, setLoading] = useState(true); const [polling, setPolling] = useState(false); const ticketId = params.ticketId as string; const checkPaymentStatus = async () => { try { const { ticket: ticketData } = await ticketsApi.getById(ticketId); setTicket(ticketData); // If still pending, continue polling if (ticketData.status === 'pending' && ticketData.payment?.status === 'pending') { return false; // Not done yet } return true; // Done polling } catch (error) { console.error('Error checking payment status:', error); return true; // Stop polling on error } }; useEffect(() => { if (!ticketId) return; // Initial load checkPaymentStatus().finally(() => setLoading(false)); // Poll for payment status every 3 seconds setPolling(true); const interval = setInterval(async () => { const isDone = await checkPaymentStatus(); if (isDone) { setPolling(false); clearInterval(interval); } }, 3000); // Stop polling after 5 minutes const timeout = setTimeout(() => { setPolling(false); clearInterval(interval); }, 5 * 60 * 1000); return () => { clearInterval(interval); clearTimeout(timeout); }; }, [ticketId]); const formatDate = (dateStr: string) => { return new Date(dateStr).toLocaleDateString(locale === 'es' ? 'es-ES' : 'en-US', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', }); }; const formatTime = (dateStr: string) => { return new Date(dateStr).toLocaleTimeString(locale === 'es' ? 'es-ES' : 'en-US', { hour: '2-digit', minute: '2-digit', }); }; if (loading) { return (

{locale === 'es' ? 'Verificando pago...' : 'Verifying payment...'}

); } if (!ticket) { return (

{locale === 'es' ? 'Reserva no encontrada' : 'Booking not found'}

{locale === 'es' ? 'No pudimos encontrar tu reserva. Por favor, contacta con soporte.' : 'We could not find your booking. Please contact support.'}

); } const isPaid = ticket.status === 'confirmed' || ticket.payment?.status === 'paid'; const isPending = ticket.status === 'pending' && ticket.payment?.status === 'pending'; const isFailed = ticket.payment?.status === 'failed'; return (
{/* Status Icon */} {isPaid ? (
) : isPending ? (
) : (
)} {/* Title */}

{isPaid ? (locale === 'es' ? '¡Pago Confirmado!' : 'Payment Confirmed!') : isPending ? (locale === 'es' ? 'Esperando Pago...' : 'Waiting for Payment...') : (locale === 'es' ? 'Pago Fallido' : 'Payment Failed') }

{isPaid ? (locale === 'es' ? 'Tu reserva está confirmada. ¡Te esperamos!' : 'Your booking is confirmed. See you there!') : isPending ? (locale === 'es' ? 'Estamos verificando tu pago. Esto puede tomar unos segundos.' : 'We are verifying your payment. This may take a few seconds.') : (locale === 'es' ? 'Hubo un problema con tu pago. Por favor, intenta de nuevo.' : 'There was an issue with your payment. Please try again.') }

{/* Polling indicator */} {polling && isPending && (
{locale === 'es' ? 'Verificando...' : 'Checking...'}
)} {/* Ticket Details */}
{ticket.qrCode}
{ticket.event && ( <>

{locale === 'es' ? 'Evento' : 'Event'}: {ticket.event.title}

{locale === 'es' ? 'Fecha' : 'Date'}: {formatDate(ticket.event.startDatetime)}

{locale === 'es' ? 'Hora' : 'Time'}: {formatTime(ticket.event.startDatetime)}

{locale === 'es' ? 'Ubicación' : 'Location'}: {ticket.event.location}

)}
{/* Status Badge */}
{isPaid ? (locale === 'es' ? 'Confirmado' : 'Confirmed') : isPending ? (locale === 'es' ? 'Pendiente de Pago' : 'Pending Payment') : (locale === 'es' ? 'Pago Fallido' : 'Payment Failed') }
{/* Email note */} {isPaid && (

{locale === 'es' ? 'Un correo de confirmación ha sido enviado a tu bandeja de entrada.' : 'A confirmation email has been sent to your inbox.'}

)} {/* Download Ticket Button */} {isPaid && ( )} {/* Actions */}
); }