<!DOCTYPE html>
<html lang="id">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title><?= esc($title ?? 'Admin') ?> — Repositori STT El-Shadday</title>
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link href="https://fonts.googleapis.com/css2?family=Kreon:wght@400;600;700&display=swap" rel="stylesheet">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css" rel="stylesheet">
  <link rel="icon" type="image/gif" href="<?= base_url('assets/img/logo.gif') ?>">
  <link rel="stylesheet" href="<?= base_url('assets/css/custom.css') ?>">
</head>
<body class="admin-body">

<div class="d-flex" id="adminWrapper">

  <!-- Sidebar -->
  <nav id="adminSidebar" class="stt-sidebar d-flex flex-column">
    <div class="sidebar-brand p-3 d-flex align-items-center gap-2">
      <img src="<?= base_url('assets/img/logo.gif') ?>" alt="Logo" height="36">
      <span class="fw-bold text-white small">Repositori Admin</span>
    </div>
    <hr class="sidebar-divider m-0">

    <ul class="nav flex-column px-2 py-3 flex-grow-1">
      <li class="nav-item">
        <a class="nav-link sidebar-link <?= uri_string() === 'admin' ? 'active' : '' ?>"
           href="<?= base_url('admin') ?>">
          <i class="bi bi-speedometer2"></i> Dashboard
        </a>
      </li>
      <li class="nav-item">
        <a class="nav-link sidebar-link <?= str_starts_with(uri_string(), 'admin/documents') ? 'active' : '' ?>"
           href="<?= base_url('admin/documents') ?>">
          <i class="bi bi-file-earmark-text"></i> Dokumen
          <?php
            $pending = model('DocumentModel')->whereIn('status', ['submitted','review'])->countAllResults();
            if ($pending > 0): ?>
            <span class="badge bg-warning text-dark ms-auto"><?= $pending ?></span>
          <?php endif; ?>
        </a>
      </li>
      <li class="nav-item">
        <a class="nav-link sidebar-link <?= str_starts_with(uri_string(), 'admin/users') ? 'active' : '' ?>"
           href="<?= base_url('admin/users') ?>">
          <i class="bi bi-people"></i> Pengguna
        </a>
      </li>
      <li class="nav-item">
        <a class="nav-link sidebar-link <?= str_starts_with(uri_string(), 'admin/programs') ? 'active' : '' ?>"
           href="<?= base_url('admin/programs') ?>">
          <i class="bi bi-mortarboard"></i> Prodi
        </a>
      </li>
      <li class="nav-item">
        <a class="nav-link sidebar-link <?= str_starts_with(uri_string(), 'admin/reports') ? 'active' : '' ?>"
           href="<?= base_url('admin/reports') ?>">
          <i class="bi bi-file-earmark-bar-graph"></i> Laporan
        </a>
      </li>
      <li class="nav-item">
        <a class="nav-link sidebar-link <?= str_starts_with(uri_string(), 'admin/logs') ? 'active' : '' ?>"
           href="<?= base_url('admin/logs') ?>">
          <i class="bi bi-journal-code"></i> Log Aktivitas
        </a>
      </li>
    </ul>

    <hr class="sidebar-divider m-0">
    <div class="p-3">
      <a class="nav-link sidebar-link" href="<?= base_url('/') ?>">
        <i class="bi bi-globe"></i> Lihat Situs
      </a>
      <a class="nav-link sidebar-link text-danger-soft" href="<?= base_url('logout') ?>">
        <i class="bi bi-box-arrow-right"></i> Keluar
      </a>
    </div>
  </nav>

  <!-- Main content -->
  <div class="flex-grow-1 d-flex flex-column" style="min-width:0;">

    <!-- Top bar -->
    <header class="admin-topbar d-flex align-items-center px-4 py-2">
      <button class="btn btn-sm btn-link text-muted p-0 me-3 d-lg-none" id="sidebarToggle">
        <i class="bi bi-list fs-5"></i>
      </button>
      <span class="fw-semibold text-muted small"><?= esc($title ?? 'Dashboard') ?></span>
      <div class="ms-auto d-flex align-items-center gap-3">
        <span class="small text-muted">
          <i class="bi bi-person-circle me-1"></i>
          <?= esc(session()->get('user_name') ?? auth()->user()->username) ?>
          <span class="badge bg-gold text-dark ms-1"><?= esc(session()->get('user_role') ?? 'admin') ?></span>
        </span>
      </div>
    </header>

    <!-- Flash messages -->
    <div class="px-4 pt-3">
      <?php if (session()->has('success')): ?>
        <div class="alert alert-success alert-dismissible fade show py-2" role="alert">
          <i class="bi bi-check-circle me-2"></i><?= esc(session('success')) ?>
          <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
      <?php endif; ?>
      <?php if (session()->has('error')): ?>
        <div class="alert alert-danger alert-dismissible fade show py-2" role="alert">
          <i class="bi bi-exclamation-circle me-2"></i><?= esc(session('error')) ?>
          <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
      <?php endif; ?>
    </div>

    <main class="flex-grow-1 px-4 py-3">
      <?= $this->renderSection('content') ?>
    </main>

  </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script>
  document.getElementById('sidebarToggle')?.addEventListener('click', () => {
    document.getElementById('adminSidebar').classList.toggle('show');
  });
</script>
<?= $this->renderSection('scripts') ?>
</body>
</html>
