<?php
require_once '../auth/cek_login.php';
require_once '../config/koneksi.php';

// Folder admin hanya boleh dibuka oleh admin dan guru.
// Siswa diarahkan ke halaman status agar tidak terjadi redirect berulang.
if (($_SESSION['role'] ?? '') === 'siswa') {
  header('Location: ../siswa/status_lulus.php');
  exit;
}

if (!function_exists('e')) {
  function e($value)
  {
    return htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8');
  }
}

$profil_header = $conn->query("SELECT * FROM profil_sekolah LIMIT 1")->fetch_assoc();
$current_page = basename($_SERVER['PHP_SELF']);
$role = $_SESSION['role'] ?? '';
$nama_user = $_SESSION['nama_admin'] ?? 'User';
$label_role = $role === 'admin' ? 'Administrator' : ($role === 'guru' ? 'Guru' : 'Siswa');

function menu_active($pages)
{
  global $current_page;
  return in_array($current_page, (array)$pages) ? 'active' : '';
}
function menu_open($pages)
{
  global $current_page;
  return in_array($current_page, (array)$pages) ? 'show' : '';
}
?>

<!doctype html>
<html lang="id">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Aplikasi Kartu Ujian</title>

  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css">
  <link rel="stylesheet" href="../assets/css/style.css">

  <style>
    .app-shell {
      display: flex;
      min-height: 100vh;
      background: #f1f5f9;
    }

    .sidebar {
      width: 245px;
      height: 100vh;
      overflow-y: auto;
      overflow-x: hidden;
      background: #0f172a;
      padding: 10px 8px;
      position: fixed;
      left: 0;
      top: 0;
      z-index: 1000;
    }

    .sidebar::-webkit-scrollbar {
      width: 6px;
    }

    .sidebar::-webkit-scrollbar-thumb {
      background: #475569;
      border-radius: 10px;
    }

    .brand-box {
      display: flex;
      align-items: center;
      gap: 8px;
      padding: 8px;
      margin-bottom: 8px;
      color: white;
      border-bottom: 1px solid rgba(255, 255, 255, .08);
    }

    .brand-box img {
      width: 40px;
      height: 40px;
      object-fit: contain;
      background: white;
      border-radius: 50%;
      padding: 3px;
      flex-shrink: 0;
    }

    .brand-box b {
      display: block;
      font-size: 12px;
      line-height: 1.15;
    }

    .brand-box small {
      display: block;
      font-size: 9px;
      color: #cbd5e1;
      line-height: 1.15;
      margin-top: 2px;
    }

    .menu-title {
      font-size: 10px;
      padding: 10px 9px 5px;
      color: #94a3b8;
      text-transform: uppercase;
      letter-spacing: .5px;
      font-weight: 700;
    }

    .menu-link,
    .dropdown-toggle-menu {
      display: flex;
      align-items: center;
      width: 100%;
      min-height: 34px;
      gap: 8px;
      color: #e2e8f0;
      text-decoration: none;
      font-size: 12.5px;
      font-weight: 500;
      padding: 7px 9px;
      border-radius: 8px;
      margin-bottom: 3px;
      border: none;
      background: transparent;
      text-align: left;
      transition: .2s;
    }

    .menu-link i:first-child,
    .dropdown-toggle-menu i:first-child {
      width: 18px;
      min-width: 18px;
      text-align: center;
      font-size: 13px;
    }

    .menu-text {
      flex: 1;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }

    .menu-link:hover,
    .dropdown-toggle-menu:hover,
    .menu-link.active,
    .dropdown-toggle-menu.active {
      background: #2563eb;
      color: white;
    }

    .dropdown-toggle-menu .arrow {
      margin-left: auto;
      font-size: 11px;
      transition: .2s;
    }

    .dropdown-toggle-menu[aria-expanded="true"] .arrow {
      transform: rotate(180deg);
    }

    .submenu {
      margin: 2px 0 6px;
      padding-left: 8px;
      border-left: 1px solid rgba(148, 163, 184, .25);
    }

    .submenu .menu-link {
      min-height: 30px;
      font-size: 11.5px;
      padding: 6px 8px;
      margin-bottom: 2px;
      background: rgba(255, 255, 255, .035);
    }

    .submenu .menu-link i:first-child {
      font-size: 11px;
    }

    .submenu .menu-link:hover,
    .submenu .menu-link.active {
      background: #1d4ed8;
    }

    .admin-mini {
      display: flex;
      align-items: center;
      gap: 8px;
      color: #e2e8f0;
      font-size: 11px;
      padding: 10px 8px;
      margin-top: 8px;
      border-top: 1px solid rgba(255, 255, 255, .08);
    }

    .admin-mini i {
      font-size: 19px;
    }

    .main-area {
      margin-left: 245px;
      width: calc(100% - 245px);
      min-height: 100vh;
    }

    .topbar {
      height: 54px;
      background: white;
      display: flex;
      align-items: center;
      padding: 0 18px;
      border-bottom: 1px solid #e2e8f0;
      position: sticky;
      top: 0;
      z-index: 900;
    }

    .content-wrap {
      padding: 18px;
    }

    @media (max-width: 768px) {
      .sidebar {
        width: 210px;
      }

      .main-area {
        margin-left: 210px;
        width: calc(100% - 210px);
      }

      .menu-link,
      .dropdown-toggle-menu {
        font-size: 11.5px;
        padding: 6px 8px;
      }

      .submenu .menu-link {
        font-size: 11px;
      }
    }
  </style>

