<?php
$select = "--select--";
?>

    <style>
        .no-print {
            margin-top: 20px;
        }

        @media print {
            .no-print {
                display: none !important;
            }
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }

        td, th {
            border: 1px solid #000;
            padding: 10px;
        }

        h4 {
            margin-top: 0;
        }
    </style>

<div class='page-title hidden-print'>
    <div>
        <h1><i class='fa fa-laptop'></i> Result Pin Management</h1>
        <p>View Uploaded Result Pins</p>
    </div>
    
</div>

<div class='card'>
    <div class='panel panel-primary'>
        <div class='panel-heading'>
            <h4 class='text-center'>View Uploaded Pins</h4>
        </div>
        <div class='panel-body'>
            <form method='POST' action=''>
                <div class='row'>
                    <div class='col-md-4'>
                        <div class='input-group'>
							<span class='input-group-addon'>TERM</span>
							<select class='form-control' name='view_pin_term' id='view_pin_term'>
								<option selected><?php echo $select ?></option>
								<?php
								$term_array = array("First Term", "Second Term", "Third Term");
								foreach ($term_array as $term) {
									echo "<option>{$term}</option>";
								}
								?>
							</select>
						</div>
						</div>
						
						<div class='col-md-4'>
							<input type='submit' name='view_pin_btn' value='View Uploaded Pin' class='btn btn-success' />
							<button type='button' class='btn btn-primary' onclick='printPins()'>Print</button>
						</div>
					</form>

                    <br />

                    <?php
						if (isset($_POST['view_pin_btn'])) {
							$view_pin_term = $_POST['view_pin_term'];
							$failed = [];

							if ($view_pin_term == $select) {
								$_SESSION['msg'] = "Please Select the Term of the Pin You want to View";
								$_SESSION['type'] = "warning";
							} else {
								$term_table_map = [
									"First Term" => ["table" => "pin1", "column" => "first_term_pin"],
									"Second Term" => ["table" => "pin2", "column" => "second_term_pin"],
									"Third Term" => ["table" => "pin3", "column" => "third_term_pin"]
								];

								if (isset($term_table_map[$view_pin_term])) {
									$table_name = $term_table_map[$view_pin_term]["table"];
									$pin_column = $term_table_map[$view_pin_term]["column"];
									$query = "SELECT * FROM `$table_name` WHERE `term` = '{$view_pin_term}'";
									$run_query = mysqli_query($connection, $query);

									if ($run_query && mysqli_num_rows($run_query) > 0) {
										$all_pins = [];
										while ($result = mysqli_fetch_assoc($run_query)) {
											$all_pins[] = [
												'serial_number' => $result['serial_number'],
												'pin' => $result[$pin_column]
											];
										}

										// Set session success message
										$_SESSION['msg'] = "Pins for {$view_pin_term} successfully loaded.";
										$_SESSION['type'] = "success";

										// Output pin table
										echo "<div class='table-responsive' id='print-area'>";
										echo "<table class='table table-bordered'>";
										echo "<thead><tr><th colspan='2' style='text-align: center;'>{$view_pin_term} Result Pins</th></tr></thead>";
										echo "<tbody>";

										for ($i = 0; $i < count($all_pins); $i += 2) {
											echo "<tr>";

											echo "<td>
												<h4 style='text-align: center;'>{$school_short_name}</h4>
												<strong>TERM:</strong> {$view_pin_term}<br>
												<strong>S/N:</strong> {$all_pins[$i]['serial_number']}<br>
												<strong>PIN:</strong> {$all_pins[$i]['pin']}<br>
												<p style='font-size: 12px; color: red; text-align: center;'>NOTE: You can only Use this Pin 5 Times</p>
												<p style='font-size: 12px; color: blue; text-align: center;'>{$school_website}</p>
											</td>";

											if (isset($all_pins[$i + 1])) {
												echo "<td>
												    <h4 style='text-align: center;'>{$school_short_name}</h4>
												    <strong>TERM:</strong> {$view_pin_term}<br>
												    <strong>S/N:</strong> {$all_pins[$i + 1]['serial_number']}<br>
												    <strong>PIN:</strong> {$all_pins[$i + 1]['pin']}<br>
												    <p style='font-size: 12px; color: red; text-align: center;'>NOTE: You can only Use this Pin 5 Times</p>
												    <p style='font-size: 12px; color: blue; text-align: center;'>{$school_website}</p>
												</td>";
											} else {
												echo "<td></td>";
											}

											echo "</tr>";
										}

										echo "</tbody></table>";
										echo "</div>";

									} else {
										$_SESSION['msg'] = "No Result Pins Found for {$view_pin_term}.";
										$_SESSION['type'] = "error";
									}
								} else {
									$_SESSION['msg'] = "Invalid Term Selected.";
									$_SESSION['type'] = "error";
								}
							}
						}
						?>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
function printPins() {
    const term = document.getElementById("view_pin_term").value;
    if (term === "--select--") {
        alert("Please select a term first.");
        return;
    }
    window.open(`print_uploaded_pins.php?term=${encodeURIComponent(term)}`, "_blank");
}
</script>


