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

$page_title = trans('cron_monitor');

$title = trans('cron_monitor');
$subtitle = current_lang()==='ar' ? 'سجل تشغيل المهام المجدولة.' : 'Scheduled jobs run history.';
$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-clock-rotate-left','tone'=>'primary'],
  ['label'=>trans('failed'),'value'=>count(array_filter($rows ?? [], fn($r)=>($r['status']??'')==='failed')),'icon'=>'fa-solid fa-triangle-exclamation','tone'=>'danger'],
];
require __DIR__ . '/../../partials/kpi_chips.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>Job</th>
            <th><?= e(trans('status')) ?></th>
            <th><?= e(trans('message')) ?></th>
            <th>Duration</th>
            <th><?= e(trans('date')) ?></th>
          </tr>
        </thead>
        <tbody>
          <?php foreach (($rows ?? []) as $r): ?>
            <?php
              $st = (string)($r['status'] ?? 'success');
              $badge = ($st === 'failed') ? 'bg-danger' : 'bg-success';
            ?>
            <tr>
              <td><?= e((string)$r['id']) ?></td>
              <td><code><?= e((string)$r['job_key']) ?></code></td>
              <td><span class="badge ec-badge <?= e($badge) ?>"><?= e(trans($st)) ?></span></td>
              <td><?= e((string)($r['message'] ?? '')) ?></td>
              <td class="small text-muted"><?= e((string)($r['duration_ms'] ?? '')) ?> ms</td>
              <td class="small text-muted"><?= e((string)($r['ran_at'] ?? '')) ?></td>
            </tr>
          <?php endforeach; ?>

          <?php if (empty($rows ?? [])): ?>
            <tr>
              <td colspan="6">
                <?php
                  $title = trans('no_data');
                  $desc = current_lang()==='ar' ? 'لا توجد عمليات تشغيل مسجلة.' : 'No runs recorded yet.';
                  $cta_label = '';
                  $cta_href = '';
                  $icon = 'fa-solid fa-clock';
                  require __DIR__ . '/../../partials/empty_state.php';
                ?>
              </td>
            </tr>
          <?php endif; ?>
        </tbody>
      </table>
    </div>
  </div>
</div>

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