<?php
include __DIR__ . '/../database/search_log_config.php';

$search = $_GET['search'] ?? '';

$where = "WHERE 1=1";

if($search){
    $searchSafe = $log_conn->real_escape_string($search);
    $where .= " AND (
        u.admin_name LIKE '%$searchSafe%' OR
        a.activity LIKE '%$searchSafe%' OR
        a.ip_address LIKE '%$searchSafe%'
    )";
}

$logs = $log_conn->query("
SELECT a.*, u.admin_name
FROM admin_activity_logs a
LEFT JOIN admin_users u ON a.admin_id = u.id
$where
ORDER BY a.created_at DESC
LIMIT 50
");
?>

<div style="overflow-x:auto;">

<?php if($logs && $logs->num_rows > 0): ?>

<table style="width:100%;border-collapse:collapse;">
<thead>
<tr style="background:#f5f5f5;">
<th style="padding:10px;">Admin</th>
<th style="padding:10px;">Activity</th>
<th style="padding:10px;">IP</th>
<th style="padding:10px;">Date & Time</th>
<th style="padding:10px;">Action</th>
</tr>
</thead>
<tbody>

<?php while($row = $logs->fetch_assoc()): ?>
<tr style="border-bottom:1px solid #eee;">
<td style="padding:10px;"><?= htmlspecialchars($row['admin_name']) ?></td>
<td style="padding:10px;"><?= htmlspecialchars($row['activity']) ?></td>
<td style="padding:10px;"><?= htmlspecialchars($row['ip_address']) ?></td>
<td style="padding:10px;"><?= $row['created_at'] ?></td>
<td style="padding:10px;">
<a href="delete_single_log.php?id=<?= $row['id'] ?>"
onclick="return confirm('Delete this log?');"
style="color:red;text-decoration:none;">
Delete
</a>
</td>
</tr>
<?php endwhile; ?>

</tbody>
</table>

<?php else: ?>
<p style="padding:20px;">No logs found.</p>
<?php endif; ?>

</div>