<?php require __DIR__ . '/../../layouts/header.php'; $lang=current_lang(); ?>
<?php
$t=fn($ar,$en)=>($lang==='ar'?$ar:$en);
$rows=$rows??[]; $confs=$confs??[]; $confId=(int)($confId??0);
$subs=$subs??[]; $reviewers=$reviewers??[];
?>
<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-people-arrows"></i> <?= e($t('توزيع المحكمين','Review Assignments')) ?></h4>

    <form method="get" class="d-flex gap-2">
      <select class="form-select" name="conference_id" onchange="this.form.submit()">
        <?php foreach($confs as $c): ?>
          <option value="<?= e((string)$c['id']) ?>" <?= ((int)$c['id']===$confId?'selected':'') ?>>
            <?= e($lang==='ar' ? (string)($c['title_ar']??'') : (string)($c['title_en']??'')) ?>
          </option>
        <?php endforeach; ?>
      </select>
    </form>
  </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('تعيين محكم','Assign Reviewer')) ?></div>
    <div class="card-body">
      <form method="post" action="<?= e(base_path('admin/review-assignments/assign')) ?>" class="row g-2">
        <input type="hidden" name="conference_id" value="<?= e((string)$confId) ?>">
        <div class="col-md-5">
          <select class="form-select" name="submission_id" required>
            <?php foreach($subs as $s): ?>
              <option value="<?= e((string)$s['id']) ?>">#<?= e((string)$s['id']) ?> — <?= e((string)($s['title_ar']??$s['title_en']??'—')) ?></option>
            <?php endforeach; ?>
          </select>
        </div>
        <div class="col-md-5">
          <select class="form-select" name="reviewer_id" required>
            <?php foreach($reviewers as $r): ?>
              <option value="<?= e((string)$r['id']) ?>"><?= e(trim((string)($r['first_name'].' '.$r['last_name']))) ?> (<?= e((string)$r['email']) ?>)</option>
            <?php endforeach; ?>
          </select>
        </div>
        <div class="col-md-1">
          <input class="form-control" name="round_no" value="1" type="number" min="1">
        </div>
        <div class="col-md-1 d-grid">
          <button class="btn btn-success"><i class="fa-solid fa-check"></i> <?= e($t('تعيين','Assign')) ?></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><?=e($t('بحث','Submission'))?></th><th><?=e($t('محكم','Reviewer'))?></th><th><?=e($t('حالة','Status'))?></th><th><?=e($t('جولة','Round'))?></th><th><?=e($t('إجراء','Action'))?></th></tr>
        </thead>
        <tbody>
          <?php foreach($rows as $a): ?>
            <tr>
              <td class="text-muted"><?= e((string)$a['id']) ?></td>
              <td>#<?= e((string)$a['submission_id']) ?></td>
              <td><?= e((string)$a['email']) ?></td>
              <td><span class="badge bg-secondary"><?= e((string)$a['status']) ?></span></td>
              <td><?= e((string)($a['round_no']??1)) ?></td>
              <td>
                <a class="btn btn-sm btn-outline-danger" href="<?= e(base_path('admin/review-assignments/unassign?id='.(int)$a['id'].'&conference_id='.$confId)) ?>">
                  <i class="fa-solid fa-trash"></i> <?= e($t('إلغاء','Remove')) ?>
                </a>
              </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>
<?php require __DIR__ . '/../../layouts/footer.php'; ?>
