<?php
session_start(); // Required to use $_SESSION
require_once("public/connection.php");

if (isset($_POST['approve_application_btn'])) {
    $application_id = $_POST['application_id'];
    $query_approve = "UPDATE `applications` SET `status` = 'APPROVED' WHERE `id` = '$application_id'";
    if (mysqli_query($connection, $query_approve)) {
        $_SESSION['msg'] = "Application approved successfully.";
        $_SESSION['type'] = "success";
    } else {
        $_SESSION['msg'] = "Failed to approve application.";
        $_SESSION['type'] = "danger";
    }
    header("Location: admission_request");
    exit();
}

if (isset($_POST['reject_application_btn'])) {
    $application_id = $_POST['application_id'];
    $query_reject = "UPDATE `applications` SET `status` = 'REJECTED' WHERE `id` = '$application_id'";
    if (mysqli_query($connection, $query_reject)) {
        $_SESSION['msg'] = "Application rejected successfully.";
        $_SESSION['type'] = "warning";
    } else {
        $_SESSION['msg'] = "Failed to reject application.";
        $_SESSION['type'] = "danger";
    }
    header("Location: admission_request");
    exit();
}
?>


<div class='page-title'>
  <div>
    <h1><i class='fa fa-user'></i> Manage Admission Requests</h1>
    <p>Pending Applications</p>
  </div>  
</div>
   <?php
        if(isset($msg)){
            echo $msg;
        }
    ?>
<div class='card'>
	<div class="panel panel-primary">
		<div class="panel-heading">
			<h4 class="text-center">Pending Applications</h4>
		</div>
		<div class="panel-body">
			<div class="table-responsive">
				<table class="table table-bordered">
					<thead>
						<tr>
							<th>S/N</th>
							<th>Application Number</th>
							<th>Name</th>
							<th>Gender</th>
							<th>Date of Birth</th>
							<th>Class</th>
							<th>Action</th>
						</tr>
					</thead>
					<tbody>
						<?php
						$query = "SELECT * FROM `applications` WHERE `status` = 'pending'";
						$run_query = mysqli_query($connection, $query);

						if (mysqli_num_rows($run_query) > 0) {
							$i = 0;
							while ($result = mysqli_fetch_assoc($run_query)) {
								$i++;
								$name = $result['fname'] . ' ' . $result['mname'] . ' ' . $result['oname'];
								echo "
								<tr>
									<td>{$i}</td>
									<td>{$result['application_number']}</td>
									<td>{$name}</td>
									<td>{$result['gender']}</td>
									<td>{$result['dob']}</td>
									<td>{$result['class']}</td>
									<td>
										<form method='POST' style='display: inline;'>
											<input type='hidden' name='application_id' value='{$result['id']}' />
											<button type='submit' name='approve_application_btn' class='btn btn-success btn-sm'>Approve</button>
										</form>
										<form method='POST' style='display: inline;'>
											<input type='hidden' name='application_id' value='{$result['id']}' />
											<button type='submit' name='reject_application_btn' class='btn btn-danger btn-sm'>Reject</button>
										</form>
									</td>
								</tr>
								";
							}
						} else {
							echo "
							<tr>
								<td colspan='7' class='text-center text-danger'><b>No applications for now!!!!</b></td>
							</tr>
							";
						}
						?>
					</tbody>
				</table>
			</div>
		</div>
	</div>
</div>
