<?php
declare(strict_types=1);
require __DIR__ . '/../../layouts/header.php';

$page_title = trans('audit_logs');

$title = trans('audit_logs');
$subtitle = current_lang()==='ar' ? 'تتبع عمليات النظام وإجراءات الإدارة.' : 'Track system/admin actions.';
$actions = [
  ['label'=>trans('back'),'href'=>base_path('admin'),'class'=>'btn btn-outline-secondary ec-btn','icon'=>'fa-solid fa-arrow-left'],
];
require __DIR__ . '/../../partials/page_header.php';

$kpis = [
  ['label'=>'Rows','value'=>count($rows ?? []),'icon'=>'fa-solid fa-list','tone'=>'primary'],
];
require __DIR__ . '/../../partials/kpi_chips.php';

$action = base_path('admin/audit');
$placeholder = current_lang()==='ar' ? 'بحث في الإجراء/البريد/الرسالة...' : 'Search action/email/message...';
$q = (string)($q ?? ($_GET['q'] ?? ''));
$filters = [];
$actions = [];
require __DIR__ . '/../../partials/filter_toolbar.php';
?>

<div class="ec-card">
  <div class="ec-card-bd">
    <div class="table-responsive">
      <table class="table ec-table align-middle mb-0">
        <thead>
          <tr>
            <th>#</th>
            <th><?= e(trans('date')) ?></th>
            <th><?= e(trans('user')) ?></th>
            <th>Action</th>
            <th>Entity</th>
            <th><?= e(trans('message')) ?></th>
          </tr>
        </thead>
        <tbody>
          <?php foreach (($rows ?? []) as $r): ?>
            <tr>
              <td><?= e((string)$r['id']) ?></td>
              <td class="small text-muted"><?= e((string)$r['created_at']) ?></td>
              <td>
                <div class="fw-bold"><?= e((string)($r['actor_email'] ?? '')) ?></div>
                <div class="small text-muted"><?= e((string)($r['ip_address'] ?? '')) ?></div>
              </td>
              <td><code><?= e((string)$r['action']) ?></code></td>
              <td class="small text-muted"><?= e((string)($r['entity_type'] ?? '')) ?> #<?= e((string)($r['entity_id'] ?? '')) ?></td>
              <td><?= e((string)($r['message'] ?? '')) ?></td>
            </tr>
          <?php endforeach; ?>

          <?php if (empty($rows ?? [])): ?>
            <tr>
              <td colspan="6">
                <?php
                  $title = trans('no_data');
                  $desc = current_lang()==='ar' ? 'لا توجد سجلات حتى الآن.' : 'No logs yet.';
                  $cta_label = '';
                  $cta_href = '';
                  $icon = 'fa-solid fa-shield-halved';
                  require __DIR__ . '/../../partials/empty_state.php';
                ?>
              </td>
            </tr>
          <?php endif; ?>
        </tbody>
      </table>
    </div>
  </div>
</div>

<?php require __DIR__ . '/../../layouts/footer.php'; ?>
