<%
  const role = user?.role || "";
  const isAdmin = role === "admin";

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

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

<div class="sidebar">

  <div class="sidebar-title">
    <h2>Admin</h2>
  </div>

  <!-- ✅ Logo -->
  <div style="padding:20px; text-align:center;">
    <div class="logo" style="margin:0;">
      🩺 Praxis System
    </div>
  </div>

  <div class="sidebar-menu">

    <!-- ✅ Firmendaten Verwaltung -->
    <a
      href="<%= hrefIfAllowed(isAdmin, '/admin/company-settings') %>"
      class="nav-item <%= active === 'companySettings' ? 'active' : '' %> <%= lockClass(isAdmin) %>"
      title="<%= isAdmin ? '' : 'Nur Admin' %>"
      <% if (!isAdmin) { %>data-locked="Kein Zugriff – nur für Administratoren"<% } %>
    >
      <i class="bi bi-people"></i> <%= t.adminSidebar.companysettings %>
      <% if (!isAdmin) { %>
        <span style="margin-left:auto;"><i class="bi bi-lock-fill"></i></span>
      <% } %>
    </a>

    <!-- ✅ User Verwaltung -->
    <a
      href="<%= hrefIfAllowed(isAdmin, '/admin/users') %>"
      class="nav-item <%= active === 'users' ? 'active' : '' %> <%= lockClass(isAdmin) %>"
      title="<%= isAdmin ? '' : 'Nur Admin' %>"
      <% if (!isAdmin) { %>data-locked="Kein Zugriff – nur für Administratoren"<% } %>
    >
      <i class="bi bi-people"></i> <%= t.adminSidebar.user %>
      <% if (!isAdmin) { %>
        <span style="margin-left:auto;"><i class="bi bi-lock-fill"></i></span>
      <% } %>
    </a>

    <!-- ✅ Rechnungsübersicht -->
    <a
      href="<%= hrefIfAllowed(isAdmin, '/admin/invoices') %>"
      class="nav-item <%= active === 'invoice_overview' ? 'active' : '' %> <%= lockClass(isAdmin) %>"
      title="<%= isAdmin ? '' : 'Nur Admin' %>"
      <% if (!isAdmin) { %>data-locked="Kein Zugriff – nur für Administratoren"<% } %>
    >
      <i class="bi bi-calculator"></i> <%= t.adminSidebar.invocieoverview %>
      <% if (!isAdmin) { %>
        <span style="margin-left:auto;"><i class="bi bi-lock-fill"></i></span>
      <% } %>
    </a>


    <!-- ✅ Seriennummer -->
    <a
      href="<%= hrefIfAllowed(isAdmin, '/admin/serial-number') %>"
      class="nav-item <%= active === 'serialnumber' ? 'active' : '' %> <%= lockClass(isAdmin) %>"
      title="<%= isAdmin ? '' : 'Nur Admin' %>"
      <% if (!isAdmin) { %>data-locked="Kein Zugriff – nur für Administratoren"<% } %>
    >
      <i class="bi bi-key"></i> <%= t.adminSidebar.seriennumber %>
      <% if (!isAdmin) { %>
        <span style="margin-left:auto;"><i class="bi bi-lock-fill"></i></span>
      <% } %>
    </a>

    <!-- ✅ Datenbank -->
    <a
      href="<%= hrefIfAllowed(isAdmin, '/admin/database') %>"
      class="nav-item <%= active === 'database' ? 'active' : '' %> <%= lockClass(isAdmin) %>"
      title="<%= isAdmin ? '' : 'Nur Admin' %>"
      <% if (!isAdmin) { %>data-locked="Kein Zugriff – nur für Administratoren"<% } %>
    >
      <i class="bi bi-hdd-stack"></i> <%= t.adminSidebar.databasetable %>
      <% if (!isAdmin) { %>
        <span style="margin-left:auto;"><i class="bi bi-lock-fill"></i></span>
      <% } %>
    </a>

     <!-- ✅ Logout -->
  <a href="/logout" class="nav-item">
    <i class="bi bi-box-arrow-right"></i> <%= t.sidebar.logout %>
  </a>

  </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>
