<?php
$pageTitle = $pageTitle ?? 'Dashboard';
$userNama = $_SESSION['user_nama'] ?? 'Admin';
$userInitial = strtoupper(substr($userNama, 0, 1));
?>
<header class="topbar">
  <!-- Toggle button (mobile) -->
  <button class="topbar-toggle" id="sidebarToggle" aria-label="Toggle Sidebar">
    <i class="bi bi-list"></i>
  </button>

  <!-- Page title -->
  <div class="topbar-title d-none d-sm-block"><?= e($pageTitle) ?></div>

  <!-- Right side -->
  <div class="topbar-right">
    <!-- Tahun Ajaran badge -->
    <?php if (!empty($pengaturan['tahun_ajaran'])): ?>
    <span class="badge bg-primary-subtle text-primary d-none d-md-inline-flex align-items-center gap-1">
      <i class="bi bi-calendar3"></i>
      <?= e($pengaturan['tahun_ajaran']) ?> - <?= e($pengaturan['semester'] ?? 'Ganjil') ?>
    </span>
    <?php endif; ?>

    <!-- User dropdown -->
    <div class="dropdown">
      <div class="topbar-user" data-bs-toggle="dropdown" aria-expanded="false">
        <div class="avatar" style="width:32px;height:32px;background:var(--primary);border-radius:50%;display:flex;align-items:center;justify-content:center;color:#fff;font-weight:700;font-size:.8rem;">
          <?= $userInitial ?>
        </div>
        <span class="user-name d-none d-sm-block"><?= e($userNama) ?></span>
        <i class="bi bi-chevron-down" style="font-size:.7rem;color:#94a3b8;"></i>
      </div>
      <ul class="dropdown-menu dropdown-menu-end shadow border-0" style="min-width:200px;">
        <li>
          <div class="px-3 py-2 border-bottom">
            <div class="fw-600 text-dark"><?= e($userNama) ?></div>
            <div class="text-muted" style="font-size:.75rem;"><?= ucfirst($_SESSION['user_role'] ?? 'admin') ?></div>
          </div>
        </li>
        <li>
          <a class="dropdown-item" href="<?= BASE_URL ?>/index.php?page=pengaturan_sandi">
            <i class="bi bi-key me-2"></i> Ubah Sandi
          </a>
        </li>
        <li><hr class="dropdown-divider"></li>
        <li>
          <a class="dropdown-item text-danger" href="<?= BASE_URL ?>/index.php?page=logout"
             data-confirm="Apakah Anda yakin ingin keluar?">
            <i class="bi bi-box-arrow-right me-2"></i> Keluar
          </a>
        </li>
      </ul>
    </div>
  </div>
</header>
