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

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


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

// Get only students who have been flagged
$query = "
    SELECT 
        s.id AS student_id, 
        s.reg_number, 
        CONCAT(s.firstname, ' ', s.lastname) AS full_name,
        s.class, 
        s.flagged,
        CONCAT(e.exam_type, ' - ', subj.subjects) AS exam_title
    FROM students s
    LEFT JOIN exam_results r ON r.student_id = s.id
    LEFT JOIN exams e ON r.exam_id = e.id
    LEFT JOIN school_subjects subj ON e.subject_id = subj.id
    WHERE s.flagged = 1
    GROUP BY s.id
    ORDER BY s.class ASC, s.firstname ASC
";

$result = $conn->query($query);
?>

<!-- Content Wrapper -->
<div class="content-wrapper">
  <div class="content-header">
    <div class="container-fluid">
      <h1 class="m-0">Flagged Students (Malpractice Cases)</h1>
    </div>
  </div>

  <div class="content">
    <div class="container-fluid">
      <?php if ($result && $result->num_rows > 0): ?>
        <table class="table table-bordered table-striped">
          <thead class="thead-dark">
            <tr>
              <th>#</th>
              <th>Reg Number</th>
              <th>Full Name</th>
              <th>Class</th>
              <th>Exam Title</th>
              <th>Action</th>
            </tr>
          </thead>
          <tbody>
            <?php $sn = 1; while ($row = $result->fetch_assoc()): ?>
              <tr>
                <td><?= $sn++ ?></td>
                <td><?= htmlspecialchars($row['reg_number']) ?></td>
                <td><?= htmlspecialchars($row['full_name']) ?></td>
                <td><?= htmlspecialchars($row['class']) ?></td>
                <td><?= htmlspecialchars($row['exam_title'] ?? 'Unknown') ?></td>
                <td>
                  <form method="post" action="unflagged.php" class="unflag-form">
                    <input type="hidden" name="student_id" value="<?= $row['student_id'] ?>">
                    <button type="submit" class="btn btn-warning btn-sm">Unflag</button>
                  </form>
                </td>
              </tr>
            <?php endwhile; ?>
          </tbody>
        </table>
      <?php else: ?>
        <div class="alert alert-info text-center">No students have been flagged for malpractice.</div>
      <?php endif; ?>
    </div>
  </div>
</div>

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

<!-- SweetAlert JS (CDN) -->
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
  document.addEventListener('DOMContentLoaded', function () {
    const unflagForms = document.querySelectorAll('.unflag-form');

    unflagForms.forEach(function (form) {
      form.addEventListener('submit', function (e) {
        e.preventDefault(); // prevent default form submission

        Swal.fire({
          title: 'Are you sure?',
          text: "This will unflag the student.",
          icon: 'warning',
          showCancelButton: true,
          confirmButtonColor: '#3085d6',
          cancelButtonColor: '#d33',
          confirmButtonText: 'Yes, unflag',
          cancelButtonText: 'Cancel'
        }).then((result) => {
          if (result.isConfirmed) {
            form.submit(); // only submit if confirmed
          }
        });
      });
    });
  });
</script>
