<?php require __DIR__ . '/../../layouts/header.php'; $lang=current_lang(); ?>
<?php
$t=fn($ar,$en)=>($lang==='ar'?$ar:$en);
$rows=$rows??[];
$showAll = (bool)($showAll??false);
$fmt = function($v){
  if (is_array($v)) return json_encode($v, JSON_UNESCAPED_UNICODE);
  if ($v === null) return '';
  return (string)$v;
};
?>
<div class="container py-3">
  <div class="d-flex justify-content-between align-items-center mb-3">
    <h4 class="mb-0"><i class="fa-solid fa-clipboard-list"></i> <?= e($t('سجل التدقيق','Audit Log')) ?></h4>
    <?php if(function_exists('is_superadmin') && is_superadmin()): ?>
      <a class="btn btn-sm btn-outline-primary" href="<?= e(base_path('admin/audit?all=1')) ?>">
        <i class="fa-solid fa-globe"></i> <?= e($t('عرض الكل','Show All')) ?>
      </a>
    <?php endif; ?>
  </div>

  <?php if($showAll): ?>
    <div class="alert alert-warning border-0 shadow-sm">
      <i class="fa-solid fa-triangle-exclamation"></i> <?= e($t('أنت تعرض سجلات كل المنظمات.','You are viewing audit logs for all organizations.')) ?>
    </div>
  <?php endif; ?>

  <div class="card border-0 shadow-sm">
    <div class="table-responsive">
      <table class="table table-hover mb-0">
        <thead class="table-light">
          <tr>
            <th>#</th>
            <th><?= e($t('العملية','Action')) ?></th>
            <th><?= e($t('الكيان','Entity')) ?></th>
            <th><?= e($t('المستخدم','Actor')) ?></th>
            <th><?= e($t('الوقت','Time')) ?></th>
          </tr>
        </thead>
        <tbody>
          <?php foreach($rows as $r): ?>
            <tr>
              <td class="text-muted"><?= e($fmt($r['id']??'')) ?></td>
              <td><?= e($fmt($r['action']??'')) ?></td>
              <td><?= e($fmt($r['entity_type']??'')) ?> #<?= e($fmt($r['entity_id']??'')) ?></td>
              <td><?= e($fmt($r['actor_email']??'')) ?></td>
              <td class="text-muted"><?= e($fmt($r['created_at']??'')) ?></td>
            </tr>
          <?php endforeach; ?>
          <?php if(empty($rows)): ?><tr><td colspan="5" class="text-center text-muted py-4">—</td></tr><?php endif; ?>
        </tbody>
      </table>
    </div>
  </div>
</div>
<?php require __DIR__ . '/../../layouts/footer.php'; ?>
