<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>

<?php
chdir('../');
session_start();
require_once('public/connection.php');

if (!isset($_SESSION['student_id'])) {
    header("Location: ../index.php");
    exit;
}

include('partials/header.php');
include('partials/navbar.php');
include('partials/sidebar.php');
include('partials/alert.php');

$student_id = $_SESSION['student_id'];

$stmt = $conn->prepare("SELECT * FROM students WHERE id = ?");
$stmt->bind_param("i", $student_id);
$stmt->execute();
$student = $stmt->get_result()->fetch_assoc();
$student_class = $student['class'];

// === Total attempts by student ===
$exam_attempts = $conn->query("SELECT COUNT(DISTINCT exam_id) as total FROM student_exam_attempts WHERE student_id = $student_id")->fetch_assoc()['total'] ?? 0;

// === Total unique exam groups for student class ===
$total_exams = $conn->query("SELECT COUNT(DISTINCT exam_group_id) as total FROM exams WHERE class = '$student_class'")->fetch_assoc()['total'] ?? 0;
?>


<div class="content-wrapper">
  <div class="content-header">
    <div class="container-fluid">
      <div class="row mb-2">
        <div class="col-sm-6">
          <h1 class="m-0">Welcome, <?= htmlspecialchars($student['firstname']) ?>!</h1>
        </div>
      </div>
    </div>
  </div>

  <div class="content">
    <div class="container-fluid">
      <div class="row g-3">
        <div class="col-lg-6 col-12">
          <div class="small-box bg-primary">
            <div class="inner">
              <h3><?= $total_exams ?></h3>
              <p>Total Available Exams</p>
            </div>
            <div class="icon">
              <i class="fas fa-book"></i>
            </div>
          </div>
        </div>
        <div class="col-lg-6 col-12">
          <div class="small-box bg-success">
            <div class="inner">
              <h3><?= $exam_attempts ?></h3>
              <p>Your Attempts</p>
            </div>
            <div class="icon">
              <i class="fas fa-check-circle"></i>
            </div>
          </div>
        </div>
      </div>

      <!-- Instruction/Guidelines Section -->
      <div class="card mt-4">
        <div class="card-header bg-warning text-white">
          <i class="fas fa-info-circle"></i> Important Instructions & Guidelines
        </div>
        <div class="card-body">
          <h5><strong>Online Exam Guidelines:</strong></h5>
          <ul>
            <li><strong>Ensure a Stable Internet Connection:</strong> A steady internet connection is essential to avoid interruptions during the exam.</li>
            <li><strong>No Malpractice:</strong> Cheating, copying from external sources, or communicating with others during the exam is strictly prohibited. Violators will be penalized.</li>
            <li><strong>Exam Time Limit:</strong> Be mindful of the time limit for each exam. Ensure that you complete the exam before the time expires.</li>
            <li><strong>Technical Issues:</strong> In case of any technical issues, contact your exam supervisor immediately.</li>
            <li><strong>Prohibited Actions:</strong> Opening unauthorized websites or applications during the exam is considered malpractice.</li>
          </ul>
          <p><strong>Important Note:</strong> Your exam attempt will be monitored, and any suspicion of malpractice will lead to an automatic disqualification.</p>
        </div>
      </div>

<!-- Upcoming Exams Section -->
<div class="card mt-4">
  <div class="card-header bg-info text-white">
    <i class="fas fa-clock"></i> Upcoming Exams
  </div>
  <div class="card-body">
    <?php
    $class = $student['class'];

    // Fetch distinct exam groups for the student's class
    $upcoming_groups = $conn->query("
        SELECT 
          eg.exam_group_id,
          e.term, e.questtype,
          e.session,
          s.subjects,
          MIN(e.exam_date) AS earliest_date
        FROM exams e
        JOIN school_subjects s ON e.subject_id = s.id
        JOIN (
          SELECT DISTINCT exam_group_id FROM exams WHERE class = '$class'
        ) eg ON eg.exam_group_id = e.exam_group_id
        WHERE e.class = '$class' AND e.exam_date >= CURDATE()
        GROUP BY eg.exam_group_id, e.term, e.session, s.subjects
        ORDER BY earliest_date ASC
        LIMIT 5
    ");

    if ($upcoming_groups->num_rows): ?>
      <ul class="list-group">
        <?php while($row = $upcoming_groups->fetch_assoc()): ?>
          <li class="list-group-item d-flex justify-content-between align-items-center">
              
              <?php
               $qtype =  htmlspecialchars($row['questtype']) ;  
               $guesttype = $qtype;
         
       
              
              
              ?>
            <span>
              <?= strtoupper($row['subjects']) ?> | Term: <?= $row['term'] ?>  | Question Type: <?php echo $guesttype; ?> | Session: <?= $row['session'] ?>
            </span>
            <div class="d-flex align-items-center gap-2">
              <span class="badge bg-primary">
                📅 <?= date('M j, Y', strtotime($row['earliest_date'])) ?>
              </span>
              <button class="btn btn-sm btn-outline-secondary" data-bs-toggle="modal" data-bs-target="#viewExamGroup<?= $row['exam_group_id'] ?>">
                View Details
              </button>
            </div>
          </li>

          <!-- Modal for Viewing Exam Group Details -->
          <div class="modal fade" id="viewExamGroup<?= $row['exam_group_id'] ?>" tabindex="-1" aria-labelledby="viewExamGroupLabel<?= $row['exam_group_id'] ?>" aria-hidden="true">
            <div class="modal-dialog modal-dialog-scrollable">
              <div class="modal-content">
                <div class="modal-header bg-info text-white">
                  <h5 class="modal-title" id="viewExamGroupLabel<?= $row['exam_group_id'] ?>">
                    Exam Group Details
                  </h5>
                  <button type="button" class="btn-close text-white" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                  <p><strong>Subject:</strong> <?= htmlspecialchars($row['subjects']) ?></p>
                  
                   <p><strong>Question Type:</strong> <?php echo $guesttype ?></p>
                  <p><strong>Term:</strong> <?= htmlspecialchars($row['term']) ?></p>
                  <p><strong>Session:</strong> <?= htmlspecialchars($row['session']) ?></p>
                  <p><strong>Earliest Exam Date:</strong> <?= htmlspecialchars(date('M j, Y', strtotime($row['earliest_date']))) ?></p>

                  <hr>
                  <h6 class="mb-2">Included Exams</h6>
                  <ul class="list-group">
                    <?php
                    // Fetch the objective/theory exams under this group
                    $group_id = $row['exam_group_id'];
                    $sub_exams = $conn->query("SELECT exam_type, exam_date, duration_minutes FROM exams WHERE exam_group_id = $group_id");
                    while ($ex = $sub_exams->fetch_assoc()):
                    ?>
                      <li class="list-group-item d-flex justify-content-between align-items-center">
                        <?= strtoupper($ex['exam_type']) ?> (<?= $ex['duration_minutes'] ?> mins)
                        <span class="badge bg-secondary"><?= date('M j, Y', strtotime($ex['exam_date'])) ?></span>
                      </li>
                    <?php endwhile; ?>
                  </ul>
                </div>
                <div class="modal-footer">
                  <button type="button" class="btn btn-sm btn-secondary" data-bs-dismiss="modal">Close</button>
                </div>
              </div>
            </div>
          </div>
        <?php endwhile; ?>
      </ul>
    <?php else: ?>
      <p class="text-muted">No upcoming exams for your class.</p>
    <?php endif; ?>
  </div>
</div>


<?php include('partials/footer.php'); ?>

