// Get references to the form elements
const nameInput = document.getElementById("registration_form_name");
const emailInput = document.getElementById("registration_form_email");
const passwordInput = document.getElementById("registration_form_plainPassword");
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) {
    // Validate input
    const name = nameInput.value;
    const email = emailInput.value;
    const password = passwordInput.value;

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

    // Send data to server for processing
    const data = {
        "name": name,
        "email": email,
        "plainPassword": password,
        "_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);
        });
}