<?php
require_once('source/session.php');
require_once('source/error.php');
require_once('source/dbconfig.php');
require_once('source/dbconfig4.php'); // Add central settings
require_once('source/function.php');
require_once('source/restrict_admin.php');
session_start();    

// Get current branch settings from central database
$branchCode = getCurrentBranchCode();
$branchSettings = getSchoolSettings($branchCode);

if ($branchSettings) {
    $schoolName = $branchSettings['branch_name'];
    $schoolAddress = $branchSettings['branch_address'];
    $schoolShortName = $branchSettings['branch_short_name'];
    $schoolMotto = $branchSettings['school_motto'];
    $schoolLogo = $branchSettings['school_logo_path'];
    $schoolPhone = $branchSettings['contact_phone'];
    $schoolEmail = $branchSettings['contact_email'];
    $resultPortalUrl = $branchSettings['result_portal_url'];
    $footerText = $branchSettings['footer_text'];
    $footerCopyright = $branchSettings['footer_copyright'];
} else {
    // Fallback values if no branch settings found
    $schoolName = "Henzy Royal International School";
    $schoolAddress = "No 17 Amadi Street, Amakohia, Uratta Owerri Imo State Nigeria";
    $schoolShortName = "HRIS";
    $schoolMotto = "THE PRIDE OF EXCELLENCE";
    $schoolLogo = "coll_logo.png";
    $schoolPhone = "+234 803 292 9756";
    $schoolEmail = "support@hris.com.ng";
    $resultPortalUrl = "https://portal.hris.com.ng";
    $footerText = "THE PRIDE OF EXCELLENCE";
    $footerCopyright = "Copyright © Henzy Royal International School";
}

// Get system-wide settings
$supportPhone = getSystemSetting('support_phone') ?: $schoolPhone;
$supportEmail = getSystemSetting('support_email') ?: $schoolEmail;
$developerName = getSystemSetting('developer_name') ?: 'DataSwift Coders';
$developerWebsite = getSystemSetting('developer_website') ?: 'https://dataswift.com.ng';

// Check if the user is logged in 
if (isset($_SESSION['SESS_ID_ADMIN'])) {
    $userId = $_SESSION['SESS_ID_ADMIN'];

    // Fetch user details based on the ID
    $query = "SELECT * FROM tbl_admin WHERE id = :id";
    $stmt = $DBH->prepare($query);
    $stmt->bindParam(':id', $userId, PDO::PARAM_INT);
    $stmt->execute();

    if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $adminName = $row['username'];
    }
}

if(isset($_GET['msg']) && $_GET['msg'] == "refered"){
    $msg = "<div class='alert alert-success mb-3'>Refered successfully</div>";
} else if(isset($_GET['msg']) && $_GET['msg'] == "failed"){
    $msg = "<div class='alert alert-warning mb-3'>Something went wrong, please try again</div>";
}

if(isset($_SESSION['msg']) && $_SESSION['msg'] == "update") {
    $msg = "<div class='alert alert-success text-center mb-3'>Order Update Succesful. Customer will receive email.<a 
    class='btn-sm' href='new_order.php'>Refresh</a></div>";
}       

// Count students (teacher = false)
$studentCount = $DBH->query("SELECT COUNT(*) FROM tbl_user WHERE teacher = 'false'")->fetchColumn();
// Count teachers (teacher = true)
$teacherCount = $DBH->query("SELECT COUNT(*) FROM tbl_user WHERE teacher = 'true'")->fetchColumn();
// Count results based on unique admission number per term per session
$resultCount = $DBH->query("SELECT COUNT(DISTINCT CONCAT(registration_no, '-', term, '-', year)) FROM tbl_student_results")->fetchColumn();
// Count active sessions from tbl_session
$sessionCount = $DBH->query("SELECT COUNT(DISTINCT session) FROM tbl_session")->fetchColumn();
// Count admins
$adminCount = $DBH->query("SELECT COUNT(*) FROM tbl_admin")->fetchColumn();