</head>

<body class="admin-body">
  <div class="app-shell">
    <aside class="sidebar">
      <div class="brand-box">
        <img src="../assets/uploads/<?= e($profil_header['logo'] ?? 'logo.png') ?>" alt="Logo">
        <div>
          <b><?= e($profil_header['nama_sekolah'] ?? 'SEKOLAH') ?></b>
          <small><?= e($profil_header['alamat'] ?? '') ?></small>
        </div>
      </div>

      <div class="menu-title">Menu Utama</div>
      <a href="dashboard.php" class="menu-link <?= menu_active('dashboard.php') ?>">
        <i class="fa-solid fa-gauge-high"></i><span class="menu-text">Dashboard</span>
      </a>

      <?php if ($role === 'admin'): ?>
        <button class="dropdown-toggle-menu <?= menu_active('profil.php') ?>" data-bs-toggle="collapse" data-bs-target="#menuSekolah" aria-expanded="<?= menu_open('profil.php') ? 'true' : 'false' ?>">
          <i class="fa-solid fa-school"></i><span class="menu-text">Data Sekolah</span><i class="fa-solid fa-angle-down arrow"></i>
        </button>
        <div class="collapse submenu <?= menu_open('profil.php') ?>" id="menuSekolah">
          <a href="profil.php" class="menu-link <?= menu_active('profil.php') ?>"><i class="fa-solid fa-building-columns"></i><span class="menu-text">Profil Sekolah</span></a>
        </div>
      <?php endif; ?>

      <?php if (in_array($role, ['admin', 'guru'], true)): ?>
        <button class="dropdown-toggle-menu <?= menu_active(['siswa.php', 'tambah_siswa.php', 'edit_siswa.php']) ?>" data-bs-toggle="collapse" data-bs-target="#menuSiswa" aria-expanded="<?= menu_open(['siswa.php', 'tambah_siswa.php', 'edit_siswa.php']) ? 'true' : 'false' ?>">
          <i class="fa-solid fa-users"></i><span class="menu-text">Data Peserta</span><i class="fa-solid fa-angle-down arrow"></i>
        </button>
        <div class="collapse submenu <?= menu_open(['siswa.php', 'tambah_siswa.php', 'edit_siswa.php']) ?>" id="menuSiswa">
          <a href="siswa.php" class="menu-link <?= menu_active('siswa.php') ?>"><i class="fa-solid fa-address-book"></i><span class="menu-text">Data Siswa</span></a>
          <?php if ($role === 'admin'): ?>
            <a href="tambah_siswa.php" class="menu-link <?= menu_active('tambah_siswa.php') ?>"><i class="fa-solid fa-user-plus"></i><span class="menu-text">Tambah Siswa</span></a>
          <?php endif; ?>
        </div>
      <?php endif; ?>

      <?php if ($role === 'admin'): ?>
        <button class="dropdown-toggle-menu <?= menu_active(['cetak_massal.php']) ?>" data-bs-toggle="collapse" data-bs-target="#menuKartu" aria-expanded="<?= menu_open(['cetak_massal.php']) ? 'true' : 'false' ?>">
          <i class="fa-solid fa-id-card"></i><span class="menu-text">Kartu Ujian</span><i class="fa-solid fa-angle-down arrow"></i>
        </button>
        <div class="collapse submenu <?= menu_open(['cetak_massal.php']) ?>" id="menuKartu">
          <a href="cetak_massal.php" class="menu-link <?= menu_active('cetak_massal.php') ?>"><i class="fa-solid fa-layer-group"></i><span class="menu-text">Cetak Per Kelas</span></a>
          <a href="../cetak/kartu_massal.php" target="_blank" class="menu-link"><i class="fa-solid fa-print"></i><span class="menu-text">Cetak Landscape</span></a>
          <a href="../cetak/kartu_massal_potret.php" target="_blank" class="menu-link"><i class="fa-solid fa-image-portrait"></i><span class="menu-text">Cetak Potret</span></a>
        </div>

        <button class="dropdown-toggle-menu <?= menu_active(['daftar_hadir.php', 'berita_acara.php']) ?>" data-bs-toggle="collapse" data-bs-target="#menuAdministrasi" aria-expanded="<?= menu_open(['daftar_hadir.php', 'berita_acara.php']) ? 'true' : 'false' ?>">
          <i class="fa-solid fa-folder-open"></i><span class="menu-text">Administrasi Ujian</span><i class="fa-solid fa-angle-down arrow"></i>
        </button>
        <div class="collapse submenu <?= menu_open(['daftar_hadir.php', 'berita_acara.php']) ?>" id="menuAdministrasi">
          <a href="daftar_hadir.php" class="menu-link <?= menu_active('daftar_hadir.php') ?>"><i class="fa-solid fa-clipboard-user"></i><span class="menu-text">Daftar Hadir</span></a>
          <a href="berita_acara.php" class="menu-link <?= menu_active('berita_acara.php') ?>"><i class="fa-solid fa-file-signature"></i><span class="menu-text">Berita Acara</span></a>
        </div>
      <?php endif; ?>

      <?php if (in_array($role, ['admin', 'guru'], true)): ?>
        <button class="dropdown-toggle-menu <?= menu_active(['mapel.php', 'edit_mapel.php', 'nilai.php', 'rekap_nilai.php', 'nilai_akhir.php', 'cetak_nilai.php', 'cetak_skl.php']) ?>" data-bs-toggle="collapse" data-bs-target="#menuNilai" aria-expanded="<?= menu_open(['mapel.php', 'edit_mapel.php', 'nilai.php', 'rekap_nilai.php', 'nilai_akhir.php', 'cetak_nilai.php', 'cetak_skl.php']) ? 'true' : 'false' ?>">
          <i class="fa-solid fa-chart-column"></i><span class="menu-text">Nilai Ujian</span><i class="fa-solid fa-angle-down arrow"></i>
        </button>
        <div class="collapse submenu <?= menu_open(['mapel.php', 'edit_mapel.php', 'nilai.php', 'rekap_nilai.php', 'nilai_akhir.php', 'cetak_nilai.php', 'cetak_skl.php']) ?>" id="menuNilai">
          <?php if ($role === 'admin'): ?>
            <a href="mapel.php" class="menu-link <?= menu_active(['mapel.php', 'edit_mapel.php']) ?>"><i class="fa-solid fa-book"></i><span class="menu-text">Mata Pelajaran</span></a>
          <?php endif; ?>
          <a href="nilai.php" class="menu-link <?= menu_active('nilai.php') ?>"><i class="fa-solid fa-pen-to-square"></i><span class="menu-text">Input Nilai Ujian</span></a>
          <a href="rekap_nilai.php" class="menu-link <?= menu_active('rekap_nilai.php') ?>"><i class="fa-solid fa-square-poll-vertical"></i><span class="menu-text">Rekap Nilai Ujian</span></a>
          <a href="nilai_akhir.php" class="menu-link <?= menu_active('nilai_akhir.php') ?>"><i class="fa-solid fa-ranking-star"></i><span class="menu-text">Nilai Akhir</span></a>
          <a href="cetak_nilai.php" class="menu-link <?= menu_active('cetak_nilai.php') ?>"><i class="fa-solid fa-file-lines"></i><span class="menu-text">Cetak Nilai</span></a>
          <a href="cetak_skl.php" class="menu-link <?= menu_active('cetak_skl.php') ?>"><i class="fa-solid fa-file-circle-check"></i><span class="menu-text">Cetak SKL</span></a>
        </div>
      <?php endif; ?>

      <?php if ($role === 'admin'): ?>
        <div class="menu-title">Sistem</div>
        <a href="profil.php" class="menu-link <?= menu_active('profil.php') ?>"><i class="fa-solid fa-gear"></i><span class="menu-text">Pengaturan</span></a>
        <a href="pengguna.php" class="menu-link <?= menu_active('pengguna.php') ?>"><i class="fa-solid fa-user-gear"></i><span class="menu-text">Pengaturan Pengguna</span></a>
      <?php endif; ?>
      <a href="../auth/logout.php" class="menu-link"><i class="fa-solid fa-right-from-bracket"></i><span class="menu-text">Logout</span></a>

      <div class="admin-mini"><i class="fa-solid fa-user-circle"></i><span><?= e($nama_user) ?><br><small><?= e($label_role) ?></small></span></div>
    </aside>

    <section class="main-area">
      <div class="topbar">
        <i class="fa-solid fa-bars"></i>
        <div class="ms-auto d-flex align-items-center gap-3">
          <span class="badge bg-primary">TA <?= e($profil_header['tahun_pelajaran'] ?? date('Y')) ?></span>
          <span class="text-muted small"><?= strtoupper(str_replace('.php', '', $current_page)) ?></span>
          <span><i class="fa-solid fa-user-circle"></i> <?= e($label_role) ?></span>
        </div>
      </div>
      <main class="content-wrap">