
document.addEventListener("DOMContentLoaded", () => {
  const profileForm = document.getElementById("profileForm");
  const successModal = document.getElementById("profile-success-modal");
  const imageUpload = document.getElementById("imageUpload");
  const profilePreview = document.getElementById("profilePreview");

  // Image Upload then show priview
  if (imageUpload) {
    imageUpload.addEventListener("change", function () {
      const file = this.files[0];
      if (file) {
        const reader = new FileReader();
        reader.onload = (e) => (profilePreview.src = e.target.result);
        reader.readAsDataURL(file);
      }
    });
  }

  // Continue button click korle modal dekhabe
  if (profileForm) {
    profileForm.addEventListener("submit", (e) => {
      e.preventDefault();

      // Sob input check korbe (khali thakle border red hobe)
      let inputs = profileForm.querySelectorAll(".edavro-input");
      let hasError = false;

      inputs.forEach((input) => {
        if (!input.value) {
          input.classList.add("border-error");
          hasError = true;
        } else {
          input.classList.remove("border-error");
        }
      });

      // Jodi error na thake, tobe modal dekhabe
      if (!hasError) {
        successModal.classList.remove("hidden");
      }
    });

    // User jokhon type korbe, red border chole jabe
    profileForm.querySelectorAll(".edavro-input").forEach((input) => {
      input.addEventListener("input", () => {
        input.classList.remove("border-error");
      });
    });
  }
});

// Image Preview Script
document
  .getElementById("imageUpload")
  .addEventListener("change", function (event) {
    const reader = new FileReader();
    reader.onload = function () {
      document.getElementById("profilePreview").src = reader.result;
    };
    reader.readAsDataURL(event.target.files[0]);
  });
