<div class="page-title">
  <div>
    <h1><i class="fa fa-calendar"></i> Subjects you are to offer</h1>
    <p>The following subjects are the specified subjects for your class</p>
  </div>
</div>
<div class="card">
    <div class='row'>
        <div class='col-xs-12 col-sm-12 col-md-10'>
			<div class = 'panel panel-primary ch'>
                <div class = 'panel-heading'>
					<?php
					// Get the required subject count for the current student level
					$required_subjects = null;
					$level_safe = mysqli_real_escape_string($connection, $student_level);

					$query = "SELECT number_of_subject FROM subject_number_by_level WHERE level = '$level_safe' LIMIT 1";
					$result = mysqli_query($connection, $query);

					if ($row = mysqli_fetch_assoc($result)) {
						$required_subjects = (int) $row['number_of_subject'];
					}
					?>

					<h4 class='text-center'>
						All Subjects To Be Offered By <?php echo "<u>" . htmlspecialchars($student_class) . "</u>"; ?> Students
					</h4>

					<p class='text-center'>
						<?php if ($required_subjects !== null): ?>
							Note That You Must Register <strong>a Minimum of <?= $required_subjects ?> Subject<?= $required_subjects > 1 ? 's' : '' ?></strong> For Your Results To Be Out
						<?php else: ?>
							<span class='text-danger'><strong>Note: Required number of subjects for this level has not been set.</strong></span>
						<?php endif; ?>
					</p>

                </div>
				<?php
				// Display subjects from school_subject_levels table based on $student_level
				$query = "SELECT subject FROM school_subject_levels WHERE level = ?";
				$stmt = $connection->prepare($query);
				$stmt->bind_param("s", $student_level);
				$stmt->execute();
				$result = $stmt->get_result();

				if ($result->num_rows > 0) {
					$i = 0;
					echo "
						<div class='table-responsive panel-body'>
							<table class='table table-striped'>
								<thead>
								    <tr>
								        <th style='font-weight:bold;color:#074778;'>S/N</th>
								        <th style='font-weight:bold;color:#074778;'>Subject</th>
								    </tr>
								</thead>
								<tbody>
					";

					while ($row = $result->fetch_assoc()) {
						$i++;
						$subject = $row['subject'];
						echo "
							<tr>
								<td>{$i}</td>
								<td>{$subject}</td>
							</tr>
						";
					}

					echo "
								</tbody>
							</table>
						</div>
					";
				} else {
					echo "<div class='panel-body'><p class='text-center text-danger'>No subjects found for this level.</p></div>";
				}

				$stmt->close();
				?>

            </div>
        </div>
    </div>
</div>
