<!doctype html>
<html lang="{{LANG_CODE}}" class="h-full">
<head>
  <meta charset="utf-8" />
  <title>{{T_APP_TITLE}}</title>
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <script src="/admin/assets/tailwindcss.js"></script>
  <script src="/admin/assets/htmx.min.js"></script>
  <style>
    body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif; }
    .nav-link.active { background: rgb(15 23 42); color: rgb(125 211 252); }
  </style>
</head>
<body class="h-full bg-slate-950 text-slate-100">
  <!--
    Single-page shell. The sidebar drives navigation via HTMX:
    each link does an `hx-get` of an HTML fragment URL that returns the
    body of the page. The fragments live under /admin/pages/ and are
    server-rendered Rust handlers that return Html<String>.
    This keeps the UI a flat directory of static files plus a small
    set of fragment endpoints — no SPA, no Node, no build step.
  -->
  <div class="min-h-full flex">
    <aside class="w-56 shrink-0 bg-slate-900 border-r border-slate-800 flex flex-col">
      <div class="px-4 py-5 border-b border-slate-800">
        <h1 class="text-base font-semibold">{{T_APP_TITLE}}</h1>
        <p id="me-display" class="text-xs text-slate-500 mt-1" hx-get="/admin/me" hx-trigger="load" hx-swap="innerHTML">…</p>
      </div>
      <nav class="flex-1 px-2 py-3 space-y-1">
        <a class="nav-link block px-3 py-1.5 text-sm rounded text-slate-300 hover:bg-slate-800"
           hx-get="/admin/pages/users" hx-target="#main" hx-push-url="#users">{{T_NAV_USERS}}</a>
        <a class="nav-link block px-3 py-1.5 text-sm rounded text-slate-300 hover:bg-slate-800"
           hx-get="/admin/pages/devices" hx-target="#main" hx-push-url="#devices">{{T_NAV_DEVICES}}</a>
        <a class="nav-link block px-3 py-1.5 text-sm rounded text-slate-300 hover:bg-slate-800"
           hx-get="/admin/pages/groups" hx-target="#main" hx-push-url="#groups">{{T_NAV_GROUPS}}</a>
        <a class="nav-link block px-3 py-1.5 text-sm rounded text-slate-300 hover:bg-slate-800"
           hx-get="/admin/pages/strategies" hx-target="#main" hx-push-url="#strategies">{{T_NAV_STRATEGIES}}</a>
        <a class="nav-link block px-3 py-1.5 text-sm rounded text-slate-300 hover:bg-slate-800"
           hx-get="/admin/pages/address-books" hx-target="#main" hx-push-url="#address-books">{{T_NAV_AB}}</a>
        <a class="nav-link block px-3 py-1.5 text-sm rounded text-slate-300 hover:bg-slate-800"
           hx-get="/admin/pages/audit" hx-target="#main" hx-push-url="#audit">{{T_NAV_AUDIT}}</a>
        <a class="nav-link block px-3 py-1.5 text-sm rounded text-slate-300 hover:bg-slate-800"
           hx-get="/admin/pages/deploy" hx-target="#main" hx-push-url="#deploy">{{T_NAV_DEPLOY}}</a>
      </nav>
      <div class="px-2 py-3 border-t border-slate-800 space-y-1">
        <a class="nav-link block px-3 py-1.5 text-sm rounded text-slate-400 hover:bg-slate-800"
           hx-get="/admin/pages/profile" hx-target="#main" hx-push-url="#profile">{{T_NAV_PROFILE}}</a>
        <button
          class="w-full text-left px-3 py-1.5 text-sm rounded text-slate-400 hover:bg-slate-800"
          hx-post="/admin/logout"
          hx-on::after-request="window.location.href = '/admin/login.html'"
        >{{T_NAV_SIGNOUT}}</button>
        <div class="pt-2">
          <label class="block text-[10px] uppercase tracking-wide text-slate-600 px-3 mb-1">{{T_LANGUAGE}}</label>
          <select
            class="w-full bg-slate-800 border border-slate-700 rounded px-2 py-1 text-xs text-slate-300"
            onchange="document.cookie='admin_lang='+this.value+'; path=/; max-age=31536000; samesite=strict'; window.location.reload();"
          >
            <option value="en"{{LANG_SEL_EN}}>English</option>
            <option value="de"{{LANG_SEL_DE}}>Deutsch</option>
            <option value="es"{{LANG_SEL_ES}}>Español</option>
            <option value="fr"{{LANG_SEL_FR}}>Français</option>
            <option value="ro"{{LANG_SEL_RO}}>Română</option>
          </select>
        </div>
      </div>
    </aside>

    <main id="main" class="flex-1 p-6 overflow-x-hidden">
      <div class="text-slate-500 text-sm">{{T_LOADING}}</div>
    </main>
  </div>

  <!-- Toast container used by all admin handlers via hx-trigger="load delay:1s" -->
  <div id="toast"
       class="fixed bottom-4 right-4 max-w-sm space-y-2 pointer-events-none"></div>

  <!-- Load fragment + highlight active link based on the URL hash. -->
  <script>
    function linkForHash() {
      const hash = location.hash || '#users';
      return document.querySelector('.nav-link[hx-push-url="' + hash + '"]')
          || document.querySelector('.nav-link[hx-push-url="#users"]');
    }
    function refreshActive() {
      const active = linkForHash();
      document.querySelectorAll('.nav-link').forEach(a => {
        a.classList.toggle('active', a === active);
      });
    }
    function loadFromHash() {
      const link = linkForHash();
      if (link) {
        htmx.ajax('GET', link.getAttribute('hx-get'),
                  { target: '#main', swap: 'innerHTML' });
      }
      refreshActive();
    }
    window.addEventListener('hashchange', loadFromHash);
    document.body.addEventListener('htmx:afterSwap', refreshActive);
    loadFromHash();

    // Bounce to login if any HTMX request comes back 401.
    document.body.addEventListener('htmx:responseError', (evt) => {
      if (evt.detail.xhr.status === 401) {
        window.location.href = '/admin/login.html';
      }
    });

    // Close any open per-row action popover when a click happens outside it.
    // The action dropdowns are <details class="... relative"> with an
    // absolutely-positioned panel; the deploy page uses <details> too but
    // without `relative`, so the selector is specific to the popover style.
    document.addEventListener('click', (e) => {
      document.querySelectorAll('details.relative[open]').forEach(d => {
        if (!d.contains(e.target)) d.removeAttribute('open');
      });
    });
  </script>
</body>
</html>
