"use client"; import React, { Suspense } from "react"; import { Navbar } from "@/components/layout/Navbar"; import { Footer } from "@/components/layout/Footer"; import { useSearchParams, useRouter } from "next/navigation"; import { useAuth } from "@/hooks/useAuth"; import { CheckCircle2 } from "lucide-react"; import { errorMessage } from "@/lib/utils"; import type { Registration } from "@/types"; type FormField = { id: string; type: 'yes_no'|'text'|'date'|'numeric'|'statement'|'paragraph'|'rating'|'multiple_choice'; label: string; isRequired?: boolean; helpText?: string|null; options?: unknown }; function RegistrationSuccessContent() { const search = useSearchParams(); const router = useRouter(); const { token } = useAuth(); const registrationId = search.get("registrationId") || search.get("id"); const fallbackTotalParam = search.get("totalDue"); const fallbackTotalDue = React.useMemo(() => { const n = fallbackTotalParam ? Number(fallbackTotalParam) : 0; return isNaN(n) ? 0 : n; }, [fallbackTotalParam]); // Optional donation picked on the register form; added to the online checkout as its own line. const donationParam = search.get("donation"); const donation = React.useMemo(() => { const n = donationParam ? Number(donationParam) : 0; return isNaN(n) || n < 15 ? 0 : Math.round(n * 100) / 100; }, [donationParam]); const [reg, setReg] = React.useState(null); const [form, setForm] = React.useState<{ isRequired: boolean; fields: FormField[] } | null>(null); const [error, setError] = React.useState(null); const [payLoading, setPayLoading] = React.useState(false); React.useEffect(() => { (async () => { setError(null); if (!registrationId) return; try { // Try to fetch registration and event form const r = token ? await (await import('@/lib/api')).apiFetch(`/api/registrations/${encodeURIComponent(registrationId)}`, { authToken: token }) : null; if (r) { setReg(r); const ev = await (await import('@/lib/api')).apiFetch<{ form?: { isRequired: boolean; fields: FormField[] } | null }>(`/api/events/${encodeURIComponent(r.eventId)}`); if (ev?.form) setForm(ev.form); } } catch (e) { setError(errorMessage(e) || 'Failed to load details'); } })(); }, [registrationId, token]); const totalDue = React.useMemo(() => { if (!reg) return 0; // Backend attaches a tranche-aware totalDueComputed (exact even when a line spans // multiple early-bird prices) — fall back to the old client-side estimate otherwise. if (reg.totalDueComputed !== null && reg.totalDueComputed !== undefined) return reg.totalDueComputed; try { return (reg.registrationOptions || []).reduce((s, ro) => { const unit = (ro.priceSnapshot !== null && ro.priceSnapshot !== undefined) ? Number(ro.priceSnapshot) : (ro.variant?.price ?? ro.eventOption?.price ?? 0); return s + unit * (ro.quantity || 0); }, 0); } catch { return 0; } }, [reg]); React.useEffect(() => { // If free registration, don't offer online payment and redirect to dashboard after a short delay if (reg && totalDue === 0) { // attempt ticket generation if status is paid (may fail if required forms aren't completed) (async () => { try { if (token && reg?.id) { await (await import('@/lib/api')).apiFetch('/api/tickets/generate', { method: 'POST', authToken: token, body: { registrationId: reg.id } }); } } catch {} setTimeout(() => router.replace('/dashboard'), 600); })(); } }, [reg, totalDue, router, token]); const goPay = async () => { if (!registrationId) return; if (!token) { setError('Please login to pay.'); return; } try { setError(null); setPayLoading(true); const { createFullPaymentCheckout } = await import('@/lib/api'); const res = await createFullPaymentCheckout(token, registrationId, donation); if (res.priceUpdated) { // Early-bird price changed since registration — surface the new total and // reload the registration so the displayed totalDue reflects it, instead of redirecting. setError(`${res.message || 'Pricing has changed.'} New total: R ${(res.newTotal ?? 0).toFixed(2)}. Please try again.`); try { const r = await (await import('@/lib/api')).apiFetch(`/api/registrations/${encodeURIComponent(registrationId)}`, { authToken: token }); if (r) setReg(r); } catch {} return; } if (!res.redirectUrl) { setError('Failed to create checkout'); return; } window.location.href = res.redirectUrl; } catch (e) { setError(errorMessage(e) || 'Failed to create checkout'); } finally { setPayLoading(false); } }; const goDashboard = () => { router.push("/dashboard/user"); }; return (

Registration successful

Thank you! Your registration has been created{registrationId ? ` (#${registrationId.slice(0,8)})` : ""}.

{form?.isRequired && reg ? (
This event requires attendee details.
Please complete one form per main ticket to receive tickets. You can also do this later from your dashboard, but tickets cannot be generated until completed.
) : (totalDue > 0 || (!reg && fallbackTotalDue > 0)) ? ( <> {donation > 0 && (

Thank you for adding a donation of R{donation.toFixed(2)}. It's included when you pay now.

)}

Would you like to pay online now?

) : null}
{(totalDue > 0 || (!reg && fallbackTotalDue > 0)) && ( )}
{error &&

{error}

} {!registrationId && (

Missing registration reference. You can still go to your Dashboard to view registrations.

)}
); } export default function RegistrationSuccessPage() { return ( Loading...}> ); }