<?php
session_start();
require_once 'inc/connect.php';

if (!isset($_SESSION['admin_id'])) {
    header("Location: admin_login.php");
    exit;
}

// Fetch all unused tokens
$query = "
SELECT 
    tm.token_id,
    tm.examination_id, s.school_id,
    CONCAT(s.surname, ' ', s.middlename, ' ', s.lastname) AS full_name
FROM token_management tm
JOIN student_table s ON s.serial = tm.student_id
WHERE tm.status = 'unused'
ORDER BY s.surname, s.middlename, s.lastname
";

$result = $mysqli->query($query);

if (!$result) {
    die("SQL Error: " . $mysqli->error);
}

$tokens = $result->fetch_all(MYSQLI_ASSOC);
?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Print Unused Tokens</title>
    <link rel="stylesheet" href="/plugins/bootstrap/css/bootstrap.min.css">
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        .token-block {
            border: 2px dashed #333;
            padding: 12px;
            margin-bottom: 12px;
            font-size: 16px;
            font-weight: bold;
        }
        @media print {
            .noprint {
                display: none;
            }
            .token-block {
                page-break-inside: avoid;
				float:left;
				width:45%;
            }
        }
    </style>
</head>
<body>

<div class="noprint mb-4">
    <button onclick="window.print()" class="btn btn-primary">🖨️ Print Tokens</button>
</div>

<?php if (count($tokens) === 0): ?>
    <div class="alert alert-info">No unused tokens available.</div>
<?php else: ?>
    <?php foreach ($tokens as $row): ?>
        <div class="token-block">
            Name: <?= htmlspecialchars($row['full_name']) ?><br>
            School ID: <?= htmlspecialchars($row['school_id']) ?>  <br>
            Token: <?= htmlspecialchars($row['token_id']) ?><br>
            Exam: <?= htmlspecialchars($row['examination_id']) ?>
        </div>
    <?php endforeach; ?>
<?php endif; ?>

</body>
</html>
