<?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');
?>
<?php
$classes = $conn->query("SELECT DISTINCT class FROM students ORDER BY class ASC");
$class_filter = $_GET['class'] ?? '';

$query = "SELECT * FROM students";
if ($class_filter !== '') {
    $stmt = $conn->prepare("SELECT * FROM students WHERE class = ? ORDER BY firstname ASC");
    $stmt->bind_param("s", $class_filter);
    $stmt->execute();
    $students = $stmt->get_result();
} else {
    $students = null; // no query if no class selected
}
?>
  <!-- Content Wrapper -->
  <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">Students</h1>
          </div>
        </div>
      </div>
    </div>

    <div class="content">
      <div class="container-fluid">
        <form method="get" class="mb-4">
          <div class="form-group">
            <label>Select Class:</label>
            <select name="class" onchange="this.form.submit()" class="form-control" style="max-width: 300px;">
              <option value="">All Classes</option>
              <?php while ($row = $classes->fetch_assoc()): ?>
                <option value="<?= htmlspecialchars($row['class']) ?>" <?= $class_filter == $row['class'] ? 'selected' : '' ?>>
                  <?= htmlspecialchars($row['class']) ?>
                </option>
              <?php endwhile; ?>
            </select>
          </div>
        </form>
      <?php if ($class_filter !== '' && $students): ?>
  <table class="table table-bordered table-striped" id="myTable">
    <thead class="thead-dark">
      <tr>
        <th>#</th>
        <th>Passport</th>
        <th>Reg Number</th>
        <th>Full Name</th>
        <th>Gender</th>
        <th>Class</th>
      </tr>
    </thead>
    <tbody>
      <?php $sn = 1; while ($student = $students->fetch_assoc()): ?>
        <tr>
          <td><?= $sn++ ?></td>
          <td>
            <?php if (!empty($student['passport'])): ?>
              <img src="../../<?= $student['passport'] ?>" alt="N/A" width="40">
            <?php else: ?>
              N/A
            <?php endif; ?>
          </td>
          <td><?= htmlspecialchars($student['reg_number']) ?></td>
          <td><?= htmlspecialchars($student['firstname'] . ' ' . $student['lastname']) ?></td>
          <td><?= htmlspecialchars($student['gender']) ?></td>
          <td><?= htmlspecialchars($student['class']) ?></td>
        </tr>
      <?php endwhile; ?>
    </tbody>
  </table>
<?php elseif ($class_filter !== ''): ?>
  <div class="alert alert-warning">No students found for this class.</div>
<?php endif; ?>

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

