www/historico.php
2026-06-27 21:44:44 +00:00

157 lines
5.3 KiB
PHP

<?php
require_once __DIR__ . "/api/protecao.php";
try {
$pdo = new PDO("mysql:host=localhost;dbname=cagalhao;charset=utf8mb4", "master", "master", [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]);
// Se pediu para limpar pendentes
if (isset($_POST['clear_pending'])) {
$pdo->exec("DELETE FROM comandos WHERE status = 'pending'");
$_SESSION['flash'] = "🧹 Todos os comandos pendentes foram removidos!";
header("Location: historico.php");
exit;
}
// Filtros
$where = [];
$params = [];
if (!empty($_GET['uid'])) {
$where[] = "c.uid = ?";
$params[] = $_GET['uid'];
}
if (!empty($_GET['status'])) {
$where[] = "c.status = ?";
$params[] = $_GET['status'];
}
$sql = "SELECT c.id, c.uid, c.cmd, c.status, c.queued_at, c.sent_at, c.ack_ts, c.error,
r.payload AS resposta, r.ts AS resposta_at
FROM comandos c
LEFT JOIN respostas r ON r.uid = c.uid
" . ($where ? "WHERE " . implode(" AND ", $where) : "") . "
ORDER BY c.queued_at DESC
LIMIT 50";
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$comandos = $stmt->fetchAll();
$devices = $pdo->query("SELECT DISTINCT uid FROM devices ORDER BY uid ASC")->fetchAll();
} catch (Exception $e) {
die("Erro: " . $e->getMessage());
}
?>
<!DOCTYPE html>
<html lang="pt">
<head>
<meta charset="UTF-8">
<title>Histórico de Comandos</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body { background-color: #121212; color: #eee; }
.card, .table { background-color: #1e1e1e; color: #ddd; }
.table thead { background-color: #333; }
</style>
<script>
// Auto-refresh a cada 5 segundos
setTimeout(() => location.reload(), 5000);
</script>
</head>
<body class="p-4">
<div class="container-fluid">
<h2 class="mb-4 text-info">📜 Histórico de Comandos</h2>
<?php if (!empty($_SESSION['flash'])): ?>
<div class="alert alert-success"><?= $_SESSION['flash']; unset($_SESSION['flash']); ?></div>
<?php endif; ?>
<!-- Botão para limpar pendentes -->
<form method="POST" class="mb-3">
<button type="submit" name="clear_pending" class="btn btn-danger">
🧹 Limpar Pendentes
</button>
</form>
<!-- Filtros -->
<form method="GET" class="row g-3 mb-4">
<div class="col-md-4">
<select name="uid" class="form-select">
<option value="">-- Todos os dispositivos --</option>
<?php foreach ($devices as $d): ?>
<option value="<?= htmlspecialchars($d['uid']) ?>" <?= (($_GET['uid'] ?? '') === $d['uid']) ? 'selected' : '' ?>>
<?= htmlspecialchars($d['uid']) ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-4">
<select name="status" class="form-select">
<option value="">-- Todos os status --</option>
<?php foreach (['pending','sent','done','failed'] as $s): ?>
<option value="<?= $s ?>" <?= (($_GET['status'] ?? '') === $s) ? 'selected' : '' ?>>
<?= ucfirst($s) ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-4">
<button class="btn btn-primary w-100">🔎 Filtrar</button>
</div>
</form>
<!-- Tabela -->
<div class="table-responsive">
<table class="table table-dark table-hover shadow-sm">
<thead>
<tr>
<th>ID</th>
<th>Dispositivo</th>
<th>Comando</th>
<th>Status</th>
<th>Enfileirado</th>
<th>Enviado</th>
<th>ACK</th>
<th>Erro</th>
<th>Resposta</th>
<th>Resp. At</th>
</tr>
</thead>
<tbody>
<?php foreach ($comandos as $c): ?>
<?php
switch ($c['status']) {
case 'pending': $badge = 'warning'; break;
case 'sent': $badge = 'info'; break;
case 'done': $badge = 'success'; break;
case 'failed': $badge = 'danger'; break;
default: $badge = 'secondary'; break;
}
?>
<tr>
<td><?= $c['id'] ?></td>
<td><?= htmlspecialchars($c['uid']) ?></td>
<td><?= htmlspecialchars($c['cmd']) ?></td>
<td><span class="badge bg-<?= $badge ?>"><?= $c['status'] ?></span></td>
<td><?= $c['queued_at'] ?></td>
<td><?= $c['sent_at'] ?></td>
<td><?= $c['ack_ts'] ?></td>
<td><?= htmlspecialchars($c['error'] ?? '') ?></td>
<td><?= htmlspecialchars($c['resposta'] ?? '') ?></td>
<td><?= $c['resposta_at'] ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<div class="mt-4">
<a href="api/menu.php" class="btn btn-secondary">⬅️ Voltar ao Menu</a>
</div>
</div>
</body>
</html>