document.addEventListener('DOMContentLoaded', () => {

    const loginForm = document.getElementById("loginForm");
    loginForm.addEventListener('submit', function (event) {
        event.preventDefault();
        const username = loginForm.querySelector('input[placeholder="Username"]').value;
        const password = loginForm.querySelector('input[placeholder="Password"]').value;

        const xhr = new XMLHttpRequest();
        xhr.open("POST", "dist/php/custom.php?action=logout", true); 
        xhr.setRequestHeader("Content-Type", "application/json");

        xhr.onload = function () {
            if (xhr.status === 200) {
                const response = JSON.parse(xhr.responseText);
                if (response.success) {
                    alert("Login successful!");
                    window.location.href = "dashboard.html"; 
                } else {
                    alert("Invalid username or password.");
                }
            } else {
                console.error("Error during login:", xhr.statusText);
            }
        };
        
        xhr.onerror = function () {
            console.error("Login request failed:", xhr.statusText);
            // You might want to display a more user-friendly error message here.
        };

        xhr.send(JSON.stringify({ username: username, password: password }));
    });
});


