document.addEventListener("DOMContentLoaded", () => {
  fetch((window.APP_BASE || "") + "/get_profile.php")
    .then(res => res.json())
    .then(data => {
      if (data.success) {
        const user = data.user;
        document.getElementById("fullName").value = user.full_name;
        document.getElementById("email").value = user.email;
        document.getElementById("phone").value = user.phone;
        document.getElementById("referralCode").value = user.referral_code;
        document.getElementById("accountStatus").value = user.account_status;
      }
    });

  document.getElementById("profileForm").addEventListener("submit", function (e) {
    e.preventDefault();
    const fullName = document.getElementById("fullName").value.trim();
    const phone = document.getElementById("phone").value.trim();

    fetch((window.APP_BASE || "") + "/update_profile.php", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ full_name: fullName, phone: phone }),
    })
      .then(res => res.json())
      .then(data => {
        const alertBox = document.getElementById("profileAlert");
        alertBox.className = `alert mt-4 alert-${data.success ? "success" : "danger"}`;
        alertBox.textContent = data.message;
        alertBox.classList.remove("d-none");
      });
  });
});
