<div class='page-title'>
    <div>
        <h1><i class='icon-graduation icons'></i> Student Management</h1>
        <p>View Students/Pupils</p>
    </div>
</div>

<div class="card">
    <div class="row">
        <div class="col-12">
            <div class="panel panel-primary ch">
                <div class="panel-body">                    
                    <form method="POST" action="">
                        <div class="row align-items-end">
                            <div class="col-md-4">
                                <div class="input-group">
                                    <span class="input-group-addon">CLASS</span>
                                    <select name="reg_no_class" class="form-control" required>
                                        <option value="">Select Class</option>
                                        	<?php																
								// Query only classes assigned to this teacher
								$query = "SELECT DISTINCT class FROM teacher_classes WHERE teacher_id = '$staff_id'";
								$run_query = mysqli_query($connection, $query);

								if (mysqli_num_rows($run_query) > 0) {
									while ($result = mysqli_fetch_assoc($run_query)) {
										$class = htmlspecialchars($result['class']);
										echo "<option value='{$class}'>{$class}</option>";
									}
								} else {
									echo "<option disabled>No class assigned</option>";
								}
								?>
                                    </select>
                                </div>
                            </div>
                            <div class="col-md-2">
                                <button type="submit" name="get_reg_no_btn" class="btn btn-primary btn-md">VIEW</button>
                            </div>
                        </div>
                    </form>

                    <br />

<?php
if (isset($_POST['get_reg_no_btn'])) {
    $reg_no_class = inject_checker($connection, $_POST['reg_no_class']);

    if (empty($reg_no_class)) {
        $_SESSION['msg'] = "Please select a class.";
        $_SESSION['type'] = "warning"; 
    } else {
        $stmt = $connection->prepare("
            SELECT * 
            FROM students 
            WHERE class = ? 
              AND status = 'active' 
            ORDER BY firstname ASC
        ");
        $stmt->bind_param("s", $reg_no_class);
        $stmt->execute();
        $result = $stmt->get_result();
        if ($result->num_rows > 0) {
            echo "<div class='table-responsive'>
                    <table class='table table-bordered' id='myTable'>
                        <thead>
                            <tr>
                                <th>S/N</th>
                                <th>IMAGE</th>
                                <th>NAME</th>
                                <th>SEX</th>
                                <th>CLASS</th>
                                <th>ADM NUMBER</th>
                                <th>ACTION</th>
                            </tr>
                        </thead>
                        <tbody>";
            $i = 0;
            while ($row = $result->fetch_assoc()) {
                $i++;
                $edit_student_id = $row['id'];
                $firstname = htmlspecialchars($row['firstname']);
                $lastname = htmlspecialchars($row['lastname']);
                $othername = htmlspecialchars($row['othername']);
                $fullname = ucwords("$firstname $othername $lastname");
                $sex = ucfirst($row['gender']);
                $height = htmlspecialchars($row['height']);
                $weight = htmlspecialchars($row['weight']);
                $club = htmlspecialchars($row['club']);
                $class = htmlspecialchars($row['class']);
                $level = htmlspecialchars($row['level']);
                $reg_number = htmlspecialchars($row['reg_number']);
                $health_conditions = htmlspecialchars($row['health_conditions']);
                $nationality = htmlspecialchars($row['nationality']);                
                $passport = !empty($row['passport']) ? "../" . $row['passport'] : "../uploads/students/none.jpg";
                $dob = htmlspecialchars($row['dob']);
                $state = htmlspecialchars($row['state']);
                $lga = htmlspecialchars($row['lga']);
                $address = htmlspecialchars($row['address']);
                $guardian_name = htmlspecialchars($row['guardian_name']);
                $guardian_occupation = htmlspecialchars($row['guardian_occupation']);
                $guardian_phone = htmlspecialchars($row['guardian_phone']);

                echo "<tr>
                        <td>{$i}</td>
                        <td class='text-center'><img src='{$passport}' alt='Passport' width='40' height='40' class='img-thumbnail'></td>
                        <td>{$fullname}</td>
                        <td class='text-center'>{$sex}</td>
                        <td class='text-center'>{$class}</td>
                        <td class='text-center'>{$reg_number}</td>
                        <td class='text-center'>
                            <form method='POST' style='display:inline-block;'>
                                <input type='hidden' name='student_hidden_id' value='{$edit_student_id}'>
                                <div class='btn-group btn-group-sm' role='group'>
                                    <button type='button' class='btn btn-secondary' title='View Student'
                                            data-toggle='modal' data-target='#viewStudentModal{$edit_student_id}'>
                                        <i class='fa fa-eye'></i>
                                    </button>

                                    <div class='modal fade' id='viewStudentModal{$edit_student_id}' tabindex='-1' role='dialog' aria-labelledby='viewStudentModalLabel{$edit_student_id}' aria-hidden='true'>
                                        <div class='modal-dialog modal-lg' role='document'>
                                            <div class='modal-content'>
                                                <div class='modal-header'>
                                                    <h2 class='modal-title'>Student Details - {$fullname}</h2>
                                                    <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='row'>
                                                        <div class='col-md-4 text-center mb-3'>
                                                            <img src='{$passport}' class='img-thumbnail' alt='Passport' style='width: 150px; height: 150px;'>
                                                        </div>
                                                        <div class='col-md-8'>
                                                            <table class='table table-bordered table-sm'>
                                                                <tr><th>Full Name</th><td>{$fullname}</td></tr>
                                                                <tr><th>Reg Number</th><td>{$reg_number}</td></tr>
                                                                <tr><th>Class</th><td>{$class}</td></tr>
                                                                <tr><th>Level</th><td>{$level}</td></tr>
                                                                <tr><th>Gender</th><td>{$sex}</td></tr>
                                                                <tr><th>Date of Birth</th><td>{$dob}</td></tr>
                                                                <tr><th>Height</th><td>{$height}ft</td></tr>
                                                                <tr><th>Weight</th><td>{$weight}kg</td></tr>
                                                                <tr><th>Health Conditions</th><td>{$health_conditions}</td></tr>
                                                                <tr><th>LGA</th><td>{$lga}</td></tr>
                                                                <tr><th>State</th><td>{$state}</td></tr>
                                                                <tr><th>Nationality</th><td>{$nationality}</td></tr>
                                                                <tr><th>Address</th><td>{$address}</td></tr>
                                                                <tr><th>Club</th><td>{$club}</td></tr>
                                                                <tr><th>Guardian Name</th><td>{$guardian_name}</td></tr>
                                                                <tr><th>Guardian Occupation</th><td>{$guardian_occupation}</td></tr>
                                                                <tr><th>Guardian Phone</th><td>{$guardian_phone}</td></tr>
                                                            </table>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class='modal-footer'>
                                                    <button type='button' class='btn btn-secondary' data-dismiss='modal'>Close</button>
                                                </div>
                                            </div>
                                        </div>
                                    </div>

                                    <a href='slip.php?id={$edit_student_id}' class='btn btn-info' title='Print Slip' target='_blank'>
                                        <i class='fa fa-print'></i>
                                    </a>
                                </div>
                            </form>
                        </td>
                    </tr>";
            }
            // Correctly close the table outside the loop
            echo "</tbody></table></div>";
        } else {
            $_SESSION['msg'] = "No student found in {$reg_no_class}.";
            $_SESSION['type'] = "warning";          
        }

        $stmt->close();
    }
}
?>

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


