<?php $pageTitle = 'Activity Logs'; include __DIR__ . '/../shared/header.php'; ?>

<div class="page-header">
  <div>
    <div class="page-title"><i class="bi bi-journal-text me-2"></i>Activity Logs</div>
    <div class="page-subtitle">Showing <?= count($logs) ?> of <?= number_format($total) ?> records</div>
  </div>
</div>

<!-- Filters -->
<div class="card mb-3">
  <div class="card-body py-2">
    <form method="GET" class="d-flex gap-2 align-items-center flex-wrap">
      <input type="hidden" name="page" value="logs">
      <select name="module" class="form-select form-select-sm" style="width:180px;">
        <option value="">All Modules</option>
        <?php foreach ($modules as $m): ?>
          <option value="<?= e($m) ?>" <?= ($_GET['module'] ?? '') === $m ? 'selected' : '' ?>><?= e(ucfirst($m)) ?></option>
        <?php endforeach; ?>
      </select>
      <button type="submit" class="btn btn-sm btn-primary"><i class="bi bi-filter me-1"></i>Filter</button>
      <a href="<?= APP_URL ?>/index.php?page=logs" class="btn btn-sm btn-outline-secondary">Clear</a>
    </form>
  </div>
</div>

<div class="card">
  <div class="card-body p-0">
    <div class="table-responsive">
      <table class="table table-hover table-sm mb-0">
        <thead class="table-light">
          <tr><th>Time</th><th>User</th><th>Role</th><th>Action</th><th>Module</th><th>Description</th><th>IP</th></tr>
        </thead>
        <tbody>
        <?php foreach ($logs as $log): ?>
          <tr>
            <td><small class="text-nowrap"><?= date('d M Y H:i', strtotime($log['created_at'])) ?></small></td>
            <td><small><?= e($log['officer_name'] ?? 'System') ?></small></td>
            <td><span class="badge bg-secondary" style="font-size:10px;"><?= e($log['user_role'] ?? '—') ?></span></td>
            <td><code style="font-size:11px;"><?= e($log['action']) ?></code></td>
            <td><span class="badge bg-light text-dark" style="font-size:10px;"><?= e($log['module']) ?></span></td>
            <td><small class="text-muted"><?= e(substr($log['description'] ?? '', 0, 80)) ?></small></td>
            <td><small class="text-muted"><?= e($log['ip_address'] ?? '') ?></small></td>
          </tr>
        <?php endforeach; ?>
        <?php if (empty($logs)): ?>
          <tr><td colspan="7" class="text-center text-muted py-4">No activity logs found.</td></tr>
        <?php endif; ?>
        </tbody>
      </table>
    </div>
  </div>
</div>

<!-- Pagination -->
<?php if ($total > 100): ?>
<div class="d-flex justify-content-between align-items-center mt-3">
  <small class="text-muted">Page <?= floor($offset/100)+1 ?> of <?= ceil($total/100) ?></small>
  <div class="d-flex gap-2">
    <?php if ($offset > 0): ?>
      <a href="?page=logs&module=<?= urlencode($_GET['module'] ?? '') ?>&offset=<?= $offset-100 ?>" class="btn btn-sm btn-outline-primary">
        <i class="bi bi-chevron-left"></i> Previous
      </a>
    <?php endif; ?>
    <?php if ($offset + 100 < $total): ?>
      <a href="?page=logs&module=<?= urlencode($_GET['module'] ?? '') ?>&offset=<?= $offset+100 ?>" class="btn btn-sm btn-outline-primary">
        Next <i class="bi bi-chevron-right"></i>
      </a>
    <?php endif; ?>
  </div>
</div>
<?php endif; ?>

<?php include __DIR__ . '/../shared/footer.php'; ?>
