<?php
// CLEAR REJECTED APPLICATIONS
if(isset($_POST['clear_rejected'])){
    $delete_query = "DELETE FROM applications WHERE status='REJECTED'";
    mysqli_query($connection,$delete_query);
    echo "<script>alert('All rejected applications cleared successfully');</script>";
    echo "<script>window.location.href='';</script>";
}
?>

<div class='page-title'>
  <div>
    <h1><i class='fa fa-user'></i> Rejected Admissions</h1>
    <p>List of Rejected Applications</p>
  </div>  
</div>

<div class='card'>
	<div class="panel panel-primary">

		<div class="panel-heading">
			<h4 class="text-center">Rejected Applications</h4>
		</div>

		<div class="panel-body">

            <!-- CLEAR BUTTON -->
            <div style="margin-bottom:15px;">
                <form method="POST" onsubmit="return confirm('Are you sure you want to delete ALL rejected applications?');">
                    <button type="submit" name="clear_rejected" class="btn btn-danger">
                        <i class="fa fa-trash"></i> Clear All Rejected
                    </button>
                </form>
            </div>

			<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>
						</tr>
					</thead>
					<tbody>

						<?php
						$query = "SELECT * FROM `applications` WHERE `status` = 'REJECTED'";
						$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>
								</tr>
								";
							}
						} else {
							echo "
							<tr>
								<td colspan='6' class='text-center text-danger'><b>No rejected applications found!!!!</b></td>
							</tr>
							";
						}
						?>

					</tbody>
				</table>
			</div>

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