// Inscription screen — public tournament registration const { useState: useInscState, useEffect: useInscEffect } = React; // ── CPF helpers ─────────────────────────────────────────────── function formatCPF(v) { const d = v.replace(/\D/g, '').slice(0, 11); if (d.length <= 3) return d; if (d.length <= 6) return d.replace(/(\d{3})(\d+)/, '$1.$2'); if (d.length <= 9) return d.replace(/(\d{3})(\d{3})(\d+)/, '$1.$2.$3'); return d.replace(/(\d{3})(\d{3})(\d{3})(\d{1,2})/, '$1.$2.$3-$4'); } function formatPhone(v) { const d = v.replace(/\D/g, '').slice(0, 11); if (d.length <= 2) return d.length ? `(${d}` : d; if (d.length <= 6) return d.replace(/(\d{2})(\d+)/, '($1) $2'); if (d.length <= 10) return d.replace(/(\d{2})(\d{4})(\d+)/, '($1) $2-$3'); return d.replace(/(\d{2})(\d{5})(\d{4})/, '($1) $2-$3'); } function validateCPF(cpf) { const d = cpf.replace(/\D/g, ''); if (d.length !== 11) return false; if (/^(\d)\1{10}$/.test(d)) return false; let sum = 0; for (let i = 0; i < 9; i++) sum += parseInt(d[i]) * (10 - i); let r = (sum * 10) % 11; if (r >= 10) r = 0; if (r !== parseInt(d[9])) return false; sum = 0; for (let i = 0; i < 10; i++) sum += parseInt(d[i]) * (11 - i); r = (sum * 10) % 11; if (r >= 10) r = 0; return r === parseInt(d[10]); } function fmtCurrency(n) { return 'R$ ' + Number(n).toFixed(2).replace('.', ','); } // ── Component ───────────────────────────────────────────────── function InscricaoScreen({ tournamentId }) { const [tournament, setTournament] = useInscState(null); const [loading, setLoading] = useInscState(true); const [phase, setPhase] = useInscState('form'); // 'form' | 'choice' | 'free' | 'pix' const [name, setName] = useInscState(''); const [nickname, setNickname] = useInscState(''); const [cpf, setCpf] = useInscState(''); const [phone, setPhone] = useInscState(''); const [submitting, setSubmitting] = useInscState(false); const [error, setError] = useInscState(''); const [copied, setCopied] = useInscState(false); useInscEffect(() => { fetch(`/api/tournaments/${tournamentId}/info`) .then(r => r.ok ? r.json() : Promise.reject()) .then(data => { setTournament(data); setLoading(false); }) .catch(() => setLoading(false)); }, []); async function handleSubmit(e) { e.preventDefault(); if (!validateCPF(cpf)) { setError('CPF inválido'); return; } if (phone.replace(/\D/g, '').length < 10) { setError('Telefone inválido'); return; } setSubmitting(true); setError(''); try { const res = await fetch(`/api/tournaments/${tournamentId}/inscricao`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name, nickname: nickname.trim() || undefined, cpf: cpf.replace(/\D/g, ''), phone: phone.replace(/\D/g, ''), }), }); const data = await res.json(); if (!res.ok) { setError(data.error || 'Erro ao se inscrever'); return; } if (tournament.freeEntry) { setPhase('pix'); return; } const offerFree = tournament.freeForSubscribers && tournament.entryFee > 0 && tournament.channels?.length > 0; setPhase(offerFree ? 'choice' : 'pix'); } catch { setError('Erro de conexão. Tente novamente.'); } finally { setSubmitting(false); } } function copyPix() { if (!tournament?.pixCode) return; navigator.clipboard.writeText(tournament.pixCode); setCopied(true); setTimeout(() => setCopied(false), 2000); } const cpfDigits = cpf.replace(/\D/g, ''); const phoneDigits = phone.replace(/\D/g, ''); const canSubmit = name.trim() && cpfDigits.length === 11 && phoneDigits.length >= 10 && !submitting; if (loading) { return (