
<?php
require_once __DIR__ . '/includes/bootstrap.php';

protegerPainel();

$id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);

if (!$id) {
    exit('Identificação inválida.');
}

$stmt = $pdo->prepare("
    SELECT id, codigo, nome, cargo, posto
    FROM agents
    WHERE id = ?
    LIMIT 1
");
$stmt->execute([$id]);

$agente = $stmt->fetch();

if (!$agente) {
    exit('Agente não encontrado.');
}

$urlConsulta = SITE_URL . '/index.php?codigo=' . rawurlencode($agente['codigo']);

$nome = htmlspecialchars($agente['nome'], ENT_QUOTES, 'UTF-8');
$codigo = htmlspecialchars($agente['codigo'], ENT_QUOTES, 'UTF-8');
$cargo = htmlspecialchars($agente['cargo'] ?? '', ENT_QUOTES, 'UTF-8');
$posto = htmlspecialchars($agente['posto'] ?? '', ENT_QUOTES, 'UTF-8');
?>
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>QR Code - <?= $nome ?></title>

<style>
* { box-sizing: border-box; }

body {
    margin: 0;
    padding: 20px;
    font-family: Arial, sans-serif;
    background: #eef1f5;
    color: #263445;
}

.cartao {
    max-width: 480px;
    margin: 30px auto;
    background: white;
    padding: 30px;
    border-radius: 12px;
    text-align: center;
    box-shadow: 0 2px 12px #00000015;
}

h2 { color: #102d50; }

.codigo {
    font-weight: bold;
    color: #102d50;
}

#qrcode {
    margin: 25px auto;
    display: flex;
    justify-content: center;
    min-height: 220px;
}

#qrcode img, #qrcode canvas {
    max-width: 100%;
    height: auto;
}

button, .botao {
    display: inline-block;
    padding: 12px 18px;
    margin: 6px;
    border: 0;
    border-radius: 6px;
    background: #102d50;
    color: white;
    text-decoration: none;
    font-weight: bold;
    cursor: pointer;
}

button:hover, .botao:hover {
    background: #1c4775;
}

@media print {
    body { background: white; }
    .cartao { box-shadow: none; }
    .acoes { display: none; }
}
</style>
</head>

<body>

<div class="cartao">

    <h2>INFOGUARDA</h2>
    <p>BRIGADA METROPOLITANA DE SEGURANÇA URBANA</p>

    <hr>

    <h3><?= $nome ?></h3>

    <p><strong>Código funcional:</strong></p>
    <p class="codigo"><?= $codigo ?></p>

    <p><?= $cargo ?></p>
    <p><?= $posto ?></p>

    <div id="qrcode"></div>

    <p>APONTE A CÂMERA DO CELULAR PARA CONSULTAR A IDENTIFICAÇÃO FUNCIONAL.</p>

    <div class="acoes">
        <button onclick="baixarQR()">BAIXAR QR CODE</button>
        <button onclick="window.print()">IMPRIMIR</button>
        <br>
        <a class="botao" href="agente.php">VOLTAR</a>
    </div>

</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script>

<script>
const endereco = <?= json_encode($urlConsulta) ?>;

new QRCode(document.getElementById("qrcode"), {
    text: endereco,
    width: 220,
    height: 220,
    colorDark: "#102d50",
    colorLight: "#ffffff",
    correctLevel: QRCode.CorrectLevel.H
});

function baixarQR() {
    const canvas = document.querySelector("#qrcode canvas");

    if (!canvas) {
        alert("Aguarde a geração do QR Code.");
        return;
    }

    const link = document.createElement("a");
    link.href = canvas.toDataURL("image/png");
    link.download = "qrcode-<?= $codigo ?>.png";
    link.click();
}
</script>

</body>
</html>
