<?php
/** @var array $admin current logged-in super admin */
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?= isset($pageTitle) ? htmlspecialchars($pageTitle) . ' · ' : '' ?>Hammerhead Workspace</title>
<link rel="stylesheet" href="/assets/css/app.css?v=<?= defined('ASSET_VERSION') ? ASSET_VERSION : time() ?>">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.6.13/flatpickr.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.6.13/flatpickr.min.js"></script>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700;800&display=swap" rel="stylesheet">
<script>
document.addEventListener('DOMContentLoaded', function() {
  function applyFP(el) {
    if (el._fp) return;
    var savedVal = el.getAttribute('data-saved') || el.value || '';
    var defDate = null;
    if (savedVal) {
      var p = savedVal.split('-');
      if (p.length === 3) defDate = new Date(p[0], p[1]-1, p[2]);
    }
    el.value = ''; // Always start blank visually
    el._fp = flatpickr(el, {
      dateFormat: 'm/d/Y',
      allowInput: false,
      defaultDate: defDate,
      onChange: function(dates, str, inst) {
        if (dates[0]) {
          var d = dates[0];
          var iso = d.getFullYear()+'-'+String(d.getMonth()+1).padStart(2,'0')+'-'+String(d.getDate()).padStart(2,'0');
          var h = inst.element._hi;
          if (!h) {
            h = document.createElement('input');
            h.type = 'hidden';
            h.name = inst.element.name;
            inst.element.removeAttribute('name');
            inst.element.insertAdjacentElement('afterend', h);
            inst.element._hi = h;
          }
          h.value = iso;
        }
      },
      onReady: function(dates, str, inst) {
        if (savedVal) {
          var h = document.createElement('input');
          h.type = 'hidden';
          h.name = inst.element.name;
          inst.element.removeAttribute('name');
          inst.element.insertAdjacentElement('afterend', h);
          h.value = savedVal;
          inst.element._hi = h;
        }
      }
    });
  }
  document.querySelectorAll('input[type="date"]').forEach(applyFP);
});
  });
});
</script>
</head>
<body>
<div class="admin-shell">
  <aside class="sidebar">
    <div class="brand">
      <span class="logo-chip"><img src="/assets/img/hh_logo.png" alt="Hammerhead"></span>
      <span>HH WORKSPACE</span>
    </div>
    <nav>
      <a href="/admin/users" class="<?= ($active ?? '') === 'users' ? 'active' : '' ?>">👤 User Management</a>
      <a href="/admin/departments" class="<?= ($active ?? '') === 'departments' ? 'active' : '' ?>">🏢 Departments</a>
      <a href="/admin/projects" class="<?= ($active ?? '') === 'projects' ? 'active' : '' ?>">📁 Projects</a>
      <a href="/admin/audit-logs" class="<?= ($active ?? '') === 'audit' ? 'active' : '' ?>">📋 Reports &amp; Audit Logs</a>
      <a href="/admin/settings" class="<?= ($active ?? '') === 'settings' ? 'active' : '' ?>">⚙️ System Settings</a>
      <a href="/admin/reset-test-data" class="<?= ($active ?? '') === 'reset' ? 'active' : '' ?>" style="color:#fca5a5;">🧹 Reset Test Data</a>
      <a href="/dev/view-as" class="<?= ($active ?? '') === 'dev' ? 'active' : '' ?>"
         style="color:#facc15;border-top:1px solid rgba(255,255,255,0.1);margin-top:8px;padding-top:12px;">
        🔧 View as User
      </a>
    </nav>
    <div class="profile-block">
      <div class="role-badge">Logged in as<br><strong style="color:#fff;"><?= htmlspecialchars($admin['name'] ?? $admin['full_name'] ?? $admin['username'] ?? 'Admin') ?></strong><br>Super Admin · Full Access</div>
      <a href="/logout" class="logout-btn">🚪 Logout</a>
    </div>
  </aside>
  <main class="main"><div class="main-inner">
<?php if (\App\Core\Auth::isImpersonating()):
  $admin = \App\Core\Auth::currentUser(); ?>
<div style="background:#1e3a8a;color:#fff;padding:10px 20px;display:flex;align-items:center;
            justify-content:space-between;font-size:13px;font-weight:600;
            border-radius:12px;margin-bottom:12px;gap:12px;">
  <span style="display:flex;align-items:center;gap:8px;">
    <span style="background:#facc15;color:#1e3a8a;border-radius:20px;padding:2px 10px;font-size:11px;font-weight:800;letter-spacing:.06em;">
      🔧 TEST / VIEW MODE
    </span>
    Viewing as <strong style="color:#facc15;"><?= htmlspecialchars($admin['name']) ?></strong>
    — all write actions are disabled
  </span>
  <a href="/dev/impersonate/stop"
     style="background:#fff;color:#1e3a8a;border-radius:8px;padding:5px 14px;
            text-decoration:none;font-size:12px;font-weight:800;white-space:nowrap;">
    ✕ Exit View Mode
  </a>
</div>
<?php endif; ?>