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

$agente = null;
$erro = '';

$codigo = trim($_GET['codigo'] ?? '');

if ($codigo !== '') {
    try {
        $stmt = $pdo->prepare(
            "SELECT codigo, nome, cargo, posto, situacao,
                    expedicao, validade, foto
             FROM agents
             WHERE codigo = :codigo
             LIMIT 1"
        );

        $stmt->execute(['codigo' => $codigo]);
        $agente = $stmt->fetch();

        if (!$agente) {
            $erro = 'Nenhuma identificação encontrada para este código.';
        }
    } catch (PDOException $e) {
        $erro = 'Não foi possível realizar a consulta.';
    }
}

function dataBR($data) {
    if (empty($data) || $data === '0000-00-00') {
        return 'Não informada';
    }

    return date('d/m/Y', strtotime($data));
}
?>
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>INFOGUARDA - Consulta Funcional</title>

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

body {
    margin: 0;
    font-family: Arial, sans-serif;
    background: #f0f2f5;
    color: #222;
}

header {
    background: #102d50;
    color: white;
    text-align: center;
    padding: 28px 15px;
}

header h1 {
    margin: 0;
    font-size: 30px;
    letter-spacing: 2px;
}

header p {
    margin: 8px 0 0;
    font-size: 13px;
}

.container {
    max-width: 650px;
    margin: 30px auto;
    padding: 20px;
}

.card {
    background: white;
    padding: 25px;
    border-radius: 12px;
    box-shadow: 0 3px 15px rgba(0,0,0,.08);
}

h2 {
    text-align: center;
    color: #102d50;
}

form {
    display: flex;
    gap: 8px;
    margin-top: 20px;
}

input {
    flex: 1;
    min-width: 0;
    padding: 13px;
    border: 1px solid #ccc;
    border-radius: 6px;
    font-size: 15px;
}

button {
    background: #102d50;
    color: white;
    border: 0;
    border-radius: 6px;
    padding: 13px 18px;
    cursor: pointer;
    font-weight: bold;
}

.resultado {
    margin-top: 25px;
    border-top: 1px solid #ddd;
    padding-top: 20px;
}

.foto {
    width: 150px;
    height: 170px;
    object-fit: cover;
    display: block;
    margin: 0 auto 20px;
    border-radius: 8px;
    border: 2px solid #ddd;
}

.campo {
    padding: 10px 0;
    border-bottom: 1px solid #eee;
}

.campo strong {
    display: block;
    color: #555;
    font-size: 12px;
    margin-bottom: 4px;
}

.campo span {
    font-size: 16px;
    font-weight: bold;
}

.status {
    color: #147a38;
}

.status.inativo {
    color: #b3261e;
}

.erro {
    color: #b3261e;
    text-align: center;
    margin-top: 20px;
}

footer {
    text-align: center;
    color: #777;
    font-size: 12px;
    padding: 25px;
}

@media(max-width: 480px) {
    form {
        flex-direction: column;
    }

    button {
        width: 100%;
    }
}
</style>
</head>

<body>

<header>
    <h1>INFOGUARDA</h1>
    <p>BRIGADA METROPOLITANA DE SEGURANÇA URBANA</p>
    <p>SISTEMA DE CONSULTA FUNCIONAL</p>
</header>

<div class="container">
<div class="card">

<h2>CONSULTAR AGENTE</h2>

<p style="text-align:center">
Digite o código funcional para consultar a identificação.
</p>

<form method="GET" action="index.php">
    <input
        type="text"
        name="codigo"
        placeholder="Código funcional"
        value="<?= limpar($codigo) ?>"
        required
    >
    <button type="submit">CONSULTAR</button>
</form>

<?php if ($erro): ?>
    <p class="erro"><?= limpar($erro) ?></p>
<?php endif; ?>

<?php if ($agente): ?>

<div class="resultado">

    <?php if (!empty($agente['foto'])): ?>
        <img
            class="foto"
            src="<?= limpar($agente['foto']) ?>"
            alt="Fotografia do agente"
        >
    <?php endif; ?>

    <h2>IDENTIFICAÇÃO FUNCIONAL</h2>

    <div class="campo">
        <strong>SITUAÇÃO FUNCIONAL</strong>
        <span class="<?= strtoupper($agente['situacao']) === 'ATIVO' ? 'status' : 'status inativo' ?>">
            <?= limpar($agente['situacao']) ?>
        </span>
    </div>

    <div class="campo">
        <strong>NOME COMPLETO</strong>
        <span><?= limpar($agente['nome']) ?></span>
    </div>

    <div class="campo">
        <strong>CARGO</strong>
        <span><?= limpar($agente['cargo']) ?></span>
    </div>

    <div class="campo">
        <strong>POSTO</strong>
        <span><?= limpar($agente['posto']) ?></span>
    </div>

    <div class="campo">
        <strong>CÓDIGO FUNCIONAL</strong>
        <span><?= limpar($agente['codigo']) ?></span>
    </div>

    <div class="campo">
        <strong>EXPEDIÇÃO</strong>
        <span><?= dataBR($agente['expedicao']) ?></span>
    </div>

    <div class="campo">
        <strong>VALIDADE</strong>
        <span><?= dataBR($agente['validade']) ?></span>
    </div>

</div>
<?php endif; ?>

</div>
</div>

<footer>
    INFOGUARDA — Sistema de Consulta Funcional<br>
    Brigada Metropolitana de Segurança Urbana
</footer>

</body>
</html>
