// Validaci車n del formulario de login
// Versi車n: 1.1.0

// Funci車n para manejar el login local
async function handleLogin(event) {
    event.preventDefault();
    
    const email = document.getElementById('email').value;
    const password = document.getElementById('password').value;
    const errorDiv = document.getElementById('errorMessage');
    const successDiv = document.getElementById('successMessage');

    try {
        const response = await fetch('/api/auth/login', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({ email, password })
        });

        const data = await response.json();

        if (response.ok) {
            successDiv.textContent = 'Iniciando sesi車n...';
            successDiv.classList.add('show');
            setTimeout(() => {
                window.location.href = '/dashboard.html';
            }, 1500);
        } else {
            errorDiv.textContent = data.message || 'Error al iniciar sesi車n';
            errorDiv.classList.add('show');
            setTimeout(() => {
                errorDiv.classList.remove('show');
            }, 5000);
        }
    } catch (error) {
        console.error('Error:', error);
        errorDiv.textContent = 'Error de conexi車n. Intenta de nuevo.';
        errorDiv.classList.add('show');
        setTimeout(() => {
            errorDiv.classList.remove('show');
        }, 5000);
    }
}

// Funci車n para login con Google
function loginWithGoogle() {
    window.location.href = '/api/auth/google';
}
