<?php
$select = "Select Term";

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['view_used_pin_btn'])) {
    $used_pin_term = inject_checker($connection, ucwords($_POST['used_pin_term']));

    if ($used_pin_term == $select) {
        $_SESSION['msg'] = "Please select the term of used pin you want to view!";
        $_SESSION['type'] = "error";
        header("Location: view_used_pins");
        exit();
    }

    // Save term to session and redirect
    $_SESSION['used_pin_term'] = $used_pin_term;
    header("Location: view_used_pins");
    exit();
}

// Get term from session
$used_pin_term = $_SESSION['used_pin_term'] ?? '';
$run_query = null;

// Fetch result only if session has term
if (!empty($used_pin_term)) {
    $query = "SELECT * FROM `used_pins` WHERE `used_term` = '{$used_pin_term}' ORDER BY `date_used` ASC";
    $run_query = mysqli_query($connection, $query);

    if (mysqli_num_rows($run_query) === 0) {
        $_SESSION['msg'] = "No PIN has been used for {$used_pin_term}!";
        $_SESSION['type'] = "warning";
        // Optional: unset term to avoid persistent empty results
        unset($_SESSION['used_pin_term']);
        header("Location: view_used_pins");
        exit();
    }
}
?>


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

<!-- Card -->
<div class='card'>
    <div class='panel panel-primary'>
        <div class='panel-heading'>
            <h4 class='text-center'>Used Pins</h4>
        </div>
        <div class='panel-body'>
            <!-- Form -->
            <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='used_pin_term' required>
                                <option selected><?= htmlspecialchars($used_pin_term ?: $select) ?></option>
                                <?php
                                foreach (["First Term", "Second Term", "Third Term"] as $term) {
                                    echo "<option>$term</option>";
                                }
                                ?>
                            </select>
                        </div>
                    </div>
                    <div class='col-md-2'>
                        <input type='submit' name='view_used_pin_btn' value='View Used Pin' class='btn btn-success' />
                    </div>
                </div>
            </form>

            <!-- Result Table -->
            <?php if ($run_query && mysqli_num_rows($run_query) > 0): ?>
                <hr>
                <table id="myTable" class="table table-striped table-bordered">
                    <thead>
                        <tr>
                            <th>PIN</th>
                            <th>Name</th>
                            <th>Admission No</th>
                            <th>Usage</th>
                            <th>Class</th>
                            <th>Term</th>
                            <th>Session</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php while ($row = mysqli_fetch_assoc($run_query)): ?>
                            <tr>
                                <td><?= htmlspecialchars($row['used_pins']) ?></td>
                                <td><?= htmlspecialchars($row['user_name']) ?></td>
                                <td><?= htmlspecialchars($row['user_reg_number']) ?></td>
                                <td><?= htmlspecialchars($row['used_count']) ?> Times</td>
                                <td><?= htmlspecialchars($row['user_class']) ?></td>
                                <td><?= htmlspecialchars($row['used_term']) ?></td>
                                <td><?= htmlspecialchars($row['used_session']) ?></td>
                            </tr>
                        <?php endwhile; ?>
                    </tbody>
                </table>
            <?php endif; ?>
        </div>
    </div>
</div>
