<?php

$select = "--select--";
$term_array = ["First Term", "Second Term", "Third Term"];

if (isset($_POST['view_unused_btn']) || isset($_POST['print_unused_btn'])) {
    $view_unsed_pin_term = $_POST['view_unsed_pin_term'];
    $failed = [];

    if ($view_unsed_pin_term == $select) {
        $_SESSION['msg'] = "Error! Please Select the Term of the Pin You want to View";
        $_SESSION['type'] = "warning";
    } else {
        $stmt = mysqli_prepare($connection, "SELECT serial_number, unused_pins, term FROM unused_pins WHERE term = ?");
        mysqli_stmt_bind_param($stmt, "s", $view_unsed_pin_term);
        mysqli_stmt_execute($stmt);
        $result = mysqli_stmt_get_result($stmt);

        if (mysqli_num_rows($result) > 0) {
            $all_pins = [];
            while ($row = mysqli_fetch_assoc($result)) {
                $all_pins[] = [
                    'serial_number' => $row['serial_number'],
                    'unused_pin' => $row['unused_pins'],
                    'term' => $row['term']
                ];
            }

            $_SESSION['view_pins'] = $all_pins;
            $_SESSION['view_pin_term'] = $view_unsed_pin_term;

            if (isset($_POST['print_unused_btn'])) {
                echo "<script>window.open('print_unused_pins.php', '_blank');</script>";
            }
        } else {
            $_SESSION['msg'] = "No pins found for {$view_unsed_pin_term}.";
            $_SESSION['type'] = "error";
        }
    }        
}
?>

<style>
@media print {
    body * {
        display: none;
    }
    .table, .table * {
        display: table;
        visibility: visible;
    }
    .table-responsive {
        width: 100%;
        margin: 0 auto;
    }
}
</style>

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

<div class='card'>
    <div class='panel panel-primary'>
        <div class='panel-heading'>
            <h4 class='text-center'>Unused 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_unsed_pin_term'>
                                <option selected><?php echo $select; ?></option>
                                <?php foreach ($term_array as $term): ?>
                                    <option><?php echo $term; ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        </div>
						<div class='col-md-4'>
                        <input type='submit' name='view_unused_btn' value='View' class='btn btn-success' />
                        <input type='submit' name='print_unused_btn' value='Print' class='btn btn-primary' />
                        </div>
                    </form>

                    <br /><br />

                    <?php if (isset($_SESSION['view_pins'])): ?>
                        <div class='table-responsive' id='print-area'>
                            <table class='table table-bordered'>
                                <thead>
                                    <tr>
                                        <th colspan='2' class='text-center'>
                                            Unused Pins for <?php echo $_SESSION['view_pin_term']; ?>
                                        </th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $pins = $_SESSION['view_pins'];
                                    for ($i = 0; $i < count($pins); $i += 2):
                                        echo "<tr>";
                                        for ($j = 0; $j < 2; $j++) {
                                            if (isset($pins[$i + $j])) {
                                                $pin = $pins[$i + $j];
                                                echo "<td>
                                                    <h4 class='text-center'>{$school_short_name}</h4>
                                                    <strong>S/N:</strong> {$pin['serial_number']}<br>
                                                    <strong>PIN:</strong> {$pin['unused_pin']}<br>
                                                    <strong>Term:</strong> {$pin['term']}<br>
                                                </td>";
                                            } else {
                                                echo "<td></td>";
                                            }
                                        }
                                        echo "</tr>";
                                    endfor;
                                    // unset($_SESSION['view_pins'], $_SESSION['view_pin_term']);
                                    ?>
                                </tbody>
                            </table>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>

