<?php require __DIR__ . '/../../layouts/header.php'; $lang=current_lang(); ?>
<?php $t=fn($ar,$en)=>($lang==='ar'?$ar:$en); ?>
<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-diagram-project"></i> <?= e($t('المسارات','Tracks')) ?></h4>
    <form method="get">
      <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']===(int)($confId??0)?'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('إضافة مسار','Add Track')) ?></div>
    <div class="card-body">
      <form method="post" action="<?= e(base_path('admin/tracks/save')) ?>" class="row g-2">
        <input type="hidden" name="id" value="0">
        <input type="hidden" name="conference_id" value="<?= e((string)($confId??0)) ?>">

        <div class="col-md-6">
          <label class="form-label"><?= e($t('اسم المسار AR','Name AR')) ?></label>
          <input class="form-control" name="name_ar" required>
        </div>
        <div class="col-md-6">
          <label class="form-label"><?= e($t('اسم المسار EN','Name EN')) ?></label>
          <input class="form-control" name="name_en" required>
        </div>

        <div class="col-md-6">
          <label class="form-label"><?= e($t('وصف AR','Description AR')) ?></label>
          <textarea class="form-control" name="description_ar" rows="2"></textarea>
        </div>
        <div class="col-md-6">
          <label class="form-label"><?= e($t('وصف EN','Description EN')) ?></label>
          <textarea class="form-control" name="description_en" rows="2"></textarea>
        </div>

        <div class="col-md-2">
          <label class="form-label"><?= e($t('حد أدنى محكمين','Min reviewers')) ?></label>
          <input class="form-control" type="number" name="min_reviewers" value="2" min="1">
        </div>
        <div class="col-md-2">
          <label class="form-label"><?= e($t('حد أعلى محكمين','Max reviewers')) ?></label>
          <input class="form-control" type="number" name="max_reviewers" value="3" min="1">
        </div>
        <div class="col-md-2">
          <label class="form-label"><?= e($t('نشط','Active')) ?></label>
          <select class="form-select" name="is_active">
            <option value="1">ON</option>
            <option value="0">OFF</option>
          </select>
        </div>

        <div class="col-md-6 d-grid align-items-end">
          <button class="btn btn-success"><i class="fa-solid fa-floppy-disk"></i> <?= e($t('حفظ','Save')) ?></button>
        </div>
      </form>
    </div>
  </div>

  <div class="card border-0 shadow-sm">
    <div class="table-responsive">
      <table class="table table-hover mb-0 align-middle">
        <thead class="table-light">
          <tr>
            <th>#</th>
            <th><?= e($t('الاسم','Name')) ?></th>
            <th><?= e($t('Min/Max','Min/Max')) ?></th>
            <th><?= e($t('نشط','Active')) ?></th>
            <th><?= e($t('إجراء','Action')) ?></th>
          </tr>
        </thead>
        <tbody>
          <?php foreach(($rows??[]) as $r): ?>
            <tr>
              <td class="text-muted"><?= e((string)$r['id']) ?></td>
              <td>
                <div class="fw-semibold"><?= e((string)($lang==='ar'?($r['name_ar']??''):($r['name_en']??''))) ?></div>
                <div class="text-muted small"><?= e((string)($lang==='ar'?($r['description_ar']??''):($r['description_en']??''))) ?></div>
              </td>
              <td><?= e((string)$r['min_reviewers']) ?> / <?= e((string)$r['max_reviewers']) ?></td>
              <td><?= ((int)$r['is_active']===1)?'<span class="badge bg-success">ON</span>':'<span class="badge bg-danger">OFF</span>' ?></td>
              <td>
                <a class="btn btn-sm btn-outline-danger" onclick="return confirm('<?= e($t('حذف؟','Delete?')) ?>')" href="<?= e(base_path('admin/tracks/delete?id='.(int)$r['id'])) ?>">
                  <i class="fa-solid fa-trash"></i> <?= e($t('حذف','Delete')) ?>
                </a>
              </td>
            </tr>
          <?php endforeach; ?>
          <?php if(empty($rows)): ?><tr><td colspan="5" class="text-center text-muted py-4">—</td></tr><?php endif; ?>
        </tbody>
      </table>
    </div>
  </div>

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