<?php
session_start();
require_once 'inc/connect.php';

if (!isset($_SESSION['admin_id'])) {
    header('Location: admin_login.php');
    exit;
}

// Quick counts from DB
function get_count($mysqli, $table, $where = '1') {
    $result = mysqli_query($mysqli, "SELECT COUNT(*) AS total FROM `$table` WHERE $where");
    $row = mysqli_fetch_assoc($result);
    return intval($row['total']);
}

$total_students = get_count($mysqli, 'student_table', "status = 'active'");
$total_exams = get_count($mysqli, 'examination_table', "admin_status = 'active'");
$total_questions = get_count($mysqli, 'question_bank', "status = 'active' GROUP BY question_id");
$total_tokens = get_count($mysqli, 'token_management', "status = 'unused'");

    $result = mysqli_query($mysqli, "SELECT *  FROM question_bank WHERE status = 'active' GROUP BY question_id");
    $total_questions = $result->num_rows;
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Admin Dashboard</title>
  <link rel="stylesheet" href="/plugins/fontawesome-free/css/all.min.css">
  <link rel="stylesheet" href="/plugins/icheck-bootstrap/icheck-bootstrap.min.css">
  <link rel="stylesheet" href="/dist/css/adminlte.min.css">
</head>
<body class="hold-transition sidebar-mini layout-fixed">
<div class="wrapper">
  <?php include 'inc/admin_nav.php'; // optional nav include ?>


  <!-- Content -->
  <div class="content-wrapper">
    <div class="content-header">
      <div class="container-fluid"><h1 class="m-0 text-dark">Admin Dashboard</h1></div>
    </div>

    <section class="content">
      <div class="container-fluid">
        <!-- Info boxes -->
        <div class="row">
          <div class="col-lg-3 col-6">
            <div class="small-box bg-info">
              <div class="inner">
                <h3><?= $total_students ?></h3>
                <p>Active Students</p>
              </div>
              <div class="icon"><i class="fas fa-user-graduate"></i></div>
              <a href="manage_students.php" class="small-box-footer">More info <i class="fas fa-arrow-circle-right"></i></a>
            </div>
          </div>
          
          <div class="col-lg-3 col-6">
            <div class="small-box bg-success">
              <div class="inner">
                <h3><?= $total_exams ?></h3>
                <p>Active Exams</p>
              </div>
              <div class="icon"><i class="fas fa-file-alt"></i></div>
              <a href="manage_exams.php" class="small-box-footer">More info <i class="fas fa-arrow-circle-right"></i></a>
            </div>
          </div>
          
          <div class="col-lg-3 col-6">
            <div class="small-box bg-warning">
              <div class="inner">
                <h3><?= $total_questions ?></h3>
                <p>Questions in Bank</p>
              </div>
              <div class="icon"><i class="fas fa-question-circle"></i></div>
              <a href="manage_questions.php" class="small-box-footer">More info <i class="fas fa-arrow-circle-right"></i></a>
            </div>
          </div>
          
          <div class="col-lg-3 col-6">
            <div class="small-box bg-danger">
              <div class="inner">
                <h3><?= $total_tokens ?></h3>
                <p>Unused Tokens</p>
              </div>
              <div class="icon"><i class="fas fa-key"></i></div>
              <a href="manage_tokens.php" class="small-box-footer">More info <i class="fas fa-arrow-circle-right"></i></a>
            </div>
          </div>
        </div>
        <!-- /.row -->
      </div>
    </section>
  </div>

  <footer class="main-footer text-sm">
    <strong>&copy; <?= date('Y') ?> Exam System</strong> All rights reserved.
  </footer>

</div>

<script src="/plugins/jquery/jquery.min.js"></script>
<script src="/plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="/dist/js/adminlte.min.js"></script>
</body>
</html>
