<%
  // =========================
  // BASISDATEN
  // =========================
  const role = user?.role || null;

  // Arzt + Mitarbeiter dürfen Patienten bedienen
  const canPatientArea = role === "arzt" || role === "mitarbeiter";

  const pid = patient && patient.id ? patient.id : null;
  const isActive = patient && patient.active ? true : false;
  const isWaiting = patient && patient.waiting_room ? true : false;

  const canUsePatient = canPatientArea && !!pid;

  function lockClass(allowed) {
    return allowed ? "" : "locked";
  }

  function hrefIfAllowed(allowed, href) {
    return allowed ? href : "#";
  }
%>


<div class="sidebar">

  <!-- ✅ Logo -->
  <div style="margin-bottom:30px; display:flex; flex-direction:column; gap:10px;">
    <div style="padding:20px; text-align:center;">
      <div class="logo" style="margin:0;">🩺 Praxis System</div>
    </div>
  </div>

  <!-- ✅ Zurück -->
  <a href="<%= backUrl || '/patients' %>" class="nav-item">
    <i class="bi bi-arrow-left-circle"></i> <%= t.global.return %>
  </a>

  <div style="margin:10px 0; border-top:1px solid rgba(255,255,255,0.12);"></div>

  <!-- ✅ Kein Patient gewählt -->
  <% if (!pid) { %>
    <div class="nav-item locked" style="opacity:0.7;">
      <i class="bi bi-info-circle"></i> Bitte Patient auswählen
      <span style="margin-left:auto;"><i class="bi bi-lock-fill"></i></span>
    </div>
  <% } %>

  <!-- =========================
       WARTEZIMMER
       ========================= -->
  <% if (pid && canPatientArea) { %>

    <% if (isWaiting) { %>
      <div class="nav-item locked" style="opacity:0.75;">
        <i class="bi bi-hourglass-split"></i> <%= t.global.waiting %>
        <span style="margin-left:auto;"><i class="bi bi-check-circle-fill"></i></span>
      </div>
    <% } else { %>
      <form method="POST" action="/patients/waiting-room/<%= pid %>">
        <button
          type="submit"
          class="nav-item"
          style="width:100%; border:none; background:transparent; text-align:left;"
          title="Patient ins Wartezimmer setzen"
        >
          <i class="bi bi-door-open"></i><%= t.global.towaitingroom %>
        </button>
      </form>
    <% } %>

  <% } else { %>
    <div class="nav-item locked" style="opacity:0.7;">
      <i class="bi bi-door-open"></i> <%= t.global.towaitingroom %>
      <span style="margin-left:auto;"><i class="bi bi-lock-fill"></i></span>
    </div>
  <% } %>

  <!-- =========================
       BEARBEITEN
       ========================= -->
  <a
    href="<%= hrefIfAllowed(canUsePatient, '/patients/edit/' + pid) %>"
    class="nav-item <%= active === 'patient_edit' ? 'active' : '' %> <%= lockClass(canUsePatient) %>"
    title="<%= canUsePatient ? '' : 'Bitte zuerst einen Patienten auswählen' %>"
    <% if (!canUsePatient) { %>data-locked="Bitte zuerst einen Patienten auswählen"<% } %>
  >
    <i class="bi bi-pencil-square"></i> <%= t.global.edit %>
    <% if (!canUsePatient) { %>
      <span style="margin-left:auto;"><i class="bi bi-lock-fill"></i></span>
    <% } %>
  </a>

  <!-- =========================
       ÜBERSICHT (Dashboard)
       ========================= -->
  <a
    href="<%= hrefIfAllowed(canUsePatient, '/patients/' + pid) %>"
    class="nav-item <%= active === 'patient_dashboard' ? 'active' : '' %> <%= lockClass(canUsePatient) %>"
    title="<%= canUsePatient ? '' : 'Bitte zuerst einen Patienten auswählen' %>"
    <% if (!canUsePatient) { %>data-locked="Bitte zuerst einen Patienten auswählen"<% } %>
  >
    <i class="bi bi-clipboard2-heart"></i> <%= t.global.overview %>
    <% if (!canUsePatient) { %>
      <span style="margin-left:auto;"><i class="bi bi-lock-fill"></i></span>
    <% } %>
  </a>

  <!-- =========================
       STATUS TOGGLE
       ========================= -->
  <form
    method="POST"
    action="<%= canUsePatient ? (isActive ? '/patients/deactivate/' + pid : '/patients/activate/' + pid) : '#' %>"
  >
    <button
      type="submit"
      class="nav-item <%= lockClass(canUsePatient) %>"
      style="width:100%; border:none; background:transparent; text-align:left;"
      <%= canUsePatient ? '' : 'disabled' %>
      title="<%= canUsePatient ? 'Status wechseln' : 'Bitte zuerst einen Patienten auswählen' %>"
    >
      <% if (isActive) { %>
        <i class="bi bi-x-circle"></i>  <%= t.patienteoverview.closepatient %>
      <% } else { %>
        <i class="bi bi-check-circle"></i> <%= t.patienteoverview.openpatient %>
      <% } %>

      <% if (!canUsePatient) { %>
        <span style="margin-left:auto;"><i class="bi bi-lock-fill"></i></span>
      <% } %>
    </button>
  </form>

  <!-- ✅ Upload -->
  <div class="sidebar-upload <%= lockClass(canUsePatient) %>">

  <div style="font-weight: 600; margin: 10px 0 6px 0; color: #e5e7eb">
    <i class="bi bi-paperclip"></i> <%= t.global.fileupload %>
  </div>

  <% if (canUsePatient) { %>
  <form
    action="/patients/<%= pid %>/files"
    method="POST"
    enctype="multipart/form-data"
  >
  <% } %>

    <input
      id="sbUploadInput"
      type="file"
      name="file"
      class="form-control form-control-sm mb-2"
      <%= canUsePatient ? "" : "disabled" %>
      required
    />

    <button
      id="sbUploadBtn"
      type="submit"
      class="btn btn-sm btn-outline-light w-100"
      <%= canUsePatient ? "" : "disabled" %>
    >
      📎 <%= t.global.upload %>
      <% if (!canUsePatient) { %>
        <span class="ms-2"><i class="bi bi-lock-fill"></i></span>
      <% } %>
    </button>

  <% if (canUsePatient) { %>
  </form>
  <% } %>

</div>

  <div class="spacer"></div>
</div>

<!-- ✅ Kein-Zugriff Toast (wird von /js/sidebar-lock.js gesteuert) -->
<div class="position-fixed top-0 start-50 translate-middle-x p-3" style="z-index:9999; margin-top:16px;">
  <div id="lockToast" class="toast align-items-center text-bg-danger border-0" role="alert" aria-live="assertive">
    <div class="d-flex">
      <div class="toast-body d-flex align-items-center gap-2">
        <i class="bi bi-lock-fill"></i>
        <span id="lockToastMsg">Kein Zugriff</span>
      </div>
      <button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast"></button>
    </div>
  </div>
</div>
