document.getElementById("loginForm").addEventListener("submit", async (event) => {
    event.preventDefault(); // Prevent default form submission

    const email = document.getElementById("email").value;
    const password = document.getElementById("password").value;

    try {
        // Send data to the PHP API
        const response = await fetch("php/login.php", {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify({ email, password }),
        });

        const result = await response.json();

        if (!response.ok) {
            // If the response status is not OK (e.g., 401), show an error
            alert(result.error || "Invalid login credentials");
            return; // Do not redirect if the credentials are invalid
        }

        // Success: Redirect to the landing page
        alert(result.message || "Login successful");
        window.location.href = "landing.html";
    } catch (error) {
        console.error("Error logging in:", error);
        alert("An error occurred. Please try again.");
    }
});
