const message = document.getElementById("message");
const counter = document.getElementById("counter");
const btn = document.getElementById("submitBtn");
const btnText = document.getElementById("btnText");
const success = document.getElementById("successMsg");

/* Character counter + auto resize */
message.addEventListener("input", () => {
  counter.innerText = message.value.length + " / 300";
  message.style.height = "auto";
  message.style.height = message.scrollHeight + "px";
});

function submitMessage() {
  if (message.value.trim() === "") {
    alert("Please write a message!");
    return;
  }

  btn.disabled = true;
  btnText.innerText = "Sending...";
  success.style.display = "none";

  setTimeout(() => {
    btn.disabled = false;
    btnText.innerText = "Send Message 🚀";
    success.style.display = "block";
    message.value = "";
    counter.innerText = "0 / 300";
    message.style.height = "100px";
  }, 1200);
}
function submitMessage() {
  if (message.value.trim() === "") {
    alert("Please write a message!");
    return;
  }

  btn.disabled = true;
  btnText.innerText = "Sending...";
  success.style.display = "none";

  setTimeout(() => {
    /* 1️⃣ Get existing data (page 2 + 3 + 4) */
    const existingData =
      JSON.parse(localStorage.getItem("personalDetails")) || {};

    /* 2️⃣ Add message data */
    existingData.message = message.value.trim();

    /* 3️⃣ Save back to same storage */
    localStorage.setItem("personalDetails", JSON.stringify(existingData));

    success.style.display = "block";

    /* 4️⃣ Redirect to Page 5 */
    window.location.href = "../html/page5.html";
  }, 1200);
}
