// Get references to the form elements
const nameInput = document.getElementById("name");
const emailInput = document.getElementById("emailAddress");
const passwordInput = document.getElementById("password");
const confirmPasswordInput = document.getElementById("confirmPassword");
const csrfToken = document.getElementById("registration_form__token").value;

// Add event listeners to the form
const registerBtn = document.querySelector("#register-btn");
registerBtn.addEventListener("click", handleSubmit);

// Function to handle form submission
function handleSubmit(event) {
    // Prevent default form submission behavior
    event.preventDefault();

    // Validate input
    const name = nameInput.value;
    const email = emailInput.value;
    const password = passwordInput.value;
    const confirmPassword = confirmPasswordInput.value;

    if (name === "" || email === "" || password === "") {
        alert("Please fill in all fields.");
        return;
    }

    if (password !== confirmPassword) {
        alert("Passwords do not match.");
        return;
    }

    // Send data to server for processing
    const data = {
        "name": name,
        "email": email,
        "password": password,
        "plainPassword": password,
        "csrf_token": csrfToken
    };

    fetch("/register", {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
        },
        body: JSON.stringify(data),
    })
        .then((response) => response.json())
        .then((data) => {
            console.log("User registration successful.");
        })
        .catch((error) => {
            console.error("Error registering user:", error);
        });
}