const translations = { es: { title: "Bienvenidos a SwimmingArt", subtitle: "La herramienta para tus rutinas de nado sincronizado", login_prompt: "Inicia sesión con tu cuenta para continuar", email: "Correo electrónico", password: "Contraseña", forgot_password: "¿Olvidaste tu contraseña?", login_button: "Ingresar", no_account: "¿No tienes cuenta?", register: "Regístrate", create_account: "Crear cuenta de SwimmingArt", name: "Nombre completo", username: "Nombre de usuario", register_email: "Correo electrónico", register_password: "Contraseña", role: "Rol", language: "Idioma", register_button: "Registrarse", has_account: "¿Ya tienes cuenta?", login: "Inicia sesión" }, en: { title: "Welcome to SwimmingArt", subtitle: "The tool for your synchronized swimming routines", login_prompt: "Log in with your account to continue", email: "Email", password: "Password", forgot_password: "Forgot your password?", login_button: "Log in", no_account: "Don't have an account?", register: "Sign up", create_account: "Create account in SwimmingArt", name: "Full name", username: "Username", register_email: "Email", register_password: "Password", role: "Role", language: "Language", register_button: "Sign up", has_account: "Already have an account?", login: "Log in" }, fr: { title: "Bienvenue sur SwimmingArt", subtitle: "L’outil pour vos routines de natation synchronisée", login_prompt: "Connectez-vous avec votre compte pour continuer", email: "Courriel", password: "Mot de passe", forgot_password: "Mot de passe oublié ?", login_button: "Connexion", no_account: "Vous n’avez pas de compte ?", register: "S’inscrire", create_account: "Créer un compte sur SwimmingArt", name: "Nom complet", username: "Nom d'utilisateur", register_email: "Courriel", register_password: "Mot de passe", role: "Rôle", language: "Langue", register_button: "S’inscrire", has_account: "Vous avez déjà un compte ?", login: "Connexion" } }; function getLoginHTML(lang) { const t = translations[lang]; return `

${t.title}

${t.subtitle}

${t.login_prompt}

${t.forgot_password}

${t.no_account} ${t.register}

`; } function getRegisterHTML(lang) { const t = translations[lang]; return `

${t.create_account}

${t.has_account} ${t.login}

`; } let currentLang = 'es'; function showLogin() { document.getElementById('rightPanel').innerHTML = getLoginHTML(currentLang); document.getElementById('languageSelect').value = currentLang; setupLoginForm(); } function showRegister() { document.getElementById('rightPanel').innerHTML = getRegisterHTML(currentLang); document.getElementById('languageSelect').value = currentLang; setupRegisterForm(); } function changeLanguage(lang) { currentLang = lang; const isLogin = document.getElementById('loginForm') !== null; if (isLogin) { showLogin(); } else { showRegister(); } } function setupLoginForm() { const form = document.getElementById('loginForm'); if (!form) return; form.addEventListener('submit', async (e) => { e.preventDefault(); const email = form.querySelector('input[type="email"]').value; const password = form.querySelector('input[type="password"]').value; try { const res = await fetch('/auth/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email, password }) }); if (!res.ok) { alert("Credenciales incorrectas."); return; } const data = await res.json(); sessionStorage.setItem("userId", data.userId); sessionStorage.setItem("role", data.role); if (data.role === "coach") { window.location.href = "equipoDisponibles.html"; } else if (data.role === "athlete") { window.location.href = "atleta.html"; } else { window.location.href = "ventanaPrincipal.html"; } } catch (error) { console.error("Error al iniciar sesión:", error); alert("Error de red al intentar iniciar sesión."); } }); } function setupRegisterForm() { const form = document.getElementById('registerForm'); if (!form) return; form.addEventListener('submit', async (e) => { e.preventDefault(); const data = Object.fromEntries(new FormData(form)); try { const response = await fetch('/auth/register', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }); if (response.ok) { //alert('Registro exitoso. Ahora inicia sesión.'); showLogin(); } else { const errorText = await response.text(); alert(`Error: ${errorText}`); } } catch (err) { console.error('Error de red:', err); alert('Ocurrió un error. Intenta más tarde.'); } }); } window.onload = () => { showLogin(); };