<?php require __DIR__ . '/../layouts/header.php'; $lang=current_lang(); ?>
<div class="container">
  <div class="d-flex justify-content-between align-items-center mb-3 flex-wrap gap-2">
    <h4 class="mb-0"><i class="fa-solid fa-file-lines me-2"></i><?= e($lang==='ar'?'إدارة التقديمات':'Submissions Board') ?></h4>
    <a class="btn btn-outline-secondary btn-rounded" href="<?= e(base_path('admin')) ?>"><?= e($lang==='ar'?'لوحة التحكم':'Dashboard') ?></a>
  </div>

  <div class="card border-0 shadow-sm mb-3">
    <div class="card-body">
      <form method="get" class="row g-2">
        <div class="col-md-3">
          <input class="form-control" name="q" value="<?= e((string)($q ?? '')) ?>" placeholder="<?= e($lang==='ar'?'بحث: عنوان/بريد':'Search: title/email') ?>">
        </div>
        <div class="col-md-3">
          <select class="form-select" name="status">
            <option value=""><?= e($lang==='ar'?'كل الحالات':'All statuses') ?></option>
            <?php foreach (['submitted','under_review','revision_minor','revision_major','revision_submitted','accepted','rejected','camera_ready_required','camera_ready_submitted','published','archived'] as $s): ?>
              <option value="<?= e($s) ?>" <?= ((string)($status??'')===$s?'selected':'') ?>><?= e($s) ?></option>
            <?php endforeach; ?>
          </select>
        </div>
        <div class="col-md-2 d-grid">
          <button class="btn btn-primary"><?= e($lang==='ar'?'تصفية':'Filter') ?></button>
        </div>
      </form>
    </div>
  </div>

  <form method="post" action="<?= e(base_path('admin/submissions/bulk-status')) ?>">
    <input type="hidden" name="_csrf" value="<?= e(csrf_token()) ?>">

    <div class="card border-0 shadow-sm">
      <div class="card-body">
        <div class="d-flex gap-2 flex-wrap mb-3">
          <select class="form-select form-select-sm" name="new_status" style="max-width:260px" required>
            <option value=""><?= e($lang==='ar'?'تغيير الحالة إلى...':'Change status to...') ?></option>
            <?php foreach (['under_review','revision_minor','revision_major','accepted','rejected','camera_ready_required','published','archived'] as $s): ?>
              <option value="<?= e($s) ?>"><?= e($s) ?></option>
            <?php endforeach; ?>
          </select>
          <button class="btn btn-sm btn-primary"><i class="fa-solid fa-wand-magic-sparkles me-2"></i><?= e($lang==='ar'?'تطبيق':'Apply') ?></button>
          <span class="text-muted small"><?= e($lang==='ar'?'حدد عدة أبحاث ثم اختر الحالة الجديدة.':'Select multiple papers then choose new status.') ?></span>
        </div>

        <div class="table-responsive">
          <table class="table align-middle">
            <thead>
              <tr>
                <th style="width:40px"><input type="checkbox" onclick="document.querySelectorAll('.sidchk').forEach(c=>c.checked=this.checked)"></th>
                <th>#</th>
                <th><?= e($lang==='ar'?'العنوان':'Title') ?></th>
                <th><?= e($lang==='ar'?'المؤلف':'Author') ?></th>
                <th><?= e($lang==='ar'?'الحالة':'Status') ?></th>
                <th><?= e($lang==='ar'?'تاريخ':'Created') ?></th>
              </tr>
            </thead>
            <tbody>
            <?php foreach (($rows??[]) as $r): ?>
              <tr>
                <td><input class="sidchk" type="checkbox" name="ids[]" value="<?= e((string)$r['id']) ?>"></td>
                <td><?= e((string)$r['id']) ?></td>
                <td class="fw-bold">
                  <a href="<?= e(base_path('author/submission?id='.(int)$r['id'])) ?>" target="_blank">
                    <?= e((string)($lang==='ar' ? ($r['title_ar']??$r['title_en']??'') : ($r['title_en']??$r['title_ar']??''))) ?>
                  </a>
                </td>
                <td><?= e((string)$r['author_email']) ?></td>
                <td><span class="badge bg-secondary"><?= e((string)$r['status']) ?></span></td>
                <td class="text-muted"><?= e((string)$r['created_at']) ?></td>
              </tr>
            <?php endforeach; ?>
            <?php if (empty($rows)): ?><tr><td colspan="6" class="text-center text-muted py-4">—</td></tr><?php endif; ?>
            </tbody>
          </table>
        </div>

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