<?php
if (isset($_POST['del_students_reg_subjects'])) {
    $success = true;

    // Prepare the DELETE statement once
    $stmt = $connection->prepare("DELETE FROM subjects WHERE id = ?");
    if (!$stmt) {
        $_SESSION['msg'] = "❌ Failed to prepare statement.";
        $_SESSION['type'] = "error";
        header("Location: view_registered_subjects");
        exit();
    }

    for ($j = 0; $j < (int)$_POST["total_num"]; $j++) {
        $reg_subject_id = $_POST['sub_reg_id'][$j];

        // Bind and execute
        $stmt->bind_param("i", $reg_subject_id);
        if (!$stmt->execute()) {
            $success = false;
            break;
        }
    }

    $stmt->close();

    if ($success) {
        $_SESSION['msg'] = "Registered subjects deleted successfully.";
        $_SESSION['type'] = "success";
    } else {
        $_SESSION['msg'] = "❌ Something went wrong.<br /> Not all subjects were deleted.";
        $_SESSION['type'] = "error";
    }

    header("Location: view_registered_subjects");
    exit();
}
?>



<div class='page-title'>
  <div>
    <h1><i class='fa fa-laptop'></i> Subject Management</h1>
    <p>View Registered Student Subject</p>
  </div>
</div>
<div class='card'>
    <div class='panel panel-primary'>
        <div class='panel-body'>
            <form method='POST' action=''>
                <div class='row'>
                    <div class='col-md-3'>
						<div class='input-group'>
							<span class='input-group-addon' id='basic-addon2'>Class:</span>
							<select class='form-control' name='class' id='class-select' required>
								<option selected disabled>Select Class</option>
								<?php
								$query = "SELECT * FROM `classes`";
								$run_query = mysqli_query($connection, $query);
								if (mysqli_num_rows($run_query) > 0) {
									while ($result = mysqli_fetch_assoc($run_query)) {
										$attendance = $result['classes'];
										echo "<option value='{$attendance}'>{$attendance}</option>";
									}
								}
								?>
							</select>
						</div>
						<br />
					</div>

					<div class='col-md-3'>
						<div class='input-group'>
							<span class='input-group-addon' id='basic-addon2'>Subject:</span>
							<select class='form-control' name='subject' id='subject-select' required>
								<option selected>Select a Class First</option>
							</select>
						</div>
						<br />
					</div>

                    <div class='col-md-3'>
                        <div class='input-group'>
                            <span class='input-group-addon' id='basic-addon2'>Term:</span>
                            <select class='form-control' name='term'>
								<?php
									///// POST ACTION TO DISPLAY CURRRENT TERM /////////////
									$query = " SELECT * FROM `current_season` ";
									$run_query = mysqli_query($connection, $query);
									if(mysqli_num_rows($run_query) == 1){
										while($result = mysqli_fetch_assoc($run_query)){
											$show_current_term = $result['current_term'];
											echo"
												<option>{$show_current_term}</option>
											";
										}
									}
								?>
                            </select>
                        </div>
                        <br />
                    </div>

                    <div class='col-md-3'>
                        <div class='input-group'>
                            <span class='input-group-addon' id='basic-addon2'>Session:</span>
                            <select class='form-control' name='session'>
                                    <?php
                                        ///////////// POST ACTION TO DISPLAY CURRENT SESSION //////////////
                                        $query = " SELECT * FROM `current_season` ";
                                        $run_query = mysqli_query($connection, $query);
                                        if(mysqli_num_rows($run_query) == 1){
                                            while($result = mysqli_fetch_assoc($run_query)){
                                                $show_current_session = $result['current_session'];
                                                echo"
                                                    <option>{$show_current_session}</option>
                                                ";
                                            }
                                        }
                                    ?>
                            </select>
                        </div>
                        <br />
                    </div>
                    
                    <div class='col-md-3'>
                        <input type='submit' name='view_registered_subject' value='VIEW REGISTERED SUBJECT' class='btn btn-primary' />
                    </div>
                </div>
            </form>
            <br />
        </div>
    </div>
    <?php
        if(isset($_POST['view_registered_subject'])){
            $class = $_POST['class'];
            $subject = $_POST['subject'];
            $term = $_POST['term'];
            $session = $_POST['session'];
            
            if($class == $select || $term == $select){
                $msg = "<p class='text-danger'><b>No field should be empty</b></p>";
            }else{
                $query = " SELECT * FROM subjects WHERE class = '{$class}' AND subjects = '{$subject}' AND term = '{$term}' AND session = '{$session}' ORDER BY name ASC ";
                $run_query = mysqli_query($connection, $query);
                
                if(mysqli_num_rows($run_query) > 0){
                    $i = 0;
                    $num = 0;
                    echo "
                        <form method='POST' action=''>
                        <div class='table-responsive'>
                            <table class='table table-bordered table-condensed table-hover table-export' id='myTable'>
                                <thead>
                                    <tr>
                                        <th>S/N</th>
                                        <th>REG NO</th>
                                        <th>NAME</th>
                                        <th>CLASS</th>
                                        <th>SUBJECT</th>
                                        <th>TERM</th>
                                        <th>SESSION</th>
                                        <th>SELECT ALL <input type='checkbox' class='form-control' id='selectall' /></th>
                                    </tr>
                                </thead>
                                <tbody>
                    ";
                    while($result = mysqli_fetch_assoc($run_query)){
                        $i++;
                        echo "
                            
                                <tr>
                                    <td>{$i}</td>
                                    <td>{$result['reg_number']}</td>
                                    <td>{$result['name']}</td>
                                    <td>{$result['class']}</td>
                                    <td>{$result['subjects']}</td>
                                    <td>{$result['term']}</td>
                                    <td>{$result['session']}</td>
                                    <td>
                                        <input type='hidden' name='subject_reg_hidden_id[]' value='{$result['id']}' />
                                        <input type='checkbox' class='form-control subgroup' name='sub_reg_id[]' value='{$result['id']}' />
                                    </td>
                                </tr>
                            
                        ";
                        $num++;
                    }
                    echo "
                    		</tbody>
                            </table>
                            
                            <input type='hidden' name='total_num' value='{$num}' />
                            <button type = 'submit' name='del_students_reg_subjects' class='btn btn-danger btn-md pull-right' style='margin-left:20px;'><span class= 'glyphicon glyphicon-user'></span>Delete Selected</button><br/>
                            
                        </div>
                        </form>
                    ";
                }else{
                    echo "<h3 class='text-danger'>No registered subjects</h3>";
                }
            }
        }
    ?>
</div>
