document.addEventListener("DOMContentLoaded", function () {
  var form = document.querySelector("[data-contact-form]");
  var status = document.querySelector("[data-contact-status]");

  if (!form || !status) {
    return;
  }

  function setStatus(message, isError) {
    status.textContent = message;
    status.classList.toggle("is-error", Boolean(isError));
  }

  form.addEventListener("submit", function (event) {
    var data;
    var name;
    var company;
    var email;
    var phone;
    var service;
    var message;
    var subject;
    var lines;
    var mailtoHref;

    event.preventDefault();

    if (!form.checkValidity()) {
      form.reportValidity();
      setStatus("Complete the required fields before sending your enquiry.", true);
      return;
    }

    data = new FormData(form);
    name = (data.get("name") || "").toString().trim();
    company = (data.get("company") || "").toString().trim();
    email = (data.get("email") || "").toString().trim();
    phone = (data.get("phone") || "").toString().trim();
    service = (data.get("service") || "").toString().trim();
    message = (data.get("message") || "").toString().trim();

    subject = service ? service + " enquiry - " + name : "Project enquiry - " + name;
    lines = [
      "Full Name: " + name,
      "Company: " + (company || "Not provided"),
      "Email: " + email,
      "Phone: " + (phone || "Not provided"),
      "Service Area: " + (service || "Not specified"),
      "",
      "Project Details:",
      message
    ];

    mailtoHref = "mailto:info@niqong.com?subject=" + encodeURIComponent(subject) + "&body=" + encodeURIComponent(lines.join("\n"));
    window.location.href = mailtoHref;
    setStatus("Your email app should open with the enquiry prefilled. If it does not, send the details manually to info@niqong.com.", false);
  });
});
