<?php
include ('includes/header.php');

/* ============================
   ARQUIVO DE TESTES
============================ */
$teste_file = 'api/teste.json';

/* ============================
   GARANTE QUE O ARQUIVO EXISTA
============================ */
if (!file_exists($teste_file)) {
    file_put_contents($teste_file, json_encode([], JSON_PRETTY_PRINT));
}

$testes = json_decode(file_get_contents($teste_file), true) ?? [];

/* ============================
   APAGAR TESTE ESPECÍFICO
============================ */
if (isset($_GET['del']) && isset($testes[$_GET['del']])) {
    unset($testes[$_GET['del']]);
    file_put_contents($teste_file, json_encode($testes, JSON_PRETTY_PRINT));
    echo "<script>
        alert('Teste removido com sucesso!');
        window.location.href='index.php';
    </script>";
    exit;
}

/* ============================
   LIMPAR TODOS OS TESTES
============================ */
if (isset($_GET['clear_all'])) {
    file_put_contents($teste_file, json_encode([], JSON_PRETTY_PRINT));
    echo "<script>
        alert('Todos os testes foram apagados!');
        window.location.href='index.php';
    </script>";
    exit;
}
?>

<div class="container-fluid pt-4 px-4">
    <div class="row g-4">
        <div class="col-12">
            <div class="bg-secondary rounded p-4">

                <h2 class="mb-4">Macs en Demo</h2>

                <?php if (empty($testes)): ?>
                    <p>Nenhum teste registrado.</p>
                <?php else: ?>
                    <div class="table-responsive">
                        <table class="table table-dark table-bordered">
                            <thead>
                                <tr>
                                    <th>MAC Address</th>
                                    <!--<th>Data do Teste</th>-->
                                    <th>Device ID</th>
                                    <th>Acciones</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($testes as $mac => $info): ?>
                                    <tr>
                                        <td><?= htmlspecialchars($mac) ?></td>
                                        <!--<td><?= htmlspecialchars($info['data'] ?? '-') ?></td>-->
                                        <td><?= htmlspecialchars($info['device_id'] ?? '-') ?></td>
                                        <td>
                                            <a href="?del=<?= urlencode($mac) ?>"
                                               class="btn btn-danger btn-sm"
                                               onclick="return confirm('Deseja apagar este teste?')">
                                                Eliminar
                                            </a>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>

                    <a href="?clear_all=1"
                       class="btn btn-warning mt-3"
                       onclick="return confirm('Deseja apagar TODOS os testes?')">
                        Eliminar Todos
                    </a>
                <?php endif; ?>

            </div>
        </div>
    </div>
</div>

<?php include ('includes/footer.php'); ?>
