<div class='page-title'>
	<div>
		<h1><i class='fa fa-contact'></i> Generate Contact Numbers</h1>
	</div>	
</div>

<div class='card'>
	<div class="row">
		<div class="col-md-6">
			<div class='panel panel-primary'>
				<div class='panel-heading'>
					<h4 class='text-center'>Student Guardian's Contacts</h4>
				</div>
				<div class='panel-body'>
					<form method='POST' action=''>
						<div class='row'>
							<div class='col-md-8'>
								<div class='input-group'>
									<span class='input-group-addon' id='basic-addon2'>Student Class:</span>
									<select class='form-control' name='contact_class'>
										<option selected><?php echo htmlspecialchars($select); ?></option>
										<option>All</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 = htmlspecialchars($result['classes']);
													echo "<option>{$attendance}</option>";
												}
											}
										?>
									</select>
								</div>
							</div>
							<div class='col-md-2'>
								<input type='submit' name='generate_contact_btn' value='SHOW CONTACT' class='btn btn-primary' />
							</div>
						</div>
					</form>
					<br />
				</div>
			</div>

			<?php
				if (isset($_POST['generate_contact_btn'])) {
					$contact_class = mysqli_real_escape_string($connection, $_POST['contact_class']);

					if ($contact_class === "All") {
						$query = "SELECT firstname, lastname, othername, guardian_phone FROM students";
					} else {
						$query = "SELECT firstname, lastname, othername, guardian_phone FROM students WHERE class = '$contact_class'";
					}

					$run_query = mysqli_query($connection, $query);

					if (mysqli_num_rows($run_query) > 0) {
						echo "<table class='table table-bordered' id='myTable'>
								<thead>
									<tr>
										<th class='text-center'>Student Name</th>
										<th class='text-center'>Guardian's Contact</th>

									</tr>
								</thead>
								<tbody>";

						while ($result = mysqli_fetch_assoc($run_query)) {
							$student_name = htmlspecialchars($result['firstname']) . ' ' . htmlspecialchars($result['othername']) . ' ' . htmlspecialchars($result['lastname']);
							$guardian_contacts = htmlspecialchars($result['guardian_phone']);

							echo "<tr>
									<td class='text-center'>{$student_name}</td>
									<td class='text-center'>{$guardian_contacts}</td>

								  </tr>";
						}

						echo "  </tbody>
							  </table>";
					} else {
						 $_SESSION['msg']  = "No contacts found.";
						 $_SESSION['type'] = "error";
					}
				}
			?>
		</div>


        <div class="col-md-6">
            <div class='panel panel-primary'>
                <div class='panel-heading'>
                    <h4 class='text-center'>Staff Contacts</h4>
                </div>
                <div class='panel-body'>
                    <form method='POST' action=''>
                        <div class='row'>
                            <div class='col-md-2'>
                                <input type='submit' name='generate_teachers_contact_btn' value='SHOW CONTACT' class='btn btn-primary' />
                            </div>
                        </div>
                    </form>
                    <br />
                </div>
            </div>

			<?php
				if (isset($_POST['generate_teachers_contact_btn'])) {

					$query = "SELECT staff.*, role.role_name FROM staff 
							  LEFT JOIN role ON staff.role = role.id";
					$run_query = mysqli_query($connection, $query);

					if (mysqli_num_rows($run_query) > 0) {
						$i = 0;
						$staffList = [];
						echo "<table class='table table-bordered' id='myTable'>
								<thead>
									<tr>
										<th class='text-center'>Name</th>
										<th class='text-center'>Role</th>
										<th class='text-center'>Contact</th>
									</tr>
								</thead>
								<tbody>";

						while ($result = mysqli_fetch_assoc($run_query)) {
							$staff_name = htmlspecialchars($result['name']);
							$staff_role = htmlspecialchars($result['role_name']);
							$staff_contact = htmlspecialchars($result['phone']);

							echo "<tr>
									<td class='text-center'>{$staff_name}</td>
									<td class='text-center'>{$staff_role}</td>
									<td class='text-center'>{$staff_contact}</td>
								  </tr>";
						}

						echo "</tbody>
							  </table>";
					} else {
						$_SESSION['msg']  = "No contacts found.";
   						$_SESSION['type'] = "error";
					}
				}
			?>

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