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

registerForm.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> Creating Account...`;
    submitBtn.disabled = true;

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

        const data = await response.json();

        if (response.ok) {
            alert('Account created successfully! You can now login.');
            window.location.href = 'index.html';
        } else {
            alert('Error: ' + data.error);
        }
    } catch (err) {
        console.error('Fetch error:', err);
        alert('Could not connect to server.');
    } finally {
        submitBtn.innerHTML = 'Create Account';
        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)`;
});
