Files
2026-09-16 23:20:08 +07:00

84 lines
4.3 KiB
PHP

<?php
require_once 'includes/header.php';
// Check date filter
$start_date = $_GET['start_date'] ?? date('Y-m-d', strtotime('-30 days'));
$end_date = $_GET['end_date'] ?? date('Y-m-d');
?>
<div class="page-content">
<div class="page-header d-flex justify-content-between align-items-center">
<div>
<h2 class="page-title">Activity Log</h2>
<p class="page-subtitle">ประวัติการใช้งานระบบทั้งหมด</p>
</div>
<div>
<form method="GET" class="d-flex gap-2">
<input type="date" name="start_date" class="form-control" value="<?= htmlspecialchars($start_date) ?>" required>
<span class="align-self-center text-muted">ถึง</span>
<input type="date" name="end_date" class="form-control" value="<?= htmlspecialchars($end_date) ?>" required>
<button type="submit" class="btn btn-primary"><i class="bi bi-filter"></i> กรอง</button>
</form>
</div>
</div>
<div class="card-premium">
<div class="card-body p-4">
<div class="table-responsive">
<table id="logsTable" class="table table-hover align-middle mb-0 w-100">
<thead class="table-light">
<tr>
<th class="ps-3">วันเวลา</th>
<th>ผู้ใช้งาน (CID / ชื่อ)</th>
<th>การกระทำ (Action)</th>
<th>รายละเอียด (Detail)</th>
</tr>
</thead>
<tbody>
<?php
try {
$stmt = $pdo_sys->prepare("SELECT * FROM system_logs WHERE DATE(created_at) BETWEEN :start AND :end ORDER BY id DESC LIMIT 2000");
$stmt->execute(['start' => $start_date, 'end' => $end_date]);
while ($row = $stmt->fetch()) {
$actionColor = 'text-primary bg-primary-subtle';
if(strpos($row['action'], 'Failed') !== false || strpos($row['action'], 'Delete') !== false) {
$actionColor = 'text-danger bg-danger-subtle';
} else if(strpos($row['action'], 'Success') !== false || strpos($row['action'], 'Create') !== false) {
$actionColor = 'text-success bg-success-subtle';
} else if(strpos($row['action'], 'Update') !== false) {
$actionColor = 'text-warning bg-warning-subtle';
}
echo "<tr>";
echo "<td data-order='" . $row['created_at'] . "' class='text-center small text-muted'>" . format_thai_date($row['created_at'], true) . "</td>";
echo "<td class='text-center'><div class='fw-medium text-dark'>" . htmlspecialchars($row['user_name'] ?? 'Unknown') . "</div><div class='small text-muted'>" . htmlspecialchars($row['user_id'] ?? '') . "</div></td>";
echo "<td class='text-center'><span class='badge-soft $actionColor px-2 py-1 rounded'>" . htmlspecialchars($row['action']) . "</span></td>";
echo "<td class='text-muted'>" . htmlspecialchars($row['detail']) . "</td>";
echo "</tr>";
}
} catch (PDOException $e) {
echo "<tr><td colspan='4' class='text-danger'>Error: " . $e->getMessage() . "</td></tr>";
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<?php require_once 'includes/footer.php'; ?>
<script>
$(document).ready(function() {
$('#logsTable').DataTable({
"language": {
"url": "//cdn.datatables.net/plug-ins/1.13.6/i18n/th.json"
},
"order": [[0, "desc"]],
"pageLength": 10,
"dom": '<"row mb-3"<"col-sm-12 col-md-6"l><"col-sm-12 col-md-6"f>>t<"row mt-3"<"col-sm-12 col-md-5"i><"col-sm-12 col-md-7"p>>',
});
});
</script>