<?php
// pages/admin/audit_logs
?>
<div class="d-flex align-items-center justify-content-between mb-3">
  <h4 class="mb-0">سجلات النظام</h4>
</div>

<?php if (empty($ready)): ?>
  <div class="alert alert-warning">
    لا توجد جداول السجلات بعد. شغّل ملف التحديث:
    <b>sql/migrate_v1_19_auth_admin.sql</b>
  </div>
<?php else: ?>

<div class="card mb-3">
  <div class="card-body">
    <form class="row g-2 align-items-end" method="get" action="<?= e(url('/admin/audit-logs')) ?>">
      <div class="col-md-3">
        <label class="form-label">المستخدم</label>
        <select name="user_id" class="form-select">
          <option value="">الكل</option>
          <?php foreach ($users as $u): ?>
            <?php $name = ($u['full_name'] ?: $u['username']); ?>
            <option value="<?= (int)$u['id'] ?>" <?= ((int)$filters['user_id']===(int)$u['id'])?'selected':'' ?>>
              <?= e($name) ?>
            </option>
          <?php endforeach; ?>
        </select>
      </div>

      <div class="col-md-3">
        <label class="form-label">العملية</label>
        <select name="action_code" class="form-select">
          <option value="">الكل</option>
          <?php foreach ($actions as $a): ?>
            <option value="<?= e(label_def((string)$a['action'], 'audit_action')) ?>" <?= ($filters['action_code']===$a['action'])?'selected':'' ?>>
              <?= e(label_def((string)$a['action'], 'audit_action')) ?>
            </option>
          <?php endforeach; ?>
        </select>
      </div>

      <div class="col-md-2">
        <label class="form-label">من</label>
        <input type="date" name="from" class="form-control" value="<?= e($filters['from'] ?? '') ?>">
      </div>

      <div class="col-md-2">
        <label class="form-label">إلى</label>
        <input type="date" name="to" class="form-control" value="<?= e($filters['to'] ?? '') ?>">
      </div>

      <div class="col-md-2 d-grid">
        <button class="btn btn-primary">بحث</button>
      </div>
    </form>
  </div>
</div>

<div class="card">
  <div class="card-body p-0">
    <div class="table-responsive">
      <table class="table table-striped mb-0 align-middle">
        <thead class="table-light">
          <tr>
            <th>التاريخ</th>
            <th>المستخدم</th>
            <th>العملية</th>
            <th>الكيان</th>
            <th>مرجع</th>
            <th>تفاصيل</th>
          </tr>
        </thead>
        <tbody>
          <?php foreach ($logs as $l): ?>
            <tr>
              <td class="text-muted small"><?= e($l['created_at']) ?></td>
              <td><?= e(($l['full_name'] ?: $l['username']) ?: '—') ?></td>
              <td class="fw-semibold"><span><?= e(label_def((string)$l['action'], 'audit_action')) ?></span><div class="text-muted small"><code><?= e((string)$l['action']) ?></code></div></td>
              <td><?= e(($l['entity'] ?? '') !== '' ? label_def((string)$l['entity'], 'audit_entity') : '-') ?></td>
              <td><?= e($l['entity_id'] ?: '-') ?></td>
              <td class="text-muted small" style="max-width:420px; white-space:nowrap; overflow:hidden; text-overflow:ellipsis;">
                <?= e(label_meta($l['meta_json'] ?? '')) ?>
              </td>
            </tr>
          <?php endforeach; ?>
          <?php if (empty($logs)): ?>
            <tr><td colspan="6" class="text-center text-muted p-4">لا يوجد سجلات</td></tr>
          <?php endif; ?>
        </tbody>
      </table>
    </div>
  </div>

  <?php
    $pages = (int)ceil($total / $per_page);
    if ($pages < 1) $pages = 1;
  ?>
  <div class="card-footer d-flex justify-content-between align-items-center">
    <div class="text-muted small">إجمالي: <?= (int)$total ?></div>
    <div class="btn-group">
      <?php
        $qs = $_GET;
        $qs['page'] = max(1, $page-1);
      ?>
      <a class="btn btn-sm btn-outline-secondary <?= $page<=1?'disabled':'' ?>"
         href="<?= e(url('/admin/audit-logs?'.http_build_query($qs))) ?>">السابق</a>
      <span class="btn btn-sm btn-outline-secondary disabled"><?= (int)$page ?> / <?= (int)$pages ?></span>
      <?php
        $qs = $_GET;
        $qs['page'] = min($pages, $page+1);
      ?>
      <a class="btn btn-sm btn-outline-secondary <?= $page>=$pages?'disabled':'' ?>"
         href="<?= e(url('/admin/audit-logs?'.http_build_query($qs))) ?>">التالي</a>
    </div>
  </div>
</div>

<?php endif; ?>
