<?php require __DIR__ . '/../../layouts/header.php'; $lang=current_lang(); ?>
<?php $t=fn($ar,$en)=>($lang==='ar'?$ar:$en); ?>
<?php $rows=$rows??[]; $status=$status??''; $q=$q??''; $kpi=$kpi??['queued'=>0,'running'=>0,'done'=>0,'failed'=>0,'all'=>0]; ?>

<div class="container py-3">
  <div class="d-flex justify-content-between align-items-center flex-wrap gap-2 mb-3">
    <h4 class="mb-0"><i class="fa-solid fa-gears"></i> <?= e($t('مراقبة المهام','Jobs Monitor')) ?></h4>
    <div class="d-flex gap-2 flex-wrap">
      <a class="btn btn-outline-primary" href="<?= e(base_path('admin/jobs/run-worker')) ?>"><i class="fa-solid fa-play"></i> <?= e($t('تشغيل العامل','Run Worker')) ?></a>
      <a class="btn btn-outline-secondary" target="_blank" href="<?= e(base_path('admin/jobs/output')) ?>"><i class="fa-solid fa-terminal"></i> <?= e($t('مخرجات','Output')) ?></a>
    </div>
  </div>

  <div class="row g-3 mb-3">
    <div class="col-md-3"><div class="card border-0 shadow-sm"><div class="card-body"><div class="text-muted">All</div><div class="fs-4 fw-bold"><?= e((string)$kpi['all']) ?></div></div></div></div>
    <div class="col-md-3"><div class="card border-0 shadow-sm"><div class="card-body"><div class="text-muted">Queued</div><div class="fs-4 fw-bold"><?= e((string)$kpi['queued']) ?></div></div></div></div>
    <div class="col-md-3"><div class="card border-0 shadow-sm"><div class="card-body"><div class="text-muted">Done</div><div class="fs-4 fw-bold"><?= e((string)$kpi['done']) ?></div></div></div></div>
    <div class="col-md-3"><div class="card border-0 shadow-sm"><div class="card-body"><div class="text-muted">Failed</div><div class="fs-4 fw-bold"><?= e((string)$kpi['failed']) ?></div></div></div></div>
  </div>

  <div class="card border-0 shadow-sm mb-3">
    <div class="card-body">
      <form class="row g-2" method="get">
        <div class="col-md-6"><input class="form-control" name="q" value="<?= e((string)$q) ?>" placeholder="<?= e($t('بحث...','Search...')) ?>"></div>
        <div class="col-md-3">
          <select class="form-select" name="status">
            <option value=""><?= e($t('كل الحالات','All statuses')) ?></option>
            <?php foreach(['queued','running','done','failed'] as $s): ?>
              <option value="<?= e($s) ?>" <?= $status===$s?'selected':'' ?>><?= e($s) ?></option>
            <?php endforeach; ?>
          </select>
        </div>
        <div class="col-md-3 d-grid"><button class="btn btn-primary"><i class="fa-solid fa-filter"></i> <?= e($t('تصفية','Filter')) ?></button></div>
      </form>
    </div>
  </div>

  <div class="card border-0 shadow-sm mb-3">
    <div class="card-header bg-white fw-bold"><i class="fa-solid fa-plus"></i> <?= e($t('إضافة مهمة','Enqueue Job')) ?></div>
    <div class="card-body">
      <form method="post" action="<?= e(base_path('admin/jobs/enqueue')) ?>" class="row g-2">
        <div class="col-md-4"><input class="form-control" name="job_key" placeholder="job_key" required></div>
        <div class="col-md-5"><input class="form-control" name="payload_json" placeholder='{"any":"json"} (optional)'></div>
        <div class="col-md-3"><input class="form-control" type="datetime-local" name="run_after"></div>
        <div class="col-12 d-grid"><button class="btn btn-success"><i class="fa-solid fa-paper-plane"></i> <?= e($t('إضافة','Enqueue')) ?></button></div>
      </form>
    </div>
  </div>

  <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>job_key</th><th>Status</th><th>Attempts</th><th>Run After</th><th>Started</th><th>Finished</th><th><?= e($t('إجراءات','Actions')) ?></th>
        </tr></thead>
        <tbody>
          <?php foreach($rows as $r): ?>
            <tr>
              <td class="text-muted"><?= e((string)$r['id']) ?></td>
              <td class="fw-bold"><?= e((string)$r['job_key']) ?></td>
              <td><span class="badge bg-secondary"><?= e((string)$r['status']) ?></span></td>
              <td><?= e((string)($r['attempts']??0)) ?></td>
              <td class="text-muted"><?= e((string)($r['run_after']??'')) ?></td>
              <td class="text-muted"><?= e((string)($r['started_at']??'')) ?></td>
              <td class="text-muted"><?= e((string)($r['finished_at']??'')) ?></td>
              <td><a class="btn btn-sm btn-outline-danger" href="<?= e(base_path('admin/jobs/delete?id='.$r['id'])) ?>"><i class="fa-solid fa-trash"></i> <?= e($t('حذف','Delete')) ?></a></td>
            </tr>
          <?php endforeach; ?>
          <?php if(empty($rows)): ?><tr><td colspan="8" class="text-center text-muted py-4">—</td></tr><?php endif; ?>
        </tbody>
      </table>
    </div>
  </div>

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