// Settings screen — edit tournament config + restart const { useState: useStState, useEffect: useStEffect } = React; function SettingsScreen({ tournamentId }) { const [tournament, setTournament] = useStState(null); const [loading, setLoading] = useStState(true); const [saving, setSaving] = useStState(false); const [restarting, setRestarting] = useStState(false); const [saved, setSaved] = useStState(false); const [confirmRestart, setConfirmRestart] = useStState(false); const [confirming, setConfirming] = useStState(false); const [refazendo, setRefazendo] = useStState(false); const [confirmRefazer, setConfirmRefazer] = useStState(false); const [resetando, setResetando] = useStState(false); const [confirmResetar, setConfirmResetar] = useStState(false); const [error, setError] = useStState(''); // form fields const [name, setName] = useStState(''); const [title, setTitle] = useStState(''); const [rules, setRules] = useStState(''); const [bgColor, setBgColor] = useStState('#05080e'); const [winsNeeded, setWinsNeeded] = useStState(2); const [entryFee, setEntryFee] = useStState(''); const [pixCode, setPixCode] = useStState(''); const [bannerImage, setBannerImage] = useStState(''); const [channels, setChannels] = useStState([]); const [contactPhone, setContactPhone] = useStState(''); const [freeForSubs, setFreeForSubs] = useStState(false); const [freeEntry, setFreeEntry] = useStState(false); const [hideNames, setHideNames] = useStState(false); // inscriptions const [inscricoes, setInscricoes] = useStState([]); const [inscLoading, setInscLoading] = useStState(false); const [togglingId, setTogglingId] = useStState(null); useStEffect(() => { fetch(`/api/tournaments/${tournamentId}`) .then(r => r.json()) .then(data => { const t = backendToReact(data); setTournament(t); setName(t.name || ''); setTitle(t.title || ''); setRules(t.rules || ''); setBgColor(t.bgColor || '#05080e'); setWinsNeeded(t.winsNeeded || 2); setEntryFee(data.entryFee > 0 ? String(data.entryFee) : ''); setPixCode(data.pixCode || ''); setBannerImage(data.bannerImage || ''); setChannels(data.channels || []); setContactPhone(data.contactPhone || ''); setFreeForSubs(!!data.freeForSubscribers); setFreeEntry(!!data.freeEntry); setHideNames(!!data.hideNames); setLoading(false); }) .catch(() => setLoading(false)); loadInscricoes(); }, []); async function loadInscricoes() { setInscLoading(true); try { const res = await fetch(`/api/tournaments/${tournamentId}/inscricoes`); if (res.status === 401) { window.location.href = '/admin/login'; return; } const data = await res.json(); setInscricoes(data); } catch {} finally { setInscLoading(false); } } async function setStatus(reg, newStatus) { setTogglingId(reg.id); try { const res = await fetch(`/api/inscricoes/${reg.id}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ status: newStatus }), }); if (!res.ok) return; setInscricoes(prev => prev.map(r => r.id === reg.id ? { ...r, status: newStatus } : r)); } catch {} finally { setTogglingId(null); } } async function handleSave() { setSaving(true); setError(''); try { const res = await fetch(`/api/tournaments/${tournamentId}/settings`, { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name, title, rules, bgColor, winsNeeded, entryFee: parseFloat(entryFee) || 0, pixCode, bannerImage, channels, contactPhone, freeForSubscribers: freeForSubs, freeEntry, hideNames, }), }); if (res.status === 401) { window.location.href = '/admin/login'; return; } if (!res.ok) throw new Error('Erro ao salvar'); setSaved(true); setTimeout(() => setSaved(false), 2500); } catch (e) { setError(e.message); } finally { setSaving(false); } } function handleBannerUpload(e) { const file = e.target.files[0]; if (!file) return; if (file.size > 3 * 1024 * 1024) { setError('A imagem deve ter no máximo 3 MB'); e.target.value = ''; return; } const reader = new FileReader(); reader.onload = ev => setBannerImage(ev.target.result); reader.readAsDataURL(file); } async function handleResetar() { setResetando(true); setError(''); try { const res = await fetch(`/api/tournaments/${tournamentId}/resetar`, { method: 'POST' }); if (res.status === 401) { window.location.href = '/admin/login'; return; } if (!res.ok) { const d = await res.json(); setError(d.error || 'Erro ao resetar'); return; } window.location.href = `/champion/${tournamentId}`; } catch { setError('Erro de conexão'); } finally { setResetando(false); setConfirmResetar(false); } } async function handleRefazer() { setRefazendo(true); setError(''); try { const res = await fetch(`/api/tournaments/${tournamentId}/refazer`, { method: 'POST' }); if (res.status === 401) { window.location.href = '/admin/login'; return; } const data = await res.json(); if (!res.ok) { setError(data.error || 'Erro ao refazer chaveamento'); return; } window.location.href = `/champion/${tournamentId}`; } catch (e) { setError('Erro de conexão'); } finally { setRefazendo(false); setConfirmRefazer(false); } } async function handleConfirmar() { setConfirming(true); setError(''); try { const res = await fetch(`/api/tournaments/${tournamentId}/confirmar`, { method: 'POST' }); if (res.status === 401) { window.location.href = '/admin/login'; return; } const data = await res.json(); if (!res.ok) { setError(data.error || 'Erro ao gerar chaveamento'); return; } window.location.href = `/champion/${tournamentId}`; } catch (e) { setError('Erro de conexão'); } finally { setConfirming(false); } } async function handleRestart() { setRestarting(true); setError(''); try { const res = await fetch(`/api/tournaments/${tournamentId}/restart`, { method: 'POST', }); if (res.status === 401) { window.location.href = '/admin/login'; return; } if (!res.ok) throw new Error('Erro ao reiniciar'); window.location.href = `/champion/${tournamentId}`; } catch (e) { setError(e.message); setRestarting(false); setConfirmRestart(false); } } if (loading) { return (