<?php
// ============================================================
//  footer.php — Closes the shared admin layout
//  Place in: /admin/includes/footer.php
// ============================================================
?>
  </main><!-- /.page-content -->
</div><!-- /.main-wrapper -->

<script>
// ══════════════════════════════════════════════════════════════
//  GLOBAL ADMIN JS
// ══════════════════════════════════════════════════════════════

// ── Sidebar toggle (mobile) ───────────────────────────────────
const sidebar        = document.getElementById('sidebar');
const menuToggle     = document.getElementById('menuToggle');
const sidebarClose   = document.getElementById('sidebarClose');
const sidebarOverlay = document.getElementById('sidebarOverlay');

function openSidebar()  {
  sidebar.classList.add('open');
  sidebarOverlay.classList.add('show');
  document.body.style.overflow = 'hidden';
}
function closeSidebar() {
  sidebar.classList.remove('open');
  sidebarOverlay.classList.remove('show');
  document.body.style.overflow = '';
}

menuToggle?.addEventListener('click', openSidebar);
sidebarClose?.addEventListener('click', closeSidebar);
sidebarOverlay?.addEventListener('click', closeSidebar);

// ── Admin dropdown ────────────────────────────────────────────
const trigger  = document.getElementById('adminDropdownTrigger');
const dropdown = document.getElementById('adminDropdown');

trigger?.addEventListener('click', (e) => {
  e.stopPropagation();
  dropdown.classList.toggle('show');
  trigger.querySelector('.topbar-chevron')?.classList.toggle('rotated');
});

document.addEventListener('click', () => {
  dropdown?.classList.remove('show');
  trigger?.querySelector('.topbar-chevron')?.classList.remove('rotated');
});

// ── Global search (redirect to users page) ────────────────────
const globalSearch = document.getElementById('globalSearch');
globalSearch?.addEventListener('keydown', (e) => {
  if (e.key === 'Enter') {
    const q = globalSearch.value.trim();
    if (q) window.location.href = `users.php?search=${encodeURIComponent(q)}`;
  }
});

// ── Auto-dismiss flash alerts ─────────────────────────────────
document.querySelectorAll('.flash-alert').forEach(el => {
  setTimeout(() => {
    el.style.transition = 'opacity 0.5s, transform 0.5s';
    el.style.opacity    = '0';
    el.style.transform  = 'translateY(-8px)';
    setTimeout(() => el.remove(), 500);
  }, 4000);
});

// ── Nav user count badge ──────────────────────────────────────
fetch('api/user_count.php')
  .then(r => r.json())
  .then(d => {
    const badge = document.getElementById('navUserCount');
    if (badge && d.count !== undefined) badge.textContent = d.count;
  })
  .catch(() => {});

</script>

<?= $extra_scripts ?? '' ?>
</body>
</html>
