<?php
if (session_status() === PHP_SESSION_NONE) session_start();

function admin_display_name(): string {
    if (isset($_SESSION['admin']) && is_array($_SESSION['admin'])) {
        foreach (['name','username','email'] as $k) {
            if (!empty($_SESSION['admin'][$k])) return (string)$_SESSION['admin'][$k];
        }
    }
    foreach (['admin_name','username','email','admin'] as $k) {
        if (!empty($_SESSION[$k]) && is_string($_SESSION[$k])) return (string)$_SESSION[$k];
    }
    return 'Administrator';
}

$adminName = admin_display_name();
$logo = isset($logo) ? $logo : '../assets/img/edencampus.png';
$curr = basename($_SERVER['PHP_SELF'] ?? '');
$role = strtolower((string)($_SESSION['admin_role'] ?? ''));

function nav_active(array $files, string $curr): string {
    return in_array($curr, $files, true) ? 'active' : '';
}
?>
<style>
  :root{
    --ec-bg:#0b2c61;
    --ec-bg-2:#08234c;
    --ec-link:#edf3ff;
    --ec-line:#ef2b2d;
    --ec-border:rgba(255,255,255,.10);
  }

  .ec-topbar{
    background:linear-gradient(180deg,var(--ec-bg),var(--ec-bg-2));
    box-shadow:0 10px 24px rgba(0,0,0,.18);
    border-bottom:1px solid rgba(255,255,255,.06);
    margin:24px auto 0;
    max-width:1280px;
  }

  .ec-topbar .container{
    max-width:1280px;
    padding-top:.45rem;
    padding-bottom:.45rem;
  }

  .ec-topbar .navbar-brand{
    display:flex;
    align-items:center;
    gap:10px;
    margin-right:18px;
  }

  .ec-topbar .navbar-brand img{
    height:42px;
    width:auto;
    object-fit:contain;
    background:#fff;
    padding:2px;
  }

  .ec-brand-text{
    line-height:1.05;
    color:#fff;
  }

  .ec-brand-text .small-line{
    display:block;
    font-size:11px;
    font-weight:800;
    text-transform:uppercase;
    letter-spacing:.2px;
    opacity:.95;
  }

  .ec-brand-text .big-line{
    display:block;
    font-size:15px;
    font-weight:800;
    letter-spacing:.2px;
    white-space:nowrap;
  }

  .ec-topbar .navbar-nav{
    align-items:center;
    gap:.2rem;
  }

  .ec-topbar .nav-link{
    color:var(--ec-link);
    font-weight:700;
    font-size:.95rem;
    padding:.7rem .45rem .95rem;
    position:relative;
    white-space:nowrap;
  }

  .ec-topbar .nav-link:hover{ color:#fff; }
  .ec-topbar .nav-link.active{ color:#fff; }

  .ec-topbar .nav-link.active::before{
  content:"";
  position:absolute;
  left:0;
  right:0;
  bottom:.22rem;
  height:5px;
  border-radius:999px;
  background:var(--ec-line);
}

  .ec-user-badge{
    display:inline-flex;
    align-items:center;
    background:rgba(255,255,255,.12);
    border:1px solid var(--ec-border);
    color:#fff;
    padding:.46rem .8rem;
    border-radius:999px;
    font-weight:700;
    font-size:.88rem;
    white-space:nowrap;
    margin-left:.35rem;
  }

  .ec-topbar .dropdown-menu{
    background:#10346f;
    border:1px solid rgba(255,255,255,.10);
    border-radius:12px;
    padding:.45rem;
    box-shadow:0 16px 30px rgba(0,0,0,.22);
  }

  .ec-topbar .dropdown-item{
    color:#eef3ff;
    border-radius:8px;
    font-weight:600;
    padding:.55rem .8rem;
  }

  .ec-topbar .dropdown-item:hover,
  .ec-topbar .dropdown-item:focus,
  .ec-topbar .dropdown-item.active{
    background:rgba(255,255,255,.10);
    color:#fff;
  }

  @media (max-width:991.98px){
    .ec-topbar{
      margin:12px 10px 0;
    }

    .ec-topbar .navbar-collapse{
      border-top:1px solid rgba(255,255,255,.10);
      margin-top:.55rem;
      padding-top:.55rem;
    }

    .ec-topbar .nav-link{
      padding:.7rem .4rem;
    }

   .ec-topbar .nav-link.active::after{

    .ec-brand-text .big-line{
      font-size:14px;
    }
  }

  @media (max-width:767.98px){
    .ec-brand-text{ display:none; }
  }
</style>

<nav class="navbar navbar-expand-lg navbar-dark ec-topbar">
  <div class="container">

    <a class="navbar-brand" href="dashboard.php">
      <img src="<?= htmlspecialchars($logo) ?>" alt="EDEN Campus Logo">
      <div class="ec-brand-text">
        <span class="small-line">EDEN CAMPUS SRILANKA</span>
        <span class="big-line">Exam Management Systems</span>
      </div>
    </a>

    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#ecNav" aria-controls="ecNav" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse" id="ecNav">
      <ul class="navbar-nav ms-auto">

        <?php if (in_array($role, ['super','admin'], true)): ?>
        <li class="nav-item">
          <a class="nav-link <?= nav_active(['users.php'], $curr) ?>" href="users.php">User</a>
        </li>
        <?php endif; ?>

        <?php if (in_array($role, ['super','admin'], true)): ?>
        <li class="nav-item">
          <a class="nav-link <?= nav_active(['courses.php'], $curr) ?>" href="courses.php">Course</a>
        </li>
        <?php endif; ?>

        <?php if (in_array($role, ['super','admin'], true)): ?>
        <li class="nav-item">
          <a class="nav-link <?= nav_active(['create_exam.php'], $curr) ?>" href="create_exam.php">Create exam</a>
        </li>
        <?php endif; ?>

        <?php if (in_array($role, ['super','admin'], true)): ?>
        <li class="nav-item dropdown">
  <a class="nav-link dropdown-toggle <?= nav_active(['add_question.php','add_question_select.php','bulk_upload_questions.php'], $curr) ?>"
     href="#"
     id="addQuestionDropdown"
     role="button"
     data-bs-toggle="dropdown"
     aria-expanded="false">
    Add Question
  </a>
  <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="addQuestionDropdown">
    <li>
      <a class="dropdown-item <?= $curr==='add_question_select.php' && (($_GET['mode'] ?? '')==='single') ? 'active' : '' ?>" href="add_question_select.php?mode=single">
        Single Questions
      </a>
    </li>
    <li>
      <a class="dropdown-item <?= $curr==='add_question_select.php' && (($_GET['mode'] ?? '')==='bulk') ? 'active' : '' ?>" href="add_question_select.php?mode=bulk">
        Bulk Upload Questions
      </a>
    </li>
  </ul>
</li>
<?php endif; ?>
<?php if (in_array($role, ['super','admin','staff'], true)): ?>
<li class="nav-item dropdown">
  <a class="nav-link dropdown-toggle <?= nav_active(['add_student.php','bulk_exam_attempt.php'], $curr) ?>"
     href="#"
     id="examAttemptsDropdown"
     role="button"
     data-bs-toggle="dropdown"
     aria-expanded="false">
    Exam Attempts
  </a>
  <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="examAttemptsDropdown">
    <li>
      <a class="dropdown-item <?= $curr==='add_student.php' ? 'active' : '' ?>" href="add_student.php">
        Add Student
      </a>
    </li>
    <li>
      <a class="dropdown-item <?= $curr==='bulk_exam_attempt.php' ? 'active' : '' ?>" href="bulk_exam_attempt.php">
        Bulk Student Assignment
      </a>
    </li>
  </ul>
</li>
        <?php endif; ?>

        <?php if (in_array($role, ['super','admin','staff'], true)): ?>
        <li class="nav-item">
          <a class="nav-link <?= nav_active(['view_students.php','student_created_list.php'], $curr) ?>" href="student_created_list.php">View Students</a>
        </li>
        <?php endif; ?>

        <?php if (in_array($role, ['super','admin','staff'], true)): ?>
        <li class="nav-item">
          <a class="nav-link <?= nav_active(['results.php'], $curr) ?>" href="results.php">All results</a>
        </li>
        <?php endif; ?>

        <li class="nav-item ms-lg-2">
          <span class="ec-user-badge">👋 <?= htmlspecialchars($adminName) ?></span>
        </li>

        <li class="nav-item">
          <a class="nav-link" href="logout.php">Logout</a>
        </li>

      </ul>
    </div>
  </div>
</nav>