<?php require __DIR__ . '/../../layouts/header.php'; $lang=current_lang(); ?>
<?php $t=fn($ar,$en)=>($lang==='ar'?$ar:$en); ?>
<?php
$sub=$sub??[];
$assignments=$assignments??[];
$reviewers=$reviewers??[];
$id=(int)($sub['id']??0);

$title='';
if($lang==='ar' && !empty($titleAr)) $title=(string)($sub[$titleAr]??'');
if($title==='' && !empty($titleEn)) $title=(string)($sub[$titleEn]??'');
if($title==='') $title='—';

$stVal = isset($sub['status']) ? (string)$sub['status'] : '';

$filePath = '';
foreach (['file_path','paper_path','pdf_path','document_path'] as $fc) {
  if (!empty($sub[$fc])) { $filePath = (string)$sub[$fc]; break; }
}
?>
<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-file-lines"></i> <?= e($t('تفاصيل البحث','Submission Details')) ?> #<?= e((string)$id) ?></h4>
    <div class="d-flex gap-2">
      <a class="btn btn-outline-secondary" href="<?= e(base_path('admin/submissions')) ?>"><i class="fa-solid fa-arrow-left"></i> <?= e($t('رجوع','Back')) ?></a>
      <a class="btn btn-outline-secondary" href="<?= e(base_path('whoami.php')) ?>"><i class="fa-solid fa-id-badge"></i> <?= e($t('جلسة','Session')) ?></a>
    </div>
  </div>

  <div class="row g-3">
    <div class="col-lg-7">
      <div class="card border-0 shadow-sm mb-3">
        <div class="card-body">
          <div class="fw-bold mb-2"><?= e($title) ?></div>

          <div class="mb-2">
            <span class="badge bg-secondary"><?= e($t('الحالة','Status')) ?>: <?= e($stVal) ?></span>
          </div>

          <?php if($filePath!==''): ?>
            <div class="mb-3">
              <a class="btn btn-sm btn-outline-primary" href="<?= e($filePath) ?>" target="_blank">
                <i class="fa-solid fa-download"></i> <?= e($t('فتح الملف','Open File')) ?>
              </a>
            </div>
          <?php endif; ?>

          <?php if(!empty($absAr) && !empty($sub[$absAr])): ?>
            <div class="text-muted small mb-2"><?= e($t('الملخص','Abstract')) ?></div>
            <div class="border rounded p-2 mb-3"><?= nl2br(e((string)$sub[$absAr])) ?></div>
          <?php endif; ?>

          <?php if(!empty($kwAr) && !empty($sub[$kwAr])): ?>
            <div class="text-muted small mb-2"><?= e($t('الكلمات المفتاحية','Keywords')) ?></div>
            <div class="mb-3"><?= e((string)$sub[$kwAr]) ?></div>
          <?php endif; ?>

          <div class="d-flex flex-wrap gap-2 mb-3">
            <form method="post" action="<?= e(base_path('admin/submissions/status')) ?>">
              <input type="hidden" name="id" value="<?= e((string)$id) ?>">
              <input type="hidden" name="status" value="submitted">
              <button class="btn btn-sm btn-outline-secondary"><i class="fa-solid fa-inbox"></i> <?= e($t('Submitted','Submitted')) ?></button>
            </form>
            <form method="post" action="<?= e(base_path('admin/submissions/status')) ?>">
              <input type="hidden" name="id" value="<?= e((string)$id) ?>">
              <input type="hidden" name="status" value="under_review">
              <button class="btn btn-sm btn-outline-primary"><i class="fa-solid fa-clipboard-check"></i> <?= e($t('Under Review','Under Review')) ?></button>
            </form>
            <form method="post" action="<?= e(base_path('admin/submissions/status')) ?>">
              <input type="hidden" name="id" value="<?= e((string)$id) ?>">
              <input type="hidden" name="status" value="revision">
              <button class="btn btn-sm btn-outline-warning"><i class="fa-solid fa-pen-to-square"></i> <?= e($t('Revision','Revision')) ?></button>
            </form>
            <form method="post" action="<?= e(base_path('admin/submissions/status')) ?>">
              <input type="hidden" name="id" value="<?= e((string)$id) ?>">
              <input type="hidden" name="status" value="camera_ready">
              <button class="btn btn-sm btn-outline-success"><i class="fa-solid fa-check"></i> <?= e($t('Camera Ready','Camera Ready')) ?></button>
            </form>
            <form method="post" action="<?= e(base_path('admin/submissions/status')) ?>">
              <input type="hidden" name="id" value="<?= e((string)$id) ?>">
              <input type="hidden" name="status" value="published">
              <button class="btn btn-sm btn-success"><i class="fa-solid fa-bullhorn"></i> <?= e($t('Published','Published')) ?></button>
            </form>
          </div>

          <form method="post" action="<?= e(base_path('admin/submissions/status')) ?>" class="row g-2">
            <input type="hidden" name="id" value="<?= e((string)$id) ?>">
            <div class="col-md-8">
              <input class="form-control" name="status" placeholder="<?= e($t('حالة جديدة مثل accepted/rejected','New status e.g. accepted/rejected')) ?>" required>
            </div>
            <div class="col-md-4 d-grid">
              <button class="btn btn-primary"><i class="fa-solid fa-rotate"></i> <?= e($t('تحديث الحالة','Update Status')) ?></button>
            </div>
          </form>

        </div>
      </div>

      <div class="card border-0 shadow-sm">
        <div class="card-header bg-white fw-bold"><i class="fa-solid fa-gavel"></i> <?= e($t('اتخاذ القرار','Decision')) ?></div>
        <div class="card-body">
          <form method="post" action="<?= e(base_path('admin/decision-board/decide')) ?>">
            <input type="hidden" name="submission_id" value="<?= e((string)$id) ?>">
            <div class="row g-2">
              <div class="col-md-4">
                <select class="form-select" name="decision" required>
                  <option value="accepted"><?= e($t('قبول','Accepted')) ?></option>
                  <option value="rejected"><?= e($t('رفض','Rejected')) ?></option>
                  <option value="revision"><?= e($t('تعديلات','Revision')) ?></option>
                  <option value="camera_ready"><?= e($t('نسخة نهائية','Camera Ready')) ?></option>
                </select>
              </div>
              <div class="col-md-8">
                <input class="form-control" name="comments" placeholder="<?= e($t('ملاحظات للمؤلف (اختياري)','Comments to author (optional)')) ?>">
              </div>
            </div>
            <div class="d-grid mt-2">
              <button class="btn btn-danger"><i class="fa-solid fa-gavel"></i> <?= e($t('حفظ القرار وإشعار المؤلف','Save decision & notify author')) ?></button>
            </div>
          </form>
        </div>
      </div>

    </div>

    <div class="col-lg-5">
      <div class="card border-0 shadow-sm mb-3">
        <div class="card-header bg-white fw-bold"><i class="fa-solid fa-user-check"></i> <?= e($t('توزيع المحكمين','Assign Reviewers')) ?></div>
        <div class="card-body">
          <form method="post" action="<?= e(base_path('admin/submissions/assign')) ?>">
            <input type="hidden" name="id" value="<?= e((string)$id) ?>">
            <div class="mb-2">
              <label class="form-label"><?= e($t('المحكمون','Reviewers')) ?></label>
              <select class="form-select" name="reviewer_ids[]" multiple size="8" required>
                <?php foreach($reviewers as $u): ?>
                  <option value="<?= e((string)$u['id']) ?>"><?= e(trim(($u['first_name']??'').' '.($u['last_name']??''))) ?> — <?= e((string)$u['email']) ?></option>
                <?php endforeach; ?>
              </select>
            </div>

            <div class="row g-2">
              <div class="col-md-6">
                <label class="form-label"><?= e($t('الموعد النهائي','Due date')) ?></label>
                <input class="form-control" type="date" name="due_date">
              </div>
              <div class="col-md-6">
                <label class="form-label"><?= e($t('جولة التحكيم','Round')) ?></label>
                <input class="form-control" type="number" name="round_no" value="1" min="1">
              </div>
            </div>

            <div class="d-grid mt-3">
              <button class="btn btn-success"><i class="fa-solid fa-paper-plane"></i> <?= e($t('توزيع وإشعار المحكمين','Assign & Notify')) ?></button>
            </div>
          </form>
        </div>
      </div>

      <div class="card border-0 shadow-sm">
        <div class="card-header bg-white fw-bold"><i class="fa-solid fa-list-check"></i> <?= e($t('التوزيعات الحالية','Current Assignments')) ?></div>
        <div class="card-body p-0">
          <div class="table-responsive">
            <table class="table table-sm mb-0">
              <thead class="table-light"><tr><th><?= e($t('المحكم','Reviewer')) ?></th><th><?= e($t('الحالة','Status')) ?></th><th><?= e($t('الجولة','Round')) ?></th></tr></thead>
              <tbody>
                <?php foreach($assignments as $a): ?>
                  <tr>
                    <td><?= e(trim(($a['first_name']??'').' '.($a['last_name']??''))) ?></td>
                    <td><span class="badge bg-secondary"><?= e((string)($a['status']??'')) ?></span></td>
                    <td><?= e((string)($a['round_no']??1)) ?></td>
                  </tr>
                <?php endforeach; ?>
                <?php if(empty($assignments)): ?><tr><td colspan="3" class="text-center text-muted py-3">—</td></tr><?php endif; ?>
              </tbody>
            </table>
          </div>
        </div>
      </div>

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