"use client"; import { ExtraFieldInput, isExtraFieldType } from "@/components/shared/FormFieldExtras"; import React, { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { fetchManagedEvents, fetchEvent } from "@/lib/api"; import { AuthResponse, TwoFactorChallenge, isTwoFactorChallenge } from "@/lib/auth"; import { CouponCodeField, AppliedCoupon } from "@/components/shared/CouponCodeField"; import { BundleBuilder, BundleOffers, BundleSavings, useBundleQuote } from "@/components/shared/BundleDeals"; import { AttendeeNamesEditor, AttendeeEntries, TicketDelivery, attendeesPayload, consentDetailsMissing } from "@/components/shared/AttendeeNamesEditor"; import { errorMessage } from "@/lib/utils"; import type { Event, PublicBundle, Registration } from "@/types"; import { choosesSessions, isPickOption } from "@/lib/sessions"; import { SessionPicker } from "@/components/events/SessionPicker"; import { ModalOverlay } from "@/components/shared/ModalOverlay"; import { EventPicker } from "@/components/shared/EventPicker"; import { apiV2Data, apiV2 } from "@/lib/apiV2"; import { toAuthResponse } from "@/contexts/AuthContext"; // ─── Types ──────────────────────────────────────────────────────────────────── type EarlyBirdTier = { deadline: string; price: number; order?: number; variantId?: string | null }; type OptionVariant = { id: string; name: string; price: number | null; stockLimit?: number; availableCount?: number | null }; type EventOption = { id: string; name: string; price: number; isMainTicket?: boolean; earlyBirdTiers?: EarlyBirdTier[]; variants?: OptionVariant[]; }; type FormField = { id: string; type: string; label: string; isRequired: boolean; order: number; helpText?: string | null; options?: unknown; }; type EventForm = { id: string; isRequired: boolean; fields: FormField[]; }; type KioskEvent = { id: string; title: string; description?: string; startDate: string; endDate: string; picture?: string; eventOptions?: EventOption[]; form?: EventForm | null; allowNamedAttendees?: boolean; requiresParentalConsent?: boolean; collectGender?: boolean; }; type Screen = "setup" | "form" | "eventform" | "confirmation"; // ─── Helpers ────────────────────────────────────────────────────────────────── function effectiveUnit(opt: EventOption): number { const base = opt.price || 0; const tiers = (Array.isArray(opt.earlyBirdTiers) ? opt.earlyBirdTiers : []).filter(t => !t.variantId); if (tiers.length === 0) return base; const now = new Date(); const applicable = tiers .map((t) => ({ ...t, deadline: new Date(t.deadline) })) .filter((t) => now < t.deadline) .sort((a, b) => a.deadline.getTime() - b.deadline.getTime() || (a.order || 0) - (b.order || 0) || a.price - b.price); return applicable.length > 0 ? applicable[0].price : base; } function effectiveVariantUnit(opt: EventOption, variant: OptionVariant): number { const base = variant.price !== null && variant.price !== undefined ? variant.price : opt.price || 0; const allTiers = Array.isArray(opt.earlyBirdTiers) ? opt.earlyBirdTiers : []; const variantTiers = allTiers.filter(t => t.variantId === variant.id); const tiers = variantTiers.length > 0 ? variantTiers : allTiers.filter(t => !t.variantId); if (tiers.length === 0) return base; const now = new Date(); const applicable = tiers .map((t) => ({ ...t, deadline: new Date(t.deadline) })) .filter((t) => now < t.deadline) .sort((a, b) => a.deadline.getTime() - b.deadline.getTime() || (a.order || 0) - (b.order || 0) || a.price - b.price); return applicable.length > 0 ? applicable[0].price : base; } function fmtDate(dateStr: string) { try { return new Date(dateStr).toLocaleDateString(undefined, { weekday: "short", year: "numeric", month: "short", day: "numeric", }); } catch { return dateStr; } } function fmtCurrency(val: number) { return val === 0 ? "Free" : `R${val.toFixed(2)}`; } function prefLabel(pref: "email" | "whatsapp" | "both") { return pref === "whatsapp" ? "WhatsApp" : pref === "both" ? "Email & WhatsApp" : "Email"; } // Loose "does this look like a mobile number" check — covers 0821234567 (10, leading 0), // 821234567 (9, no leading 0), and 27821234567 / +27821234567 (11 digits, country code). function looksLikePhone(s: string): boolean { const digits = s.replace(/\D/g, ""); return digits.length >= 9 && digits.length <= 11; } // Show/hide toggle rendered inside a password input — absolutely positioned on its // right edge, so callers must wrap the input in a `relative` container and add // enough right padding (`pr-12`) for it not to overlap the typed text. function PasswordToggleButton({ shown, onToggle }: { shown: boolean; onToggle: () => void }) { return ( ); } // ─── Kiosk Page ─────────────────────────────────────────────────────────────── export default function SelfServicePage() { const [screen, setScreen] = useState("setup"); // ── Setup state ───────────────────────────────────────────────── const [setupEmail, setSetupEmail] = useState(""); const [setupPassword, setSetupPassword] = useState(""); const [showSetupPassword, setShowSetupPassword] = useState(false); const [setupLoading, setSetupLoading] = useState(false); const [setupError, setSetupError] = useState(null); const [supervisorToken, setSupervisorToken] = useState(null); const [kioskCoupon, setKioskCoupon] = useState(null); const [kioskBundles, setKioskBundles] = useState([]); const [namingOthers, setNamingOthers] = useState(false); const [attendeeEntries, setAttendeeEntries] = useState({}); const [delivery, setDelivery] = useState("payer"); // Set while a supervisor with two-factor on still has to enter their code. const [setupChallenge, setSetupChallenge] = useState(null); const [setupCode, setSetupCode] = useState(""); const [events, setEvents] = useState([]); const [selectedEventId, setSelectedEventId] = useState(""); const [eventsLoading, setEventsLoading] = useState(false); // ── Change-event modal ─────────────────────────────────────────── const [showChangeModal, setShowChangeModal] = useState(false); const [changePassword, setChangePassword] = useState(""); const [showChangePassword, setShowChangePassword] = useState(false); const [changeError, setChangeError] = useState(null); const [changeLoading, setChangeLoading] = useState(false); // ── Visitor form state ─────────────────────────────────────────── const [visitorName, setVisitorName] = useState(""); const [visitorEmail, setVisitorEmail] = useState(""); const [visitorPhone, setVisitorPhone] = useState(""); const [notificationPref, setNotificationPref] = useState<"email" | "whatsapp" | "both">("email"); const [createAccount, setCreateAccount] = useState(false); const [visitorPassword, setVisitorPassword] = useState(""); const [showVisitorPassword, setShowVisitorPassword] = useState(false); const [quantities, setQuantities] = useState>({}); // ── Account lookup (automatic, triggered when email/phone is entered) ── // Email and phone are resolved to an existing account independently. If they // resolve to the SAME account, that account is "matched". If they resolve to // two DIFFERENT accounts, that's a conflict — surfaced to the operator instead // of silently overwriting one field's details with the other's account. type MatchedAccount = { id: string; name: string; email: string | null; phoneNumber: string | null; notificationPreference: "email" | "whatsapp" | "both" }; const [emailMatch, setEmailMatch] = useState(null); const [phoneMatch, setPhoneMatch] = useState(null); const accountConflict = !!(emailMatch && phoneMatch && emailMatch.id !== phoneMatch.id); const matchedAccount = accountConflict ? null : (emailMatch || phoneMatch); const accountExists = !!matchedAccount; const [lookupLoading, setLookupLoading] = useState(false); const [lookupMessage, setLookupMessage] = useState(null); const [showUpdateConfirm, setShowUpdateConfirm] = useState(false); const [formLoading, setFormLoading] = useState(false); const [formError, setFormError] = useState(null); // ── Event form (attendee form) state ──────────────────────────── const [currentRegistrationId, setCurrentRegistrationId] = useState(null); const [currentTicketIndex, setCurrentTicketIndex] = useState(0); const [allFormAnswers, setAllFormAnswers] = useState[]>([]); const [currentTicketAnswers, setCurrentTicketAnswers] = useState>({}); const [formSubmitLoading, setFormSubmitLoading] = useState(false); const [formSubmitError, setFormSubmitError] = useState(null); // ── Confirmation state ─────────────────────────────────────────── const [confirmationName, setConfirmationName] = useState(""); const [registrationIsFree, setRegistrationIsFree] = useState(false); const [countdown, setCountdown] = useState(10); const countdownRef = useRef | null>(null); // ─── Derived ──────────────────────────────────────────────────── const selectedEvent = useMemo( () => events.find((e) => e.id === selectedEventId) || null, [events, selectedEventId] ); // The events list doesn't carry bundle deals or places per session; the single-event // response does. const [kioskFull, setKioskFull] = useState(null); useEffect(() => { if (!selectedEventId) { setKioskBundles([]); setKioskFull(null); return; } let cancelled = false; fetchEvent(selectedEventId, { authToken: supervisorToken || undefined }) .then(ev => { if (!cancelled) { setKioskBundles(ev?.bundles || []); setKioskFull(ev || null); } }) .catch(() => { if (!cancelled) { setKioskBundles([]); setKioskFull(null); } }); return () => { cancelled = true; }; }, [selectedEventId, supervisorToken]); // "Pick sessions" options: the sessions chosen per quantity key. const [picks, setPicks] = useState>({}); useEffect(() => { setPicks({}); }, [selectedEventId]); const pickOpt = (optionId: string) => { const o = (kioskFull?.eventOptions || []).find(x => x.id === optionId); return kioskFull && o && isPickOption(kioskFull, o) ? o : null; }; // On a "pick sessions" event every ticket has sessions to tick; only pickOpt ones are charged per session. const choiceOpt = (optionId: string) => { const o = (kioskFull?.eventOptions || []).find(x => x.id === optionId); return kioskFull && o && choosesSessions(kioskFull) ? o : null; }; const times = (key: string) => (pickOpt(key.split("::")[0]) ? (picks[key] || []).length : 1); const picker = (key: string, optionId: string) => { const o = choiceOpt(optionId); if (!o || !kioskFull) return null; // The sessions show straight away; ticking one when the quantity is 0 makes it 1. const qty = quantities[key] || 0; return 0 ? picks[key] || [] : []} onChange={ids => { setPicks(p => ({ ...p, [key]: ids })); if (qty === 0 && ids.length > 0) setQuantities(q => ({ ...q, [key]: 1 })); }} />; }; const total = useMemo(() => { if (!selectedEvent) return 0; return (selectedEvent.eventOptions || []).reduce((sum, o) => { if ((o.variants || []).length > 0) { return sum + (o.variants || []).reduce((vs, v) => vs + (quantities[`${o.id}::${v.id}`] || 0) * times(`${o.id}::${v.id}`) * effectiveVariantUnit(o, v), 0); } return sum + (quantities[o.id] || 0) * times(o.id) * effectiveUnit(o); }, 0); }, [selectedEvent, quantities, picks, kioskFull]); // eslint-disable-line react-hooks/exhaustive-deps const kioskBasket = useMemo(() => Object.entries(quantities).map(([key, quantity]) => { const [eventOptionId, variantId] = key.split("::"); return { eventOptionId, quantity, variantId: variantId || null }; }), [quantities]); const bundleQuote = useBundleQuote({ eventId: selectedEventId || "", basket: kioskBasket, token: supervisorToken, forUserId: matchedAccount?.id ?? null }); // Does the selected event have a required form? const eventHasRequiredForm = useMemo(() => { return !!(selectedEvent?.form?.isRequired && (selectedEvent.form.fields || []).some(f => f.type !== 'statement')); }, [selectedEvent]); // Answerable form fields (exclude statement-type display fields) const answerableFields = useMemo(() => { if (!selectedEvent?.form) return []; return (selectedEvent.form.fields || []) .filter(f => f.type !== 'statement') .sort((a, b) => (a.order || 0) - (b.order || 0)); }, [selectedEvent]); // Number of main tickets selected (for form count) const mainTicketCount = useMemo(() => { if (!selectedEvent) return 0; return (selectedEvent.eventOptions || []) .filter(o => o.isMainTicket) .reduce((sum, o) => { if ((o.variants || []).length > 0) { return sum + (o.variants || []).reduce((vs, v) => vs + (quantities[`${o.id}::${v.id}`] || 0), 0); } return sum + (quantities[o.id] || 0); }, 0); }, [selectedEvent, quantities]); // Differences between what's on file for the matched account and what's currently // typed in the form — shown to the operator for confirmation before anything is saved. const pendingChanges = useMemo(() => { if (!matchedAccount) return []; const changes: { field: string; from: string; to: string }[] = []; const name = visitorName.trim(); const email = visitorEmail.trim(); const phone = visitorPhone.trim(); if (name && name !== matchedAccount.name) { changes.push({ field: "Name", from: matchedAccount.name, to: name }); } if (email && email !== (matchedAccount.email || "")) { changes.push({ field: "Email", from: matchedAccount.email || "(none on file)", to: email }); } if (phone && phone !== (matchedAccount.phoneNumber || "")) { changes.push({ field: "Phone", from: matchedAccount.phoneNumber || "(none on file)", to: phone }); } if (notificationPref !== matchedAccount.notificationPreference) { changes.push({ field: "Send tickets via", from: prefLabel(matchedAccount.notificationPreference), to: prefLabel(notificationPref), }); } return changes; }, [matchedAccount, visitorName, visitorEmail, visitorPhone, notificationPref]); // ─── Load events after supervisor login ───────────────────────── const loadEvents = useCallback(async (token: string) => { setEventsLoading(true); try { const data = await fetchManagedEvents(token, { closed: false }); setEvents(data); } catch { setEvents([]); } finally { setEventsLoading(false); } }, []); // ─── Setup: supervisor login ───────────────────────────────────── async function handleSetupLogin(e: React.FormEvent) { e.preventDefault(); setSetupError(null); setSetupLoading(true); try { // Second pass for accounts with two-factor on: exchange the code that was // sent after the password step. The kiosk never asks to be remembered. let data: AuthResponse | TwoFactorChallenge; if (setupChallenge) { const { data: session } = await apiV2("post", "/auth/login/code", { body: { challengeId: setupChallenge.challengeId, code: setupCode, rememberDevice: false } }); data = toAuthResponse(session); } else { const { data: res } = await apiV2("post", "/auth/login", { body: { identifier: setupEmail, password: setupPassword } }); data = res.status === "code_required" ? { twoFactorRequired: true, challengeId: res.challengeId, method: res.method, destination: res.destination || "" } : toAuthResponse(res); } if (isTwoFactorChallenge(data)) { setSetupChallenge(data); setSetupCode(""); return; } const role: string = data.role || ""; if (!["admin", "supervisor"].includes(role)) { setSetupError("Only admins and supervisors can set up the kiosk."); setSetupChallenge(null); setSetupCode(""); setSetupLoading(false); return; } setSetupChallenge(null); setSetupCode(""); setSupervisorToken(data.token); await loadEvents(data.token); } catch (err) { setSetupError(errorMessage(err) || "Login failed. Please check your credentials."); } finally { setSetupLoading(false); } } function handleStartKiosk() { if (!selectedEventId || !selectedEvent) return; const initial: Record = {}; (selectedEvent.eventOptions || []).forEach((o) => { if ((o.variants || []).length > 0) { (o.variants || []).forEach((v) => (initial[`${o.id}::${v.id}`] = 0)); } else { initial[o.id] = 0; } }); setQuantities(initial); setScreen("form"); } // ─── Change event modal ────────────────────────────────────────── async function handleChangeEvent(e: React.FormEvent) { e.preventDefault(); setChangeError(null); setChangeLoading(true); try { // Re-confirm the password against the supervisor session the kiosk already // holds, rather than logging in again — a fresh login would send a new // two-factor code every time the event is changed. if (!supervisorToken) throw new Error("Your session has expired. Reload the kiosk and sign in again."); await apiV2Data("post", "/me/verify-password", { authToken: supervisorToken, body: { password: changePassword }, }); await loadEvents(supervisorToken); setChangePassword(""); setChangeError(null); setShowChangeModal(false); setScreen("setup"); setSelectedEventId(""); resetForm(); } catch (err) { setChangeError(errorMessage(err) || "Incorrect password."); } finally { setChangeLoading(false); } } // ─── Visitor registration ──────────────────────────────────────── // Validates the form and, if the typed name/email/phone differ from the matched // account's details, shows a confirmation modal before anything is saved. function handleRegisterSubmit(e: React.FormEvent) { e.preventDefault(); setFormError(null); if (!visitorName.trim() || (!visitorEmail.trim() && !visitorPhone.trim())) { setFormError("Name and at least an email or phone number are required."); return; } if (createAccount && visitorPassword.trim().length < 6) { setFormError("Password must be at least 6 characters."); return; } if (!selectedEventId) { setFormError("No event selected."); return; } if (accountConflict) { setFormError("The email and phone number entered belong to two different existing accounts. Please check and correct one of them before continuing."); return; } if (selectedEvent?.requiresParentalConsent) { const missing = consentDetailsMissing(selectedEvent.eventOptions || [], (o, v) => quantities[v ? `${o}::${v}` : o] || 0, attendeeEntries, selectedEvent.startDate); if (missing) { setFormError(missing); return; } } if (pendingChanges.length > 0) { setShowUpdateConfirm(true); return; } handleRegister(); } async function handleRegister() { setFormLoading(true); try { const user: { name: string; email?: string; phoneNumber?: string; password?: string } = { name: visitorName.trim(), ...(visitorEmail.trim() ? { email: visitorEmail.trim() } : {}), ...(visitorPhone.trim() ? { phoneNumber: visitorPhone.trim() } : {}), }; if (createAccount && visitorPassword) { user.password = visitorPassword; } const payload: Record = { eventId: selectedEventId, options: Object.entries(quantities) .filter(([, qty]) => qty > 0) .map(([key, quantity]) => { const [eventOptionId, variantId] = key.split("::"); return { eventOptionId, quantity, ...(variantId ? { variantId } : {}), ...(choiceOpt(eventOptionId) ? { sessionIds: picks[key] || [] } : {}) }; }), user, // If the visitor declined to create an account, don't push an unsolicited // activation email/WhatsApp at them afterward. skipActivationNotice: !createAccount, notificationPreference: notificationPref, ...(kioskCoupon ? { couponCode: kioskCoupon.code } : {}), }; const kioskNames = (selectedEvent?.allowNamedAttendees && namingOthers) || selectedEvent?.requiresParentalConsent ? attendeesPayload(selectedEvent.eventOptions || [], (o, v) => quantities[v ? `${o}::${v}` : o] || 0, attendeeEntries) : []; if (kioskNames.length > 0) { payload.attendees = kioskNames; payload.ticketDelivery = delivery; } const result = await (apiV2Data("post", "/registrations/manual", { body: payload as never, authToken: supervisorToken, }) as Promise); setCurrentRegistrationId(result?.id || null); setRegistrationIsFree(result?.status === "paid" || (result?.totalDueComputed ?? total) === 0); setConfirmationName(visitorName.trim()); // If event has a required form and there are main tickets, show form step if (eventHasRequiredForm && mainTicketCount > 0 && result?.id) { setCurrentTicketIndex(0); setAllFormAnswers([]); setCurrentTicketAnswers({}); setFormSubmitError(null); setScreen("eventform"); } else { setScreen("confirmation"); startCountdown(); } } catch (err) { setFormError(errorMessage(err) || "Registration failed. Please try again."); } finally { setFormLoading(false); } } // ─── Event form submission (per-ticket) ────────────────────────── async function handleFormSubmit(e: React.FormEvent) { e.preventDefault(); setFormSubmitError(null); // Validate required fields for current ticket for (const field of answerableFields) { if (field.isRequired && !currentTicketAnswers[field.id]?.trim()) { setFormSubmitError(`"${field.label}" is required.`); return; } } if (!currentRegistrationId) { setFormSubmitError("No registration found."); return; } const collectedAnswers = [...allFormAnswers, currentTicketAnswers]; // If more tickets remain, advance to the next one if (currentTicketIndex < mainTicketCount - 1) { setAllFormAnswers(collectedAnswers); setCurrentTicketAnswers({}); setCurrentTicketIndex((i) => i + 1); return; } // Last ticket — submit all responses setFormSubmitLoading(true); try { const responses = collectedAnswers.map((answers) => ({ answers })); await apiV2Data("post", "/registrations/{id}/form-responses", { params: { id: currentRegistrationId }, body: { responses }, authToken: supervisorToken, }); setScreen("confirmation"); startCountdown(); } catch (err) { setFormSubmitError(errorMessage(err) || "Form submission failed. Please try again."); } finally { setFormSubmitLoading(false); } } // ─── Confirmation countdown ────────────────────────────────────── function startCountdown() { setCountdown(10); if (countdownRef.current) clearInterval(countdownRef.current); countdownRef.current = setInterval(() => { setCountdown((c) => { if (c <= 1) { clearInterval(countdownRef.current!); resetAndGoToForm(); return 0; } return c - 1; }); }, 1000); } function resetAndGoToForm() { if (countdownRef.current) clearInterval(countdownRef.current); resetForm(); setScreen("form"); const initial: Record = {}; (selectedEvent?.eventOptions || []).forEach((o) => { if ((o.variants || []).length > 0) { (o.variants || []).forEach((v) => (initial[`${o.id}::${v.id}`] = 0)); } else { initial[o.id] = 0; } }); setQuantities(initial); } function resetForm() { setKioskCoupon(null); setNamingOthers(false); setAttendeeEntries({}); setDelivery("payer"); setVisitorName(""); setVisitorEmail(""); setVisitorPhone(""); setNotificationPref("email"); setCreateAccount(false); setVisitorPassword(""); setShowVisitorPassword(false); setEmailMatch(null); setPhoneMatch(null); appliedMatchIdRef.current = null; setShowUpdateConfirm(false); setLookupMessage(null); setFormError(null); setCurrentRegistrationId(null); setCurrentTicketIndex(0); setAllFormAnswers([]); setCurrentTicketAnswers({}); setFormSubmitError(null); } useEffect(() => { return () => { if (countdownRef.current) clearInterval(countdownRef.current); }; }, []); // ─── Automatic account lookup — runs once the operator finishes entering the // email or phone field (on blur), never on keystroke. Matches exactly against // that one value and only ever returns that one matched account (or nothing) — // never a broader/fuzzy match. Email and phone are tracked as two independent // matches (emailMatch/phoneMatch, above) rather than being merged into a single // "last found account" — that's what previously let changing the phone number // silently pull in and overwrite the form with an unrelated account's details. ── async function performLookup(kind: "email" | "phone", rawValue: string) { const value = rawValue.trim(); const setMatch = kind === "email" ? setEmailMatch : setPhoneMatch; if (!value || !supervisorToken) { setMatch(null); return; } if (kind === "email" ? !value.includes("@") : !looksLikePhone(value)) { setMatch(null); return; } setLookupLoading(true); setLookupMessage(null); try { const params = new URLSearchParams(); if (kind === "email") params.set("email", value); else params.set("phone", value); const data = await (apiV2Data("get", "/users/exists", { query: Object.fromEntries(params), authToken: supervisorToken }) as Promise<{ exists: boolean; user?: MatchedAccount | null; }>); if (data?.exists && data.user) { setMatch(data.user); setLookupMessage("Account found — details filled in below."); } else { setMatch(null); setLookupMessage(null); } } catch { setLookupMessage("Lookup failed. Please try again."); } finally { setLookupLoading(false); } } function handleEmailBlur() { performLookup("email", visitorEmail); } function handlePhoneBlur() { performLookup("phone", visitorPhone); } // Autofill Name/Email/Phone/preference from the matched account — but only once per // distinct account id. Re-focusing elsewhere on the page (e.g. tapping a ticket // quantity button) blurs whatever field was last active and re-runs its lookup; // that returns the same account as a new object each time, and keying this off // object identity instead of id made it re-fire and stomp every field — including // ones the operator had already deliberately edited — back to the account's // original values on every unrelated tap. const appliedMatchIdRef = useRef(null); useEffect(() => { if (!matchedAccount || appliedMatchIdRef.current === matchedAccount.id) return; appliedMatchIdRef.current = matchedAccount.id; setVisitorName(matchedAccount.name); if (matchedAccount.email) setVisitorEmail(matchedAccount.email); if (matchedAccount.phoneNumber) setVisitorPhone(matchedAccount.phoneNumber); setNotificationPref(matchedAccount.notificationPreference); }, [matchedAccount]); // Existing accounts are linked automatically — never show the "create account" toggle for them useEffect(() => { if (accountExists && createAccount) { setCreateAccount(false); setVisitorPassword(""); } }, [accountExists, createAccount]); // ─── Setup screen ──────────────────────────────────────────────── const isLoggedIn = !!supervisorToken; // ─── Render ────────────────────────────────────────────────────── return (
{/* Change Event button */} {screen === "form" && (
)} {/* ── Change Event Modal ─────────────────────────────────────── */} {showChangeModal && ( setShowChangeModal(false)}>

Change Event

Re-enter your password to change the event.

setChangePassword(e.target.value)} className="w-full border border-gray-300 rounded-lg px-4 py-3 pr-12 text-base focus:outline-none focus:ring-2 focus:ring-brand-500" placeholder="Your password" autoFocus required /> setShowChangePassword((v) => !v)} />
{changeError &&

{changeError}

}
)} {/* ── Setup Screen ───────────────────────────────────────────── */} {screen === "setup" && (

Self-Service Kiosk

Supervisor setup required

{!isLoggedIn && (
setSetupEmail(e.target.value)} className="w-full border border-gray-300 rounded-xl px-4 py-3 text-base focus:outline-none focus:ring-2 focus:ring-brand-500" placeholder="supervisor@example.com" required autoComplete="email" />
setSetupPassword(e.target.value)} className="w-full border border-gray-300 rounded-xl px-4 py-3 pr-12 text-base focus:outline-none focus:ring-2 focus:ring-brand-500" placeholder="••••••••" required autoComplete="current-password" /> setShowSetupPassword((v) => !v)} />
{setupChallenge && (
setSetupCode(e.target.value.replace(/\D/g, ""))} className="w-full border border-gray-300 rounded-xl px-4 py-3 text-base tracking-widest focus:outline-none focus:ring-2 focus:ring-brand-500" placeholder="6-digit code" autoFocus required />

{setupChallenge.enrolling && "Two-factor authentication is required for your role — this code turns it on. "} We sent a code to {setupChallenge.destination} by {setupChallenge.method === "whatsapp" ? "WhatsApp" : "email"}.{" "}

)} {setupError &&

{setupError}

}
)} {isLoggedIn && (

Signed in as supervisor. Select an event to begin.

{eventsLoading ? (

Loading events…

) : (
)} {selectedEvent && (

{selectedEvent.title}

{fmtDate(selectedEvent.startDate)} — {fmtDate(selectedEvent.endDate)}

{selectedEvent.form?.isRequired && (

This event requires an attendee form to be completed at registration.

)}
)}
)}
)} {/* ── Registration Form Screen ───────────────────────────────── */} {screen === "form" && selectedEvent && (

Registering for

{selectedEvent.title}

{fmtDate(selectedEvent.startDate)}

{ const v = e.target.value; setVisitorEmail(v); // Auto-derive preference when only one contact channel is present if (v.trim() && !visitorPhone.trim()) setNotificationPref("email"); else if (!v.trim() && visitorPhone.trim()) setNotificationPref("whatsapp"); }} onBlur={handleEmailBlur} className="w-full border border-gray-300 rounded-xl px-4 py-3.5 text-lg focus:outline-none focus:ring-2 focus:ring-brand-500" placeholder="john@example.com" />
{ const v = e.target.value; setVisitorPhone(v); if (v.trim() && !visitorEmail.trim()) setNotificationPref("whatsapp"); else if (!v.trim() && visitorEmail.trim()) setNotificationPref("email"); }} onBlur={handlePhoneBlur} className="w-full border border-gray-300 rounded-xl px-4 py-3.5 text-lg focus:outline-none focus:ring-2 focus:ring-brand-500" placeholder="+27 82 000 0000" />
{lookupLoading && (

Checking for an existing account…

)} {!lookupLoading && accountConflict && emailMatch && phoneMatch && (
This email matches an existing account for {emailMatch.name}, but this phone number matches a different existing account for {phoneMatch.name}. Please check and correct one of these fields before continuing.
)} {!lookupLoading && !accountConflict && lookupMessage && (

{lookupMessage}

)}
setVisitorName(e.target.value)} className="w-full border border-gray-300 rounded-xl px-4 py-3.5 text-lg focus:outline-none focus:ring-2 focus:ring-brand-500" placeholder="John Smith" required />
{/* Preference selector — only shown when both channels are available */} {visitorEmail.trim() && visitorPhone.trim() && (

Send tickets via

{(["email", "whatsapp", "both"] as const).map((p) => ( ))}
)} {/* Ticket options */} {(selectedEvent.eventOptions || []).length > 0 && (

Ticket Options

{(selectedEvent.eventOptions || []).map((opt) => { const hasVariants = (opt.variants || []).length > 0; if (hasVariants) { return (

{opt.name}

{(opt.variants || []).map((v) => { const unit = effectiveVariantUnit(opt, v); const baseVariantPrice = v.price !== null && v.price !== undefined ? v.price : opt.price; const key = `${opt.id}::${v.id}`; return (

{v.name}

{fmtCurrency(unit)} {unit !== baseVariantPrice && baseVariantPrice > 0 ? ` (was ${fmtCurrency(baseVariantPrice)})` : ""}

{quantities[key] || 0}
{picker(key, opt.id) &&
{picker(key, opt.id)}
}
); })}
); } const unit = effectiveUnit(opt); return (

{opt.name}

{fmtCurrency(unit)}{unit !== opt.price && opt.price > 0 ? ` (was ${fmtCurrency(opt.price)})` : ""}

{quantities[opt.id] || 0}
{picker(opt.id, opt.id)}
); })}
quantities[v ? `${o}::${v}` : o] || 0} onSetQuantity={(o, v, n) => setQuantities(q => ({ ...q, [v ? `${o}::${v}` : o]: n }))} /> {(selectedEvent?.allowNamedAttendees || selectedEvent?.requiresParentalConsent) && (
quantities[v ? `${o}::${v}` : o] || 0} enabled={namingOthers} onEnabledChange={setNamingOthers} entries={attendeeEntries} onEntriesChange={setAttendeeEntries} delivery={delivery} onDeliveryChange={setDelivery} consentEventStart={selectedEvent.requiresParentalConsent ? selectedEvent.startDate : undefined} collectGender={!!selectedEvent.collectGender} />
)} {total > 0 && selectedEventId && (
)} {total > 0 && (
{((kioskCoupon && kioskCoupon.discount > 0) || bundleQuote) && ( {fmtCurrency(kioskCoupon?.subtotal ?? bundleQuote?.subtotal ?? total)} )} Total: {fmtCurrency(kioskCoupon ? kioskCoupon.total : bundleQuote ? bundleQuote.total : total)}
)}
)} {/* Account creation toggle — hidden once we know an account already exists */} {accountExists ? (
An account with this email/number already exists — you'll be registered using that account.
) : (
{createAccount && (
setVisitorPassword(e.target.value)} className="w-full border border-gray-300 rounded-xl px-4 py-3 pr-12 text-base focus:outline-none focus:ring-2 focus:ring-brand-500" placeholder="Min. 6 characters" autoComplete="new-password" /> setShowVisitorPassword((v) => !v)} />
)}
)} {formError && (
{formError}
)}
)} {/* ── Confirm Account Changes Modal ──────────────────────────── */} {showUpdateConfirm && ( setShowUpdateConfirm(false)}>

Confirm account changes

These details differ from what's on file for this account. Confirm to update them.

{pendingChanges.map((c) => (

{c.field}

{c.from}

{c.to}

))}
)} {/* ── Event Form Screen ──────────────────────────────────────── */} {screen === "eventform" && selectedEvent?.form && (

{selectedEvent.title}

Attendee Information

{mainTicketCount > 1 && (

Attendee {currentTicketIndex + 1} of {mainTicketCount}

{Array.from({ length: mainTicketCount }).map((_, i) => (
))}
)} {mainTicketCount <= 1 && (

This form is required to complete your registration.

)}
{answerableFields.map((field) => (
{field.helpText && (

{field.helpText}

)} {isExtraFieldType(field.type) ? ( setCurrentTicketAnswers(a => ({ ...a, [field.id]: v }))} /> ) : field.type === "yes_no" ? (
{["Yes", "No"].map((opt) => ( ))}
) : field.type === "numeric" ? ( setCurrentTicketAnswers(a => ({ ...a, [field.id]: e.target.value }))} required={field.isRequired} className="w-full border border-gray-300 rounded-xl px-4 py-3 text-base focus:outline-none focus:ring-2 focus:ring-brand-500" /> ) : field.type === "date" ? ( setCurrentTicketAnswers(a => ({ ...a, [field.id]: e.target.value }))} required={field.isRequired} className="w-full border border-gray-300 rounded-xl px-4 py-3 text-base focus:outline-none focus:ring-2 focus:ring-brand-500" /> ) : field.type === "paragraph" ? (