<?php
// Lecturer layout (TOP) — sidebar only (no admin links), logo + title, mobile hamburger
declare(strict_types=1);
if (session_status() === PHP_SESSION_NONE) session_start();

/* ---- access gate (lecturer only) ---- */
if (empty($_SESSION['user']['role']) || strtolower((string)$_SESSION['user']['role']) !== 'lecturer') {
  // send to lecturer login with return URL
  $req = $_SERVER['REQUEST_URI'] ?? '/';
  $login = '/lecturer/login.php?next=' . rawurlencode($req);
  header('Location: ' . $login);
  exit;
}

/* ---- helpers (fallbacks) ---- */
if (!function_exists('esc')) {
  function esc($s){ return htmlspecialchars((string)$s, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); }
}
if (!function_exists('app_url')) {
  function app_url(string $path=''): string {
    // Build rooted at /elms (strip trailing /lecturer from current dir)
    $scriptDir = rtrim(str_replace('\\','/', dirname($_SERVER['SCRIPT_NAME'] ?? '')), '/');
    $root = preg_replace('#/lecturer$#', '', $scriptDir); // e.g. "/elms"
    $root = ($root === '' ? '/' : $root);
    if ($path === '') return $root;
    return $root . ($path[0] === '/' ? $path : '/'.$path);
  }
}

/* ---- page chrome ---- */
$PAGE_TITLE  = $PAGE_TITLE  ?? 'Lecturer';
$ACTIVE_MENU = $ACTIVE_MENU ?? 'dashboard';
$logo        = app_url('/includes/logo/edden_logo.jpg');

/* nav helper */
$nav = function(string $key, string $href, string $label, string $icon) use($ACTIVE_MENU){
  $active = $ACTIVE_MENU === $key ? ' is-active' : '';
  $url = esc(app_url($href));
  echo '<a class="eden-link'.$active.'" href="'.$url.'"><i class="bi '.$icon.'"></i><span>'.$label.'</span></a>';
};
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title><?= esc($PAGE_TITLE) ?> · Lecturer</title>
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <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">
  <style>
    :root{
      --sb:#0f172a; --sb2:#13213b; --ink:#e6edf6; --muted:#93a4b8; --hl:#2563eb;
      --bg:#f5f7fb; --card:#fff; --sep:rgba(255,255,255,.08)
    }
    body{background:var(--bg)}
    .eden-shell{display:flex; min-height:100vh}
    .eden-aside{width:240px; background:linear-gradient(180deg,var(--sb),var(--sb2)); color:var(--ink); position:sticky; top:0; transition:transform .2s ease}
    .eden-aside.collapsed{transform:translateX(-100%)}
    .eden-brand{display:flex; align-items:center; gap:.6rem; padding:12px 14px; border-bottom:1px solid var(--sep)}
    .eden-brand img{height:36px}
    .eden-section{font-size:.72rem; text-transform:uppercase; letter-spacing:.08em; color:var(--muted); padding:10px 14px 6px}
    .eden-nav{display:flex; flex-direction:column; gap:2px; padding:6px}
    .eden-link{display:flex; gap:.6rem; padding:.55rem .75rem; margin:0 6px; border-radius:10px; color:var(--ink); text-decoration:none}
    .eden-link:hover{background:rgba(255,255,255,.06)}
    .eden-link.is-active{background:linear-gradient(90deg, rgba(37,99,235,.18), rgba(37,99,235,.10))}
    .eden-link i{width:18px; text-align:center}
    .eden-foot{margin-top:auto; padding:12px 14px; color:var(--muted); font-size:.82rem; border-top:1px solid var(--sep)}
    .eden-main{flex:1; min-width:0}
    .eden-top{height:50px; display:flex; align-items:center; padding:0 .75rem; background:linear-gradient(90deg,#1b2b78,#263fa0); color:#fff; position:sticky; top:0; z-index:5}
    .eden-top .user{margin-left:auto}
    .eden-top .burger{border:0;background:transparent;color:#fff;font-size:22px;margin-right:.5rem}
    .eden-container{padding:16px}
    @media (max-width: 992px){ .eden-aside{position:fixed; height:100vh; z-index:10; left:0; top:50px} .eden-shell{flex-direction:column} }
  </style>
</head>
<body>
<div class="eden-shell">
  <aside id="edenSidebar" class="eden-aside">
    <div class="eden-brand">
      <img src="<?= esc($logo) ?>" alt="Eden">
      <div>EDEN CAMPUS</div>
    </div>

    <div class="eden-section">Home</div>
    <nav class="eden-nav">
      <?php
        $nav('dashboard',   '/lecturer/dashboard.php',   'Dashboard',          'bi-speedometer2');
      ?>
    </nav>

    <div class="eden-section">Teaching</div>
    <nav class="eden-nav">
      <?php
        $nav('modules',      '/lecturer/modules.php',        'Modules',               'bi-journals');
        $nav('assessments',  '/lecturer/assessments.php',    'Assessments',           'bi-clipboard2-check');
        $nav('quizzes',      '/lecturer/quizzes.php',        'Quizzes',               'bi-patch-question');
        $nav('students',     '/lecturer/students.php',       'Students',              'bi-people');
        $nav('student_status','/lecturer/student_status.php','Student Status (Missing)','bi-exclamation-triangle');
      ?>
    </nav>

    <div class="eden-foot">
      <?= date('M d, Y h:i A') ?><br>
      <a href="<?= esc(app_url('/lecturer/logout.php')) ?>" class="eden-link" style="margin:6px 0 0 0;display:inline-flex">
        <i class="bi bi-box-arrow-right"></i><span>Logout</span>
      </a>
    </div>
  </aside>

  <main class="eden-main">
    <div class="eden-top">
      <button class="burger" type="button" id="edenBurger" aria-label="Toggle navigation"><i class="bi bi-list"></i></button>
      <div class="brand d-flex align-items-center gap-2">
        <img src="<?= esc($logo) ?>" alt="" style="height:26px;border-radius:4px;background:#ffffff1a;padding:2px">
        <strong>Lecturer Portal</strong>
      </div>
      <div class="user"><i class="bi bi-person-circle"></i> <?= esc($_SESSION['user']['name'] ?? 'Lecturer') ?></div>
    </div>
    <div class="eden-container">
<script>
  // Sidebar collapse (mobile) with persistence
  (function(){
    const aside  = document.getElementById('edenSidebar');
    const burger = document.getElementById('edenBurger');
    const KEY = 'eden_lect_sidebar_collapsed';
    const setCollapsed = (v)=>{ if(v){aside.classList.add('collapsed')} else {aside.classList.remove('collapsed')} };
    try { setCollapsed(localStorage.getItem(KEY)==='1'); } catch(e){}
    burger?.addEventListener('click', function(){
      const c = !aside.classList.contains('collapsed');
      setCollapsed(c);
      try { localStorage.setItem(KEY, c ? '1' : '0'); } catch(e){}
    });
  })();
</script>
