document.addEventListener("DOMContentLoaded", () => {
  const radios = document.querySelectorAll(".patient-radio");

  const sidebarPatientInfo = document.getElementById("sidebarPatientInfo");

  const sbOverview = document.getElementById("sbOverview");
  const sbHistory = document.getElementById("sbHistory");
  const sbEdit = document.getElementById("sbEdit");
  const sbMeds = document.getElementById("sbMeds");

  const sbWaitingRoomWrapper = document.getElementById("sbWaitingRoomWrapper");
  const sbActiveWrapper = document.getElementById("sbActiveWrapper");

  const sbUploadForm = document.getElementById("sbUploadForm");
  const sbUploadInput = document.getElementById("sbUploadInput");
  const sbUploadBtn = document.getElementById("sbUploadBtn");

  if (
    !radios.length ||
    !sidebarPatientInfo ||
    !sbOverview ||
    !sbHistory ||
    !sbEdit ||
    !sbMeds ||
    !sbWaitingRoomWrapper ||
    !sbActiveWrapper ||
    !sbUploadForm ||
    !sbUploadInput ||
    !sbUploadBtn
  ) {
    return;
  }

  // ✅ Sicherheit: Upload blocken falls nicht aktiv
  sbUploadForm.addEventListener("submit", (e) => {
    if (!sbUploadForm.action || sbUploadForm.action.endsWith("#")) {
      e.preventDefault();
    }
  });

  radios.forEach((radio) => {
    radio.addEventListener("change", () => {
      const id = radio.value;
      const firstname = radio.dataset.firstname;
      const lastname = radio.dataset.lastname;

      const waiting = radio.dataset.waiting === "1";
      const active = radio.dataset.active === "1";

      // ✅ Patient Info
      sidebarPatientInfo.innerHTML = `
        <div class="patient-name">
          <strong>${firstname} ${lastname}</strong>
        </div>
        <div class="patient-meta text-muted">
          ID: ${id}
        </div>
      `;

      // ✅ Übersicht
      sbOverview.href = "/patients/" + id;
      sbOverview.classList.remove("disabled");

      // ✅ Verlauf
      sbHistory.href = "/patients/" + id + "/overview";
      sbHistory.classList.remove("disabled");

      // ✅ Bearbeiten
      sbEdit.href = "/patients/edit/" + id;
      sbEdit.classList.remove("disabled");

      // ✅ Medikamente
      sbMeds.href = "/patients/" + id + "/medications";
      sbMeds.classList.remove("disabled");

      // ✅ Wartezimmer (NUR wenn Patient aktiv ist)
      if (!active) {
        sbWaitingRoomWrapper.innerHTML = `
          <div class="nav-item disabled">
            <i class="bi bi-door-open"></i> Ins Wartezimmer (Patient inaktiv)
          </div>
        `;
      } else if (waiting) {
        sbWaitingRoomWrapper.innerHTML = `
          <div class="nav-item disabled">
            <i class="bi bi-hourglass-split"></i> Wartet bereits
          </div>
        `;
      } else {
        sbWaitingRoomWrapper.innerHTML = `
          <form method="POST" action="/patients/waiting-room/${id}" style="margin:0;">
            <button type="submit" class="nav-item nav-btn">
              <i class="bi bi-door-open"></i> Ins Wartezimmer
            </button>
          </form>
        `;
      }

      // ✅ Sperren / Entsperren
      if (active) {
        sbActiveWrapper.innerHTML = `
          <form method="POST" action="/patients/deactivate/${id}" style="margin:0;">
            <button type="submit" class="nav-item nav-btn">
              <i class="bi bi-lock-fill"></i> Sperren
            </button>
          </form>
        `;
      } else {
        sbActiveWrapper.innerHTML = `
          <form method="POST" action="/patients/activate/${id}" style="margin:0;">
            <button type="submit" class="nav-item nav-btn">
              <i class="bi bi-unlock-fill"></i> Entsperren
            </button>
          </form>
        `;
      }

      // ✅ Upload nur aktiv wenn Patient ausgewählt
      sbUploadForm.action = "/patients/" + id + "/files";
      sbUploadInput.disabled = false;
      sbUploadBtn.disabled = false;
    });
  });
});
