<?= $this->extend('layouts/admin') ?>
<?= $this->section('content') ?>

<h1 class="section-title mb-4">Log Aktivitas</h1>

<form method="get" action="<?= base_url('admin/logs') ?>" class="card-stt p-3 mb-4">
  <div class="row g-2 align-items-end">
    <div class="col-md-3">
      <select name="type" class="form-select form-select-sm">
        <option value="">Semua Aksi</option>
        <?php foreach (['login','logout','view','download','submit','approve','reject','revision','login_failed'] as $t): ?>
          <option value="<?= $t ?>" <?= ($filters['type'] ?? '') === $t ? 'selected' : '' ?>><?= ucfirst($t) ?></option>
        <?php endforeach; ?>
      </select>
    </div>
    <div class="col-md-3">
      <input type="date" name="date_from" class="form-control form-control-sm"
             value="<?= esc($filters['date_from'] ?? '') ?>" placeholder="Dari tanggal">
    </div>
    <div class="col-md-3">
      <input type="date" name="date_to" class="form-control form-control-sm"
             value="<?= esc($filters['date_to'] ?? '') ?>" placeholder="Sampai tanggal">
    </div>
    <div class="col-auto d-flex gap-2">
      <button type="submit" class="btn btn-sm btn-primary">Filter</button>
      <a href="<?= base_url('admin/logs') ?>" class="btn btn-sm btn-outline-secondary">Reset</a>
    </div>
  </div>
</form>

<div class="card-stt overflow-hidden">
  <table class="table table-stt table-sm mb-0">
    <thead>
      <tr><th>Waktu</th><th>Pengguna</th><th>Peran</th><th>Aksi</th><th>Dokumen</th><th>IP</th></tr>
    </thead>
    <tbody>
      <?php if (empty($logs)): ?>
        <tr><td colspan="6" class="text-center text-muted py-4">Tidak ada log.</td></tr>
      <?php else: ?>
        <?php foreach ($logs as $log): ?>
          <tr>
            <td class="small text-muted text-nowrap">
              <?= date('d M Y H:i', strtotime($log['created_at'])) ?>
            </td>
            <td class="small"><?= esc($log['user_name'] ?? 'Tamu') ?></td>
            <td class="small text-muted"><?= esc($log['role'] ?? '—') ?></td>
            <td>
              <?php
                $badgeClass = match($log['activity_type']) {
                  'login'        => 'bg-success',
                  'logout'       => 'bg-secondary',
                  'download'     => 'bg-primary',
                  'approve'      => 'bg-success',
                  'reject'       => 'bg-danger',
                  'login_failed' => 'bg-danger',
                  default        => 'bg-secondary',
                };
              ?>
              <span class="badge <?= $badgeClass ?>"><?= esc($log['activity_type']) ?></span>
            </td>
            <td class="small text-muted">
              <?php if (!empty($log['document_title'])): ?>
                <a href="<?= base_url('admin/documents/' . $log['target_id']) ?>" class="text-decoration-none">
                  <?= esc(character_limiter($log['document_title'], 50)) ?>
                </a>
              <?php else: ?>
                —
              <?php endif; ?>
            </td>
            <td class="small text-muted font-monospace"><?= esc($log['ip_address']) ?></td>
          </tr>
        <?php endforeach; ?>
      <?php endif; ?>
    </tbody>
  </table>
</div>

<?= $this->endSection() ?>
