const loginForm = document.getElementById('loginForm');
const submitBtn = document.getElementById('submitBtn');

loginForm.addEventListener('submit', async function(e) {
    e.preventDefault();
    
    const username = document.getElementById('username').value;
    const password = document.getElementById('password').value;
    
    submitBtn.innerHTML = `<i class="fas fa-spinner fa-spin"></i> Logging in...`;
    submitBtn.disabled = true;

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

        const data = await response.json();

        if (response.ok) {
            localStorage.setItem('username', data.username);
            alert(`Welcome back, ${data.username}!`);
            window.location.href = 'home.html';
        } else {
            alert('Error: ' + data.error);
        }
    } catch (err) {
        console.error('Fetch error:', err);
        alert('Could not connect to server.');
    } finally {
        submitBtn.innerHTML = 'Login';
        submitBtn.disabled = false;
    }
});

// Parallax effect
document.addEventListener('mousemove', function(e) {
    const moveX = (e.clientX - window.innerWidth / 2) * 0.01;
    const moveY = (e.clientY - window.innerHeight / 2) * 0.01;

    document.querySelector('.mountain-back').style.transform = `translate(${moveX * 0.5}px, ${moveY * 0.2}px)`;
    document.querySelector('.mountain-mid').style.transform = `translate(${moveX * 0.8}px, ${moveY * 0.4}px)`;
    document.querySelector('.mountain-front').style.transform = `translate(${moveX}px, ${moveY * 0.6}px)`;
});
