<?php require __DIR__ . '/../../layouts/header.php'; $lang=current_lang(); ?>
<?php
$t = fn($ar,$en)=>($lang==='ar'?$ar:$en);
$sub=$sub??[];
$files=$files??[];
$decision=$decision??null;
?>
<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($t('تفاصيل البحث','Submission Details')) ?> #<?= e((string)($sub['id']??'')) ?></h4>
    <a class="btn btn-outline-secondary" href="<?= e(base_path('admin/submissions')) ?>"><?= e($t('رجوع','Back')) ?></a>
  </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="mb-2 fw-bold"><?= e($t('العنوان','Title')) ?></div>
          <div><?= e($lang==='ar'?(string)($sub['title_ar']??''):(string)($sub['title_en']??'')) ?></div>

          <hr>
          <div class="mb-2 fw-bold"><?= e($t('الملخص','Abstract')) ?></div>
          <div class="text-muted"><?= $lang==='ar' ? (string)($sub['abstract_ar']??'') : (string)($sub['abstract_en']??'') ?></div>

          <hr>
          <div class="mb-2 fw-bold"><?= e($t('الكلمات المفتاحية','Keywords')) ?></div>
          <div class="text-muted"><?= e($lang==='ar'?(string)($sub['keywords_ar']??''):(string)($sub['keywords_en']??'')) ?></div>
        </div>
      </div>

      <div class="card border-0 shadow-sm">
        <div class="card-body">
          <div class="d-flex justify-content-between align-items-center mb-2">
            <div class="fw-bold"><?= e($t('الملفات','Files')) ?></div>
          </div>

          <div class="table-responsive">
            <table class="table align-middle">
              <thead><tr><th>#</th><th><?= e($t('الفئة','Category')) ?></th><th><?= e($t('الملف','File')) ?></th><th><?= e($t('تاريخ','Date')) ?></th></tr></thead>
              <tbody>
              <?php foreach($files as $f): ?>
                <tr>
                  <td><?= e((string)$f['id']) ?></td>
                  <td><span class="badge bg-secondary"><?= e((string)($f['file_category']??'')) ?></span></td>
                  <td>
                    <a target="_blank" href="<?= e(base_path((string)$f['file_path'])) ?>">
                      <?= e((string)($f['original_name'] ?? $f['file_path'])) ?>
                    </a>
                  </td>
                  <td class="text-muted"><?= e((string)($f['created_at']??'')) ?></td>
                </tr>
              <?php endforeach; ?>
              <?php if(empty($files)): ?><tr><td colspan="4" class="text-center text-muted py-4">—</td></tr><?php endif; ?>
              </tbody>
            </table>
          </div>

        </div>
      </div>
    </div>

    <div class="col-lg-5">
      <div class="card border-0 shadow-sm mb-3">
        <div class="card-body">
          <div class="fw-bold mb-2"><?= e($t('الحالة الحالية','Current Status')) ?></div>
          <div class="mb-3"><span class="badge bg-secondary"><?= e((string)($sub['status']??'')) ?></span></div>

          <form method="post" action="<?= e(base_path('admin/submissions/update-status')) ?>" class="row g-2">
            <input type="hidden" name="_csrf" value="<?= e(csrf_token()) ?>">
            <input type="hidden" name="id" value="<?= e((string)($sub['id']??0)) ?>">

            <div class="col-12">
              <label class="form-label"><?= e($t('تحديث الحالة','Update Status')) ?></label>
              <select class="form-select" name="status">
                <?php foreach(['submitted','under_review','accepted','minor_revision','major_revision','rejected','camera_ready','production','copyedit','galley','published'] as $s): ?>
                  <option value="<?= e($s) ?>" <?= ((string)($sub['status']??'')===$s?'selected':'') ?>><?= e($s) ?></option>
                <?php endforeach; ?>
              </select>
            </div>

            <div class="col-12">
              <label class="form-label"><?= e($t('ملاحظة للمؤلف (اختياري)','Note to author (optional)')) ?></label>
              <input class="form-control" name="note" placeholder="<?= e($t('مثال: الرجاء رفع نسخة معدّلة','e.g. Please upload a revised version')) ?>">
            </div>

            <div class="col-12 d-grid">
              <button class="btn btn-primary"><?= e($t('حفظ','Save')) ?></button>
            </div>
          </form>

          <?php if(!empty($decision)): ?>
            <hr>
            <div class="fw-bold mb-2"><?= e($t('قرار اللجنة','Decision')) ?></div>
            <div class="text-muted"><?= e((string)($decision['decision']??'')) ?></div>
            <div class="text-muted small mt-2"><?= e((string)($decision['summary']??'')) ?></div>
          <?php endif; ?>
        </div>
      </div>
    </div>

  </div>

</div>

<hr>
<div class="fw-bold mb-2"><i class="fa-solid fa-timeline me-2"></i>Timeline</div>
<?php
$timeline=[];
try{
  $db=\App\Core\Database::getInstance();
  $tl=$db->prepare("SELECT * FROM submission_status_logs WHERE submission_id=:s ORDER BY id DESC LIMIT 50");
  $tl->execute([':s'=>(int)($sub['id']??0)]);
  $timeline=$tl->fetchAll()?:[];
}catch(\Throwable $e){}
?>
<div class="mt-2">
  <?php foreach($timeline as $ev): ?>
    <div class="p-3 rounded-4 border mb-2">
      <div class="fw-bold"><?= e((string)($ev['new_status']??'')) ?></div>
      <div class="text-muted small"><?= e((string)($ev['created_at']??'')) ?></div>
      <?php if(!empty($ev['note'])): ?><div class="text-muted mt-2"><?= e((string)$ev['note']) ?></div><?php endif; ?>
    </div>
  <?php endforeach; ?>
  <?php if(empty($timeline)): ?><div class="text-center text-muted py-3">—</div><?php endif; ?>
</div>


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