
<div class="card">
    <div class='row'>
        <div class='col-xs-12 col-sm-12 col-md-12'>
            <div class='panel panel-primary ch'>
                <div class='panel-heading'>
                    <h4 class='text-center'>CLASS MANAGEMENT</h4>
                </div>
                
                <div class='panel-body'>
           			<div class='mb-3 text-right'>
						<button class='btn btn-sm btn-success' data-toggle='modal' data-target='#addClassModal'>➕ Add Class</button>
					</div>
					
                    <?php
$query = "SELECT * FROM `classes`";
$run_query = mysqli_query($connection, $query);
if (mysqli_num_rows($run_query) > 0):
    $i = 0;
?>
<div class="table-responsive">
    <table class="table table-hover table-bordered" id="myTable">
        <thead>
            <tr>
                <th>S/N</th>
                <th>Classes</th>
                <th>Level</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            <?php while ($result = mysqli_fetch_assoc($run_query)):
                $i++;
                $class_id = $result['id'];
                $all_class = $result['classes'];
                $all_level = $result['level'];
            ?>
            <tr>
                <td><?= $i ?></td>
                <td><?= htmlspecialchars($all_class ?? '') ?></td>
                <td><?= htmlspecialchars($all_level?? '') ?></td>
                <td>
                    <form method="POST" action="" style="display:inline-block">
                        <input type="hidden" name="hide_class_del" value="<?= $class_id ?>" />
                        <input type="submit" name="delete_class_btn" value="Delete" class="btn btn-xs btn-danger" />
                    </form>

                    <button 
                        type="button" 
                        class="btn btn-xs btn-primary" 
                        data-toggle="modal" 
                        data-target="#editClassModal<?= $class_id ?>">
                        Edit
                    </button>

                    <!-- Edit Modal -->
                    <div class="modal fade" id="editClassModal<?= $class_id ?>" tabindex="-1" role="dialog" aria-labelledby="editClassModalLabel<?= $class_id ?>" aria-hidden="true">
                        <div class="modal-dialog" role="document">
                            <div class="modal-content">
                                <form method="POST" action="">
                                    <div class="modal-header">
                                        <h5 class="modal-title" id="editClassModalLabel<?= $class_id ?>">Edit Class</h5>
                                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                            <span aria-hidden="true">&times;</span>
                                        </button>
                                    </div>
                                    <div class="modal-body">
                                        <input type="hidden" name="edit_class_id" value="<?= $class_id ?>" />
                                        <div class="form-group">
                                            <label>Class Name</label>
                                            <input type="text" name="edit_class_name" class="form-control" value="<?= htmlspecialchars($all_class ?? '') ?>" required />
                                        </div>
                                        <div class="form-group">
                                            <label>Class Level</label>
                                            <select class="form-control" name="edit_class_level" required>
												<option value="">--Select Level--</option>
												<?php
												$level_query = 'SELECT * FROM `class_level`';
												$run_level_query = mysqli_query($connection, $level_query);
												if (mysqli_num_rows($run_level_query) > 0) {
													while ($level_result = mysqli_fetch_assoc($run_level_query)) {
														$level = $level_result['level'];
														$selected = (isset($all_level) && $level == $all_level) ? 'selected' : '';
														echo "<option value='" . htmlspecialchars($level ?? '') . "' $selected>" . htmlspecialchars($level ?? '') . "</option>";
													}
												}
												?>
											</select>
                                        </div>
                                    </div>
                                    <div class="modal-footer">
                                        <button type="submit" name="update_class_btn" class="btn btn-success">Save changes</button>
                                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </td>
            </tr>
            <?php endwhile; ?>
        </tbody>
    </table>
</div>
<?php endif; ?>

                </div>
            </div>
        </div>
    </div>
</div>
<!-- Add Class Modal -->
					<div class='modal fade' id='addClassModal' tabindex='-1' role='dialog' aria-labelledby='addClassModalLabel' aria-hidden='true'>
						<div class='modal-dialog' role='document'>
							<div class='modal-content'>
								<form method='POST' action=''>
									<div class='modal-header'>
										<h5 class='modal-title' id='addClassModalLabel'>Add New Class</h5>
										<button type='button' class='close' data-dismiss='modal' aria-label='Close'>
										    <span aria-hidden='true'>&times;</span>
										</button>
									</div>
									<div class='modal-body'>
										<div class='form-group'>
										    <label>Class Name</label>
										    <input type='text' name='new_class_name' class='form-control' placeholder='Enter class name' required />
										</div>
										<div class='form-group'>
										    <label>Class Level</label>
										    <select class="form-control" name="class_level" required>
												<option value="" selected>--Select Level--</option>
												<?php
												$level_query = 'SELECT * FROM `class_level`';
												$run_level_query = mysqli_query($connection, $level_query);
												if (mysqli_num_rows($run_level_query) > 0) {
													while ($level_result = mysqli_fetch_assoc($run_level_query)) {
														$level = $level_result['level'];
														echo "<option value='" . htmlspecialchars($level) . "'>" . htmlspecialchars($level) . "</option>";
													}
												}
												?>
											</select>
										</div>
									</div>
									<div class='modal-footer'>
										<button type='submit' name='add_class_btn' class='btn btn-primary'>Add Class</button>
										<button type='button' class='btn btn-secondary' data-dismiss='modal'>Cancel</button>
									</div>
								</form>
							</div>
						</div>
					</div>
