<div class="kpi-grid mb-3">
  <div class="kpi kpi-primary"><span class="kpi-label">Total Entri Log</span><span class="kpi-value"><?= nf($stat['total']) ?></span></div>
  <div class="kpi kpi-info"><span class="kpi-label">Aktivitas Hari Ini</span><span class="kpi-value"><?= nf($stat['hari_ini']) ?></span></div>
  <div class="kpi kpi-secondary"><span class="kpi-label">Pengguna Tercatat</span><span class="kpi-value"><?= nf($stat['pengguna']) ?></span></div>
</div>
<form class="filter-bar" method="get" action="<?= url('/log-aktivitas') ?>">
  <div class="row g-2 align-items-end">
    <div class="col-12 col-md-3"><label class="form-label" for="al_cari">Pencarian</label>
      <input type="search" class="form-control form-control-sm" id="al_cari" name="cari" value="<?= attr($filters['cari'] ?? '') ?>" placeholder="Keterangan, pengguna, IP…"></div>
    <div class="col-6 col-md-2"><label class="form-label" for="al_mod">Modul</label>
      <select class="form-select form-select-sm" id="al_mod" name="module"><option value="">Semua</option>
        <?php foreach ($modules as $m): ?><option value="<?= attr($m) ?>" <?= (($filters['module'] ?? '')===$m)?'selected':'' ?>><?= e($m) ?></option><?php endforeach; ?></select></div>
    <div class="col-6 col-md-2"><label class="form-label" for="al_act">Aksi</label>
      <select class="form-select form-select-sm" id="al_act" name="action"><option value="">Semua</option>
        <?php foreach ($actions as $a): ?><option value="<?= attr($a) ?>" <?= (($filters['action'] ?? '')===$a)?'selected':'' ?>><?= e(\App\Core\Audit::actionLabel($a)) ?></option><?php endforeach; ?></select></div>
    <div class="col-6 col-md-2"><label class="form-label" for="al_user">Pengguna</label>
      <select class="form-select form-select-sm" id="al_user" name="user_id"><option value="">Semua</option>
        <?php foreach ($users as $u): ?><option value="<?= (int)$u['id'] ?>" <?= (($filters['user_id'] ?? '')==$u['id'])?'selected':'' ?>><?= e($u['name']) ?></option><?php endforeach; ?></select></div>
    <div class="col-6 col-md-1"><label class="form-label" for="al_d1">Dari</label>
      <input type="date" class="form-control form-control-sm" id="al_d1" name="dari" value="<?= attr($filters['dari'] ?? '') ?>"></div>
    <div class="col-6 col-md-1"><label class="form-label" for="al_d2">Sampai</label>
      <input type="date" class="form-control form-control-sm" id="al_d2" name="sampai" value="<?= attr($filters['sampai'] ?? '') ?>"></div>
    <div class="col-12 col-md-auto d-flex gap-2 ms-md-auto">
      <button class="btn btn-primary btn-sm" type="submit"><i class="bi bi-funnel me-1"></i>Terapkan</button>
      <?php if (can('audit','export')): ?><a class="btn btn-outline-primary btn-sm" href="<?= url('/log-aktivitas/ekspor', array_merge($filters,['format'=>'xlsx'])) ?>"><i class="bi bi-download"></i></a><?php endif; ?>
    </div>
  </div>
</form>
<div class="card">
  <div class="card-header"><i class="bi bi-clock-history me-1"></i>Log Aktivitas <span class="text-body-secondary fw-normal">(<?= nf($p['total']) ?>)</span></div>
  <div class="table-wrap"><table class="table table-hover table-sticky mb-0">
    <thead><tr><th class="ctr">Waktu</th><th>Pengguna</th><th>Modul</th><th>Aksi</th><th>Entitas</th><th>Keterangan</th><th>IP</th><th class="ctr">Detail</th></tr></thead>
    <tbody>
    <?php if (!$p['data']): ?><tr><td colspan="8"><div class="empty-state"><i class="bi bi-clock-history"></i>Tidak ada log aktivitas.</div></td></tr><?php endif; ?>
    <?php foreach ($p['data'] as $a): ?>
      <tr>
        <td class="ctr small nowrap"><?= tglWaktu($a['created_at']) ?></td>
        <td class="small"><?= e($a['user_name'] ?: 'Sistem') ?></td>
        <td class="small"><?= e($a['module']) ?></td>
        <td><?= badge(\App\Core\Audit::actionLabel($a['action']),
              in_array($a['action'],['delete','reject','reversal','cancel'],true)?'danger':
              (in_array($a['action'],['create','approve'],true)?'success':'secondary')) ?></td>
        <td class="small"><?= e($a['entity_type'] ?: '-') ?><?= $a['entity_id'] ? ' #'.(int)$a['entity_id'] : '' ?></td>
        <td class="small"><?= e(truncate($a['description'],66)) ?></td>
        <td class="small text-body-secondary"><?= e($a['ip_address']) ?></td>
        <td class="ctr"><?php if ($a['old_values'] || $a['new_values']): ?>
          <a class="btn btn-sm btn-light py-0 px-1" href="<?= url('/log-aktivitas/'.(int)$a['id']) ?>"><i class="bi bi-eye"></i></a><?php endif; ?></td>
      </tr>
    <?php endforeach; ?>
    </tbody></table></div>
  <?= \App\Core\View::partial('partials/pagination', ['p' => $p]) ?>
</div>
