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

protegerPainel();

// Quantidade de agentes cadastrados
$totalAgentes = (int) $pdo->query(
    "SELECT COUNT(*) FROM agents"
)->fetchColumn();

// Quantidade de agentes ativos
$totalAtivos = (int) $pdo->query(
    "SELECT COUNT(*) FROM agents WHERE situacao = 'ATIVO'"
)->fetchColumn();

// Últimos agentes cadastrados
$stmt = $pdo->query(
    "SELECT id, codigo, nome, cargo, situacao
     FROM agents
     ORDER BY id DESC
     LIMIT 10"
);

$agentes = $stmt->fetchAll();
?>
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Painel administrativo - INFOGUARDA</title>

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

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

header {
    background: #102d50;
    color: white;
    padding: 22px;
}

header h1 {
    margin: 0;
}

header p {
    margin-bottom: 0;
}

.container {
    max-width: 1100px;
    margin: 30px auto;
    padding: 15px;
}

.cards {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 15px;
    margin-bottom: 25px;
}

.card {
    background: white;
    padding: 22px;
    border-radius: 10px;
    box-shadow: 0 2px 10px #0001;
}

.numero {
    font-size: 32px;
    font-weight: bold;
    color: #102d50;
}

.acoes {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    margin-bottom: 25px;
}

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

.botao.verde {
    background: #147a38;
}

.botao.vermelho {
    background: #a52828;
}

.tabela {
    background: white;
    padding: 20px;
    border-radius: 10px;
    overflow-x: auto;
}

table {
    width: 100%;
    border-collapse: collapse;
    min-width: 600px;
}

th, td {
    padding: 12px;
    text-align: left;
    border-bottom: 1px solid #eee;
}

th {
    background: #f5f6f8;
    color: #102d50;
}

.status {
    font-weight: bold;
    color: #147a38;
}

.status.inativo {
    color: #b3261e;
}

@media(max-width: 600px) {
    header {
        padding: 18px;
    }
}
</style>
</head>

<body>

<header>
    <h1>INFOGUARDA</h1>
    <p>Painel Administrativo — Brigada Metropolitana de Segurança Urbana</p>
</header>

<div class="container">

    <h2>Bem-vindo, <?= limpar($_SESSION['admin_usuario']) ?>!</h2>

    <div class="cards">

        <div class="card">
            <div>Total de agentes</div>
            <div class="numero"><?= $totalAgentes ?></div>
        </div>

        <div class="card">
            <div>Agentes ativos</div>
            <div class="numero"><?= $totalAtivos ?></div>
        </div>

    </div>

    <div class="acoes">
        <a class="botao verde" href="agente.php">
            + CADASTRAR AGENTE
        </a>

        <a class="botao" href="index.php" target="_blank">
            CONSULTA PÚBLICA
        </a>

        <a class="botao vermelho" href="logout.php">
            SAIR DO SISTEMA
        </a>
    </div>

    <div class="tabela">

        <h3>Últimos agentes cadastrados</h3>

        <?php if (count($agentes) > 0): ?>

        <table>
            <thead>
                <tr>
                    <th>Código funcional</th>
                    <th>Nome</th>
                    <th>Cargo</th>
                    <th>Situação</th>
                </tr>
            </thead>

            <tbody>
                <?php foreach ($agentes as $agente): ?>
                <tr>
                    <td><?= limpar($agente['codigo']) ?></td>
                    <td><?= limpar($agente['nome']) ?></td>
                    <td><?= limpar($agente['cargo']) ?></td>
                    <td class="<?= strtoupper($agente['situacao']) === 'ATIVO' ? 'status' : 'status inativo' ?>">
                        <?= limpar($agente['situacao']) ?>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>

        <?php else: ?>

        <p>Nenhum agente cadastrado até o momento.</p>

        <?php endif; ?>

    </div>

</div>

</body>
</html>
