// Get reference to the form
const form = document.querySelector("form");

// Function to handle form submission
async function handleSubmit(event) {
  event.preventDefault();

  // Get the value entered in the input field
  const productCode = document.getElementById("productCode").value;
  try {
    // Send POST request to server
    const response = await fetch("http://localhost:3000/submit", {
      method: "POST",
      headers: {
        "Content-Type": "application/json", // Indicate that the request body is JSON
      },
      body: JSON.stringify({ productCode }),
    });

    if (!response.ok) {
      throw new Error(
        `Network response was not ok, status: ${response.status}`
      );
    }

    // Optional: Handle JSON response data here
    // const data = await response.json();
    // console.log(data);

    console.log("Data submitted successfully");
    // Optional: Update UI to reflect success
  } catch (error) {
    console.error("Error submitting data:", error);
    // Optional: Update UI to reflect error
  }
}

// Add event listener to the form's submit event
form.addEventListener("submit", handleSubmit);