// Get all branches for branch switcher
$allBranches = getAllBranches();
?>
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <title><?php echo $schoolName; ?> - Admin Dashboard</title>
  <link rel="icon" type="image/png" href="../<?php echo $schoolLogo; ?>">
  
  <!-- Bootstrap 5 CSS -->
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
  
  <!-- Font Awesome -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
  
  <!-- Google Fonts - Poppins -->
  <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
  
  <!-- Custom CSS -->
  <style>
    :root {
      --primary-blue: #1a4b8c;    /* Royal Blue */
      --secondary-gold: #FFD700;    /* Gold */
      --dark-blue: #0f2b4f;         /* Dark Blue */
      --light-blue: #4a7db5;         /* Medium Blue */
      --light-gold: #FFF1B8;          /* Light Gold */
      
      /* Group colors - Royal Blue and Gold theme */
      --group-1: #1a4b8c;
      --group-1-light: #e6f0ff;
      --group-2: #2a5ca3;
      --group-2-light: #e8f0fe;
      --group-3: #3a6db5;
      --group-3-light: #eaf2ff;
      --group-4: #FFD700;
      --group-4-light: #FFF9E6;
      --group-5: #e6b800;
      --group-5-light: #FFF3CD;
      --group-6: #1e3c6b;
      --group-6-light: #e1e9f5;
      --group-7: #2e4f87;
      --group-7-light: #e4edf9;
      --group-8: #3f62a3;
      --group-8-light: #e7f0fd;
      --group-9: #4f75bf;
      --group-9-light: #eaf3ff;
      --group-10: #6088db;
      --group-10-light: #edf5ff;
      
      /* Stat card colors */
      --stat-1: #1a4b8c;
      --stat-2: #2a5ca3;
      --stat-3: #3a6db5;
      --stat-4: #4a7db5;
      --stat-5: #5a8ec9;
    }
    
    body {
      font-family: 'Poppins', sans-serif;
      background-color: #f5f7fa;
      padding-top: 56px;
      color: #333;
    }
    
    /* Sidebar Styles */
    .sidebar {
      background: linear-gradient(180deg, var(--dark-blue) 0%, var(--primary-blue) 100%);
      position: fixed;
      top: 56px;
      left: 0;
      width: 250px;
      height: calc(100vh - 56px);
      transition: all 0.3s;
      z-index: 1000;
      overflow-y: auto;
      border-right: 3px solid var(--secondary-gold);
    }
    
    .sidebar .nav-link {
      color: rgba(255, 255, 255, 0.9);
      font-weight: 500;
      padding: 0.75rem 1rem;
      margin-bottom: 0.2rem;
      transition: all 0.2s;
      border-left: 3px solid transparent;
    }
     
    .sidebar .nav-link:hover {
      color: white;
      background-color: rgba(255, 215, 0, 0.15);
      border-left-color: var(--secondary-gold);
    }
    
    .sidebar .nav-link.active {
      color: var(--dark-blue);
      background-color: var(--secondary-gold);
      border-left-color: var(--dark-blue);
      font-weight: 600;
    }
    
    .sidebar .nav-link i {
      margin-right: 0.5rem;
      width: 20px;
      text-align: center;
    }
    
    /* Main Content Styles */
    .main-content {
      margin-left: 250px;
      width: calc(100% - 250px);
      transition: all 0.3s;
      min-height: calc(100vh - 56px);
      padding-bottom: 60px;
    }
    
    @media (max-width: 991.98px) {
      .sidebar {
        left: -250px;
      }
      .sidebar.active {
        left: 0;
      }
      .main-content {
        margin-left: 0;
        width: 100%;
      }
      .main-content.active {
        margin-left: 250px;
        width: calc(100% - 250px);
      }
      body {
        padding-top: 56px;
      }
    }
    
    /* Compact layout */
    .dashboard-container {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
      gap: 1rem;
      padding: 1rem;
    }
    
    .group-card {
      border-radius: 0.5rem;
      box-shadow: 0 0.15rem 1.75rem 0 rgba(26, 75, 140, 0.1);
      overflow: hidden;
      height: 100%;
      display: flex;
      flex-direction: column;
      transition: transform 0.3s ease, box-shadow 0.3s ease;
      border: 1px solid rgba(255, 215, 0, 0.2);
    }
    
    .group-card:hover {
      transform: translateY(-5px);
      box-shadow: 0 0.5rem 1.5rem rgba(26, 75, 140, 0.2);
      border-color: var(--secondary-gold);
    }
    
    .group-header {
      padding: 0.75rem 1rem;
      color: white;
      font-weight: 600;
      display: flex;
      align-items: center;
      font-size: 0.95rem;
      border-bottom: 2px solid var(--secondary-gold);
    }
    
    .group-header i {
      margin-right: 0.5rem;
      font-size: 1rem;
      color: var(--secondary-gold);
    }
    
    .group-body {
      padding: 0.75rem;
      background: white;
      flex-grow: 1;
      display: grid;
      grid-template-columns: 1fr;
      gap: 0.5rem;
    }
    
    .group-item {
      display: flex;
      align-items: center;
      padding: 0.5rem;
      border-radius: 0.25rem;
      transition: all 0.2s;
      color: #333;
      text-decoration: none;
      font-size: 0.85rem;
    }
    
    /* Alternating background colors for group items */
    .group-card:nth-child(1) .group-item:nth-child(odd) { background-color: var(--group-1-light); }
    .group-card:nth-child(2) .group-item:nth-child(odd) { background-color: var(--group-2-light); }
    .group-card:nth-child(3) .group-item:nth-child(odd) { background-color: var(--group-3-light); }
    .group-card:nth-child(4) .group-item:nth-child(odd) { background-color: var(--group-4-light); }
    .group-card:nth-child(5) .group-item:nth-child(odd) { background-color: var(--group-5-light); }
    .group-card:nth-child(6) .group-item:nth-child(odd) { background-color: var(--group-6-light); }
    .group-card:nth-child(7) .group-item:nth-child(odd) { background-color: var(--group-7-light); }
    .group-card:nth-child(8) .group-item:nth-child(odd) { background-color: var(--group-8-light); }
    .group-card:nth-child(9) .group-item:nth-child(odd) { background-color: var(--group-9-light); }
    .group-card:nth-child(10) .group-item:nth-child(odd) { background-color: var(--group-10-light); }
    
    .group-item:hover {
      background-color: var(--secondary-gold) !important;
      color: var(--dark-blue) !important;
      transform: translateX(3px);
    }
    
    .group-item:hover i {
      color: var(--dark-blue);
    }
    
    .group-item i {
      width: 20px;
      text-align: center;
      margin-right: 0.5rem;
      color: var(--primary-blue);
      font-size: 0.9rem;
    }
    
    /* Navbar */
    .navbar {
      background: linear-gradient(135deg, var(--primary-blue), var(--dark-blue));
      box-shadow: 0 0.15rem 1.75rem 0 rgba(26, 75, 140, 0.2);
      position: fixed;
      top: 0;
      width: 100%;
      z-index: 1030;
      color: white;
      border-bottom: 2px solid var(--secondary-gold);
    }
    
    .navbar-brand {
      color: white !important;
      font-weight: 700;
      letter-spacing: 1px;
    }
    
    /* Welcome banner */
    .welcome-banner {
      background: linear-gradient(135deg, var(--primary-blue) 0%, var(--light-blue) 100%);
      color: white;
      border-radius: 0.5rem;
      padding: 1rem;
      margin: 1rem;
      box-shadow: 0 4px 6px rgba(0,0,0,0.1);
      border-left: 5px solid var(--secondary-gold);
    }
    
    /* Stats cards */
    .stats-container {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
      gap: 1rem;
      margin: 0 1rem 1rem;
    }
    
    .stat-card {
      border-radius: 0.5rem;
      padding: 1rem;
      text-align: center;
      box-shadow: 0 2px 10px rgba(0,0,0,0.1);
      color: white;
      transition: transform 0.3s ease;
      border-bottom: 3px solid var(--secondary-gold);
    }
    
    .stat-card:hover {
      transform: translateY(-3px);
      box-shadow: 0 5px 20px rgba(255, 215, 0, 0.3);
    }
    
    .stat-card i {
      font-size: 1.5rem;
      margin-bottom: 0.5rem;
      color: var(--secondary-gold);
      background-color: rgba(255,255,255,0.2);
      width: 50px;
      height: 50px;
      line-height: 50px;
      border-radius: 50%;
      display: inline-block;
    }
    
    .stat-card h5 {
      font-size: 1.5rem;
      margin-bottom: 0.25rem;
      font-weight: 700;
    }
    
    .stat-card p {
      font-size: 0.9rem;
      margin-bottom: 0;
      opacity: 0.9;
    }
    
    /* Color assignments for stat cards */
    .stat-card:nth-child(1) { background: linear-gradient(135deg, var(--stat-1) 0%, #2a5ca3 100%); }
    .stat-card:nth-child(2) { background: linear-gradient(135deg, var(--stat-2) 0%, #3a6db5 100%); }
    .stat-card:nth-child(3) { background: linear-gradient(135deg, var(--stat-3) 0%, #4a7db5 100%); }
    .stat-card:nth-child(4) { background: linear-gradient(135deg, var(--stat-4) 0%, #5a8ec9 100%); }
    .stat-card:nth-child(5) { background: linear-gradient(135deg, var(--stat-5) 0%, #6a9fd9 100%); }
    
    /* Footer styles */
    .footer {
      background: linear-gradient(135deg, var(--dark-blue), var(--primary-blue));
      color: white;
      padding: 15px 0;
      position: fixed;
      bottom: 0;
      width: 100%;
      z-index: 100;
      border-top: 2px solid var(--secondary-gold);
    }
    
    .copyright-text {
      font-weight: 600;
      font-size: 0.85rem;
    }
    
    .powered-by {
      display: flex;
      align-items: center;
      justify-content: flex-end;
      color: white;
      font-size: 0.85rem;
    }
    
    .powered-by img {
      height: 20px;
      margin-left: 8px;
      filter: brightness(0) invert(1);
    }
    
    .powered-by-link {
      display: flex;
      align-items: center;
      text-decoration: none;
      color: var(--secondary-gold);
      transition: opacity 0.3s ease;
    }
    
    .powered-by-link:hover {
      opacity: 0.8;
      color: white;
      text-decoration: none;
    }
    
    /* Group header colors */
    .group-header.group-1 { background: linear-gradient(135deg, var(--group-1) 0%, #2a5ca3 100%); }
    .group-header.group-2 { background: linear-gradient(135deg, var(--group-2) 0%, #3a6db5 100%); }
    .group-header.group-3 { background: linear-gradient(135deg, var(--group-3) 0%, #4a7db5 100%); }
    .group-header.group-4 { background: linear-gradient(135deg, var(--group-4) 0%, #e6b800 100%); }
    .group-header.group-5 { background: linear-gradient(135deg, var(--group-5) 0%, #cc9a00 100%); }
    .group-header.group-6 { background: linear-gradient(135deg, var(--group-6) 0%, #1e3c6b 100%); }
    .group-header.group-7 { background: linear-gradient(135deg, var(--group-7) 0%, #2e4f87 100%); }
    .group-header.group-8 { background: linear-gradient(135deg, var(--group-8) 0%, #3f62a3 100%); }
    .group-header.group-9 { background: linear-gradient(135deg, var(--group-9) 0%, #4f75bf 100%); }
    .group-header.group-10 { background: linear-gradient(135deg, var(--group-10) 0%, #6088db 100%); }
    
    /* School logo area */
    .school-logo-area {
      text-align: center;
      margin-bottom: 1rem;
    }
    
    .logo-text {
      font-size: 1.8rem;
      font-weight: 900;
      color: white;
      letter-spacing: 2px;
      line-height: 1.2;
    }
    
    .logo-text span {
      color: var(--secondary-gold);
    }
    
    .logo-tagline {
      font-size: 0.7rem;
      color: var(--secondary-gold);
      letter-spacing: 1px;
    }
    
    .admin-info {
      color: white;
    }
    
    .admin-name {
      font-weight: 600;
    }
    
    .admin-access {
      opacity: 0.9;
      font-size: 0.85rem;
    }
    
    .btn-outline-light {
      border-color: var(--secondary-gold);
      color: var(--secondary-gold);
    }
    
    .btn-outline-light:hover {
      background-color: var(--secondary-gold);
      color: var(--dark-blue);
      border-color: var(--secondary-gold);
    }

    /* Branch Switcher */
    .branch-switcher {
      background: rgba(255,255,255,0.1);
      padding: 0.5rem;
      border-radius: 8px;
      margin: 0 1rem 1rem;
    }
    
    .branch-switcher select {
      background: white;
      border: 1px solid var(--secondary-gold);
      border-radius: 4px;
      padding: 0.25rem;
      font-size: 0.85rem;
    }
    
    .branch-badge {
      background: var(--secondary-gold);
      color: var(--dark-blue);
      padding: 0.25rem 0.75rem;
      border-radius: 20px;
      font-size: 0.75rem;
      font-weight: 600;
      display: inline-block;
      margin-left: 0.5rem;
    }
  </style>
</head>

<body>
  <!-- Top Navigation -->
  <nav class="navbar navbar-expand-lg navbar-light">
    <div class="container-fluid">
      <button id="sidebarToggle" class="btn btn-link d-lg-none me-2" style="color: white;">
        <i class="fas fa-bars"></i>
      </button>
      <a class="navbar-brand d-none d-lg-block" href="#">
        <img src="../<?php echo $schoolLogo; ?>" alt="<?php echo $schoolName; ?>" height="35" class="me-2" style="border-radius: 4px;" onerror="this.src='coll_logo.png';">
        <?php echo $schoolShortName ?: substr($schoolName, 0, 20); ?>
      </a>
      
      <!-- Branch Switcher (visible on desktop) -->
      <?php if (count($allBranches) > 1): ?>
      <div class="d-none d-lg-block ms-3">
        <form method="get" class="d-flex align-items-center">
          <select name="branch" class="form-select form-select-sm" style="width: auto; background: rgba(255,255,255,0.2); color: white; border-color: var(--secondary-gold);" onchange="this.form.submit()">
            <option value="" style="color: #333;">Switch Branch</option>
            <?php foreach ($allBranches as $branch): ?>
            <option value="<?php echo $branch['branch_code']; ?>" style="color: #333;" 
                    <?php echo ($branchCode == $branch['branch_code']) ? 'selected' : ''; ?>>
              <?php echo substr($branch['branch_name'], 0, 15); ?>... (<?php echo $branch['branch_code']; ?>)
            </option>
            <?php endforeach; ?>
          </select>
        </form>
      </div>
      <?php endif; ?>
      
      <div class="ms-auto d-flex align-items-center">
        <div class="admin-info me-3">
          <span class="admin-name"><?php echo htmlspecialchars($adminName ?? 'Admin'); ?></span>
          <span class="admin-access">(Administrator)</span>
          <i class="fas fa-user-circle admin-icon ms-2" style="color: var(--secondary-gold);"></i>
        </div>
        <a href="logout.php" class="btn btn-sm btn-outline-light">
          <i class="fas fa-sign-out-alt"></i> Logout
        </a>
      </div>
    </div>
  </nav>

  <!-- Sidebar -->
  <nav id="sidebar" class="sidebar">
    <div class="position-sticky pt-3">
      <div class="school-logo-area text-center mb-4 d-none d-lg-block">
        <div class="logo-text">
          <?php 
            $words = explode(' ', $schoolName);
            $initials = '';
            foreach ($words as $word) {
                if (!empty($word)) $initials .= strtoupper(substr($word, 0, 1));
            }
            echo substr($initials, 0, 2) . '.<span>' . (isset($initials[2]) ? substr($initials, 2, 1) : 'S') . '</span>';
          ?>
        </div>
        <div class="logo-tagline">
          <?php echo $schoolMotto; ?>
        </div>
        <p class="text-white-50 small mt-2">Admin Dashboard</p>
        
        <!-- Current Branch Info -->
        <div class="branch-badge">
          <i class="fas fa-map-marker-alt me-1"></i>
          <?php echo $branchCode ?: 'Main'; ?>
        </div>
        
        <hr style="border-color: var(--secondary-gold); opacity: 0.3;">
        <p class="text-white small px-2">
          <i class="fas fa-map-marker-alt" style="color: var(--secondary-gold);"></i><br>
          <?php echo $schoolAddress; ?>
        </p>
      </div>
      
      <ul class="nav flex-column">
        <li class="nav-item">
          <a class="nav-link active" href="main.php">
            <i class="fas fa-fw fa-tachometer-alt"></i>
            Dashboard
          </a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="post_session.php">
            <i class="fas fa-fw fa-calendar-alt"></i>
            School Year/Session
          </a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="post_classes.php">
            <i class="fas fa-fw fa-building"></i>
            Create Classes
          </a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="add_students.php">
            <i class="fas fa-fw fa-user-plus"></i>
            Register New Student
          </a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="post_subjects.php">
            <i class="fas fa-fw fa-book"></i>
            Create Subjects
          </a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="teachers.php">
            <i class="fas fa-fw fa-chalkboard-teacher"></i>
            Manage Teachers & Admins
          </a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="students.php">
            <i class="fas fa-fw fa-users"></i>
            Manage Students
          </a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="deactivate_students.php">
            <i class="fas fa-fw fa-user-lock"></i>
            Activate/Deactivate
          </a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="upload_results.php">
            <i class="fas fa-fw fa-upload"></i>
            Upload Results
          </a>
        </li>
        <!-- New Settings Link -->
        <li class="nav-item mt-3">
          <a class="nav-link" href="portal_settings.php" style="border-top: 1px solid rgba(255,215,0,0.3);">
            <i class="fas fa-fw fa-cog" style="color: var(--secondary-gold);"></i>
            <span style="color: var(--secondary-gold);">Portal Settings</span>
            <span class="badge bg-warning text-dark ms-2" style="font-size: 0.6rem;">Multi-Branch</span>
          </a>
        </li>
      </ul>
    </div>
  </nav>

  <!-- Main Content -->
  <main class="main-content">
    <!-- Welcome Banner -->
    <div class="welcome-banner">
      <div class="d-flex justify-content-between align-items-center">
        <div>
          <h4>
            <i class="fas fa-crown me-2" style="color: var(--secondary-gold);"></i>
            Welcome back, <?php echo htmlspecialchars($adminName ?? 'Admin'); ?>!
          </h4>
          <p class="mb-0"><?php echo $schoolName; ?> - Admin Dashboard</p>
          <small>
            <i class="fas fa-map-pin me-1" style="color: var(--secondary-gold);"></i> 
            <?php echo $schoolAddress; ?>
            <?php if ($branchCode): ?>
            <span class="branch-badge ms-2">Code: <?php echo strtoupper($branchCode); ?></span>
            <?php endif; ?>
          </small>
        </div>
        <div class="d-none d-sm-inline-block">
          <span class="badge bg-white p-2" style="color: var(--primary-blue);">
            <i class="fas fa-phone-alt me-1" style="color: var(--primary-blue);"></i> Support: <?php echo $supportPhone; ?>
          </span>
        </div>
      </div>
    </div>

    <!-- Mobile Branch Switcher (visible only on mobile) -->
    <?php if (count($allBranches) > 1): ?>
    <div class="d-lg-none branch-switcher">
      <form method="get" class="d-flex">
        <select name="branch" class="form-select form-select-sm" onchange="this.form.submit()">
          <option value="">Switch Branch</option>
          <?php foreach ($allBranches as $branch): ?>
          <option value="<?php echo $branch['branch_code']; ?>" 
                  <?php echo ($branchCode == $branch['branch_code']) ? 'selected' : ''; ?>>
            <?php echo $branch['branch_name']; ?> (<?php echo $branch['branch_code']; ?>)
          </option>
          <?php endforeach; ?>
        </select>
      </form>
    </div>
    <?php endif; ?>

    <?php if(isset($msg)) { echo $msg; } ?>

    <!-- Quick Stats Section -->
    <div class="stats-container">
      <div class="stat-card">
        <i class="fas fa-users"></i>
        <h5><?php echo number_format($studentCount); ?></h5>
        <p>Total Students</p>
      </div>
      
      <div class="stat-card">
        <i class="fas fa-chalkboard-teacher"></i>
        <h5><?php echo number_format($teacherCount); ?></h5>
        <p>Total Teachers</p>
      </div>
      
      <div class="stat-card">
        <i class="fas fa-user-shield"></i>
        <h5><?php echo number_format($adminCount); ?></h5>
        <p>Total Admins</p>
      </div>
      
      <div class="stat-card">
        <i class="fas fa-poll"></i>
        <h5><?php echo number_format($resultCount); ?></h5>
        <p>Result Records</p>
      </div>
      
      <div class="stat-card">
        <i class="fas fa-calendar-alt"></i>
        <h5><?php echo number_format($sessionCount); ?></h5>
        <p>Academic Sessions</p>
      </div>
    </div>

    <!-- Dashboard Groups (keep your existing group cards) -->
    <div class="dashboard-container">
      <!-- 1. Manage Learners -->
      <div class="group-card">
        <div class="group-header group-1">
          <i class="fas fa-users"></i>
          <span>Manage Learners</span>
        </div>
        <div class="group-body">
          <a href="add_students.php" class="group-item">
            <i class="fas fa-user-plus"></i>
            <span>Register New Student</span>
          </a>
          <a href="students.php" class="group-item">
            <i class="fas fa-users"></i>
            <span>Manage Students</span>
          </a>
          <a href="deactivate_students.php" class="group-item">
            <i class="fas fa-user-lock"></i>
            <span>Activate/Deactivate</span>
          </a>
        </div>
      </div>

      <!-- Continue with your existing group cards 2-10 here... -->
      <!-- 2. Manage Teachers & Admins -->
      <div class="group-card">
        <div class="group-header group-2">
          <i class="fas fa-chalkboard-teacher"></i>
          <span>Manage Teachers & Admins</span>
        </div>
        <div class="group-body">
          <a href="teachers.php?action=add" class="group-item">
            <i class="fas fa-user-plus"></i>
            <span>Register New Teacher</span>
          </a>
          <a href="teachers.php" class="group-item">
            <i class="fas fa-user-tie"></i>
            <span>Manage Teachers</span>
          </a>
          <a href="list_admin.php" class="group-item">
            <i class="fas fa-user-shield"></i>
            <span>Manage Admins</span>
          </a>
          <a href="lesson_plan/lesson_plan_list.php" class="group-item">
            <i class="fas fa-tasks"></i>
            <span>Lesson Plans</span>
          </a>
        </div>
      </div>

      <!-- 3. Upload Results, Psychomotor, and Comments -->
      <div class="group-card">
        <div class="group-header group-3">
          <i class="fas fa-poll"></i>
          <span>Upload Results & Assessments</span>
        </div>
        <div class="group-body">
          <a href="upload_results.php" class="group-item">
            <i class="fas fa-file-csv"></i>
            <span>Upload Results</span>
          </a>
          <a href="post_attendance.php" class="group-item">
            <i class="fas fa-file-csv"></i>
            <span>Upload Attendance & Comments</span>
          </a>
          <a href="post_skills.php" class="group-item">
            <i class="fas fa-file-csv"></i>
            <span>Upload Psychomotor Skills</span>
          </a>
          <a href="download_broadsheet.php" class="group-item">
            <i class="fas fa-file-pdf"></i>
            <span>View/Print Broadsheet</span>
          </a>
        </div>
      </div>

      <!-- 4. Vet Result Scores & Comments -->
      <div class="group-card">
        <div class="group-header group-4">
          <i class="fas fa-check-double"></i>
          <span>Vet Result Scores & Comments</span>
        </div>
        <div class="group-body">
          <a href="edit_scores_subject.php" class="group-item">
            <i class="fas fa-edit"></i>
            <span>Vet Scores by Subjects</span>
          </a>
          <a href="list_uploaded_results.php" class="group-item">
            <i class="fas fa-eye"></i>
            <span>View/Delete Uploaded Results</span>
          </a>
          <a href="edit_comments_attendance.php" class="group-item">
            <i class="fas fa-comments"></i>
            <span>Vet Attendance & Comments</span>
          </a>
          <a href="edit_skills.php" class="group-item">
            <i class="fas fa-brain"></i>
            <span>Vet Psychomotor Skills</span>
          </a>
        </div>
      </div>

      <!-- 5. Manage CBT -->
      <div class="group-card">
        <div class="group-header group-5">
          <i class="fas fa-laptop-code"></i>
          <span>Manage CBT</span>
        </div>
        <div class="group-body">
          <a href="cbt/cbt_admin_home.php" class="group-item">
            <i class="fas fa-laptop"></i>
            <span>Manage CBT</span>
          </a>
          <a href="https://cbt.hris.com.ng/entrance/login.php" class="group-item" target="_blank" rel="noopener noreferrer">
            <i class="fas fa-laptop"></i>
            <span>Manage ENTRANCE CBT</span>
          </a>
          <a href="cbt/generate_access_key.php" class="group-item">
            <i class="fas fa-key"></i>
            <span>Generate Access Code</span>
          </a>
        </div>
      </div>

      <!-- 6. Manage Complaints -->
      <div class="group-card">
        <div class="group-header group-6">
          <i class="fas fa-exclamation-circle"></i>
          <span>Manage Complaints</span>
        </div>
        <div class="group-body">
          <a href="complaint_admin.php" class="group-item">
            <i class="fas fa-comment-dots"></i>
            <span>Manage Complaints</span>
          </a>
        </div>
      </div>

      <!-- 7. Manage PINs -->
      <div class="group-card">
        <div class="group-header group-7">
          <i class="fas fa-key"></i>
          <span>Manage PINs</span>
        </div>
        <div class="group-body">
          <a href="generate_pins.php" class="group-item">
            <i class="fas fa-key"></i>
            <span>Generate PINs</span>
          </a>
          <a href="find_student_pin.php" class="group-item">
            <i class="fas fa-search"></i>
            <span>Find a Student's PIN</span>
          </a>
          <a href="pin_user.php" class="group-item">
            <i class="fas fa-check-circle"></i>
            <span>Confirm Pin Usage & Validity</span>
          </a>
          <a href="edit_pin.php" class="group-item">
            <i class="fas fa-edit"></i>
            <span>Edit PIN</span>
          </a>
          <a href="find_pin_user.php" class="group-item">
            <i class="fas fa-user-check"></i>
            <span>Confirm PIN User</span>
          </a>
        </div>
      </div>

      <!-- 8. Academic Analysis -->
      <div class="group-card">
        <div class="group-header group-8">
          <i class="fas fa-chart-bar"></i>
          <span>Academic Analysis</span>
        </div>
        <div class="group-body">
          <a href="result_analysis.php" class="group-item">
            <i class="fas fa-chart-line"></i>
            <span>Result Analysis</span>
          </a>
          <a href="#" class="group-item">
            <i class="fas fa-trophy"></i>
            <span>Top Performers</span>
          </a>
          <a href="#" class="group-item">
            <i class="fas fa-chart-pie"></i>
            <span>Subject Analysis</span>
          </a>
        </div>
      </div>

      <!-- 9. Assignments -->
      <div class="group-card">
        <div class="group-header group-9">
          <i class="fas fa-tasks"></i>
          <span>Assignments</span>
        </div>
        <div class="group-body">
          <a href="assignments.php" class="group-item">
            <i class="fas fa-book-open"></i>
            <span>Manage Assignments</span>
          </a>
        </div>
      </div>

      <!-- 10. Other Modules -->
      <div class="group-card">
        <div class="group-header group-10">
          <i class="fas fa-cubes"></i>
          <span>Other Modules</span>
        </div>
        <div class="group-body">
          <a href="post_session.php" class="group-item">
            <i class="fas fa-calendar-plus"></i>
            <span>Academic Year</span>
          </a>
          <a href="post_classes.php" class="group-item">
            <i class="fas fa-building"></i>
            <span>Classes</span>
          </a>
          <a href="post_subjects.php" class="group-item">
            <i class="fas fa-book"></i>
            <span>Subjects</span>
          </a>
          <a href="timetable.php" class="group-item">
            <i class="fas fa-calendar-alt"></i>
            <span>Time-Table</span>
          </a>
        </div>
      </div>
    </div>

    <!-- Uploaded Results Section -->
    <div class="card m-3" style="border-color: var(--secondary-gold);">
      <div class="card-header" style="background: linear-gradient(135deg, var(--primary-blue), var(--dark-blue)); color: white; border-bottom: 2px solid var(--secondary-gold);">
        <h6 class="mb-0"><i class="fas fa-calendar-alt me-2" style="color: var(--secondary-gold);"></i>Uploaded Results by Session</h6>
      </div>
      <div class="card-body">
        <div class="row">
          <?php
          $STH = $DBH->prepare("SELECT count(*) FROM tbl_student_results WHERE id >= ?");
          $result = $STH->execute(array(1));
          $count = $STH->fetchColumn();
          
          if($count >= 1) {
            $rows = sql($DBH, "SELECT MIN(id) AS id, year FROM tbl_student_results WHERE id >= ? GROUP BY year", 
            array(1), "rows");
            foreach($rows as $row) {  
              $t_year = $row['year'];
              echo '
              <div class="col-md-3 mb-3">
                <a href="main_term.php?id='.$t_year.'" target="_blank" class="btn w-100" style="background: linear-gradient(135deg, var(--primary-blue), var(--dark-blue)); color: white; border: 1px solid var(--secondary-gold);">
                  <i class="fas fa-calendar-alt me-1" style="color: var(--secondary-gold);"></i> '.$t_year.'
                </a>
              </div>';
            }
          } else {
            echo '<div class="col-12 text-center py-4">
              <i class="fas fa-info-circle fa-2x mb-3" style="color: var(--primary-blue);"></i>
              <p class="text-muted">No academic records found yet</p>
            </div>';
          }
          ?>
        </div>
      </div>
    </div>
  </main>

  <!-- Footer -->
  <footer class="footer">
    <div class="container-fluid">
      <div class="row align-items-center">
        <div class="col-md-6">
          <span class="copyright-text">
            <i class="fas fa-crown me-1" style="color: var(--secondary-gold);"></i>
            <?php echo $footerCopyright; ?> <?php echo date("Y"); ?>
            <?php if ($branchCode): ?>
            <span class="branch-badge ms-2"><?php echo strtoupper($branchCode); ?></span>
            <?php endif; ?>
          </span>
        </div>
        <div class="col-md-6">
          <div class="powered-by">
            <a href="<?php echo $developerWebsite; ?>" target="_blank" rel="noopener noreferrer" class="powered-by-link">
              <span>Powered by <?php echo $developerName; ?></span>
              <img src="datalogo.png" alt="<?php echo $developerName; ?> Logo" onerror="this.style.display='none';">
            </a>
          </div>
        </div>
      </div>
    </div>
  </footer>

  <!-- Bootstrap 5 JS Bundle with Popper -->
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
  
  <!-- Custom Scripts -->
  <script>
    // Toggle sidebar on mobile
    document.getElementById('sidebarToggle').addEventListener('click', function() {
      document.getElementById('sidebar').classList.toggle('active');
      document.querySelector('.main-content').classList.toggle('active');
    });
    
    // Close sidebar when clicking outside on mobile
    document.addEventListener('click', function(event) {
      const sidebar = document.getElementById('sidebar');
      const sidebarToggle = document.getElementById('sidebarToggle');
      
      if (window.innerWidth < 992 && 
          !sidebar.contains(event.target) && 
          !sidebarToggle.contains(event.target) && 
          sidebar.classList.contains('active')) {
        sidebar.classList.remove('active');
        document.querySelector('.main-content').classList.remove('active');
      }
    });
    
    // Auto-hide alerts
    setTimeout(function() {
      const alerts = document.querySelectorAll('.alert');
      alerts.forEach(function(alert) {
        alert.style.transition = 'opacity 0.5s ease';
        alert.style.opacity = '0';
        setTimeout(() => alert.remove(), 500);
      });
    }, 5000);
  </script>
</body>
</html>