<?php require __DIR__ . '/../../layouts/header.php'; $lang=current_lang(); ?>
<?php
$t=fn($ar,$en)=>($lang==='ar'?$ar:$en);
$sub=$sub??[]; $files=$files??[]; $reviews=$reviews??[];
$decision=$decision??null; $timeline=$timeline??[]; $responses=$responses??[];
$sid=(int)($sub['id']??0);
?>
<div class="container">

  <div class="d-flex justify-content-between align-items-center mb-3 flex-wrap gap-2">
    <div>
      <h4 class="mb-1"><i class="fa-solid fa-file-lines me-2"></i><?= e($t('ملف البحث','Submission ملف')) ?> #<?= e((string)$sid) ?></h4>
      <div class="text-muted small">
        <?= e($t('الحالة','Status')) ?>:
        <span class="badge bg-secondary"><?= e((string)($sub['status']??'')) ?></span>
      </div>
    </div>
    <div class="d-flex gap-2">
      <a class="btn btn-outline-secondary" href="<?= e(base_path('author/submissions')) ?>"><i class="fa-solid fa-arrow-right me-2"></i><?= e($t('رجوع','Back')) ?></a>
      <a class="btn btn-primary" href="<?= e(base_path('submit')) ?>"><i class="fa-solid fa-plus me-2"></i><?= e($t('تقديم جديد','New Submission')) ?></a>
    </div>
  </div>

  <div class="card border-0 shadow-sm mb-3">
    <div class="card-body">
      <div class="fw-bold mb-1"><?= e($t('العنوان','Title')) ?></div>
      <div class="mb-3"><?= e($lang==='ar'?(string)($sub['title_ar']??''):(string)($sub['title_en']??'')) ?></div>

      <div class="row g-3">
        <div class="col-lg-6">
          <div class="fw-bold mb-1"><?= e($t('الملخص','Abstract')) ?></div>
          <div class="text-muted"><?= $lang==='ar' ? (string)($sub['abstract_ar']??'') : (string)($sub['abstract_en']??'') ?></div>
        </div>
        <div class="col-lg-6">
          <div class="fw-bold mb-1"><?= e($t('الكلمات المفتاحية','Keywords')) ?></div>
          <div class="text-muted"><?= e($lang==='ar'?(string)($sub['keywords_ar']??''):(string)($sub['keywords_en']??'')) ?></div>
        </div>
      </div>
    </div>
  </div>

  <ul class="nav nav-pills mb-3 gap-2" id="subTabs" role="tablist">
    <li class="nav-item" role="presentation"><button class="nav-link active" data-bs-toggle="pill" data-bs-target="#tab-files" type="button"><?= e($t('الملفات','Files')) ?></button></li>
    <li class="nav-item" role="presentation"><button class="nav-link" data-bs-toggle="pill" data-bs-target="#tab-reviews" type="button"><?= e($t('المراجعات','Reviews')) ?></button></li>
    <li class="nav-item" role="presentation"><button class="nav-link" data-bs-toggle="pill" data-bs-target="#tab-decision" type="button"><?= e($t('القرار','Decision')) ?></button></li>
    <li class="nav-item" role="presentation"><button class="nav-link" data-bs-toggle="pill" data-bs-target="#tab-timeline" type="button"><?= e($t('سجل الأحداث','Timeline')) ?></button></li>
  </ul>

  <div class="tab-content">

    <!-- FILES -->
    <div class="tab-pane fade show active" id="tab-files">
      <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"><i class="fa-solid fa-paperclip me-2"></i><?= e($t('الملفات المرفوعة','Uploaded Files')) ?></div>
          </div>

          <?php if(!empty($files)): ?>
            <div class="list-group mb-3">
              <?php foreach($files as $f): ?>
                <a class="list-group-item list-group-item-action d-flex justify-content-between align-items-center"
                   target="_blank" href="<?= e(base_path((string)($f['file_path']??''))) ?>">
                  <div>
                    <div class="fw-bold"><i class="fa-solid fa-file-arrow-down me-2"></i><?= e((string)($f['original_name'] ?? $f['file_path'])) ?></div>
                    <div class="text-muted small">
                      <span class="badge bg-secondary"><?= e((string)($f['file_category']??'')) ?></span>
                      <?= !empty($f['created_at']) ? ' — '.e((string)$f['created_at']) : '' ?>
                    </div>
                  </div>
                  <i class="fa-solid fa-arrow-up-right-from-square"></i>
                </a>
              <?php endforeach; ?>
            </div>
          <?php else: ?>
            <div class="text-center text-muted py-4">—</div>
          <?php endif; ?>

          <hr>
          <div class="fw-bold mb-2"><?= e($t('رفع ملف إضافي / نسخة معدلة','Upload additional / revised file')) ?></div>
          <form method="post" action="<?= e(base_path('author/submissions/upload-revision')) ?>" enctype="multipart/form-data" class="row g-2">
            <input type="hidden" name="_csrf" value="<?= e(csrf_token()) ?>">
            <input type="hidden" name="submission_id" value="<?= e((string)$sid) ?>">
            <div class="col-md-4">
              <select class="form-select" name="file_category">
                <option value="revised_version"><?= e($t('نسخة معدلة','Revised Version')) ?></option>
                <option value="response_letter"><?= e($t('خطاب رد','Response Letter File')) ?></option>
                <option value="receipt"><?= e($t('إيصال','Receipt')) ?></option>
                <option value="galley"><?= e($t('نسخة نهائية','Galley')) ?></option>
                <option value="supplement"><?= e($t('ملحق','Supplement')) ?></option>
                <option value="other"><?= e($t('أخرى','Other')) ?></option>
              </select>
            </div>
            <div class="col-md-6"><input class="form-control" type="file" name="file" required></div>
            <div class="col-md-2 d-grid"><button class="btn btn-primary"><?= e($t('رفع','Upload')) ?></button></div>
          </form>
        </div>
      </div>
    </div>

    <!-- REVIEWS -->
    <div class="tab-pane fade" id="tab-reviews">
      <div class="card border-0 shadow-sm">
        <div class="card-body">
          <div class="fw-bold mb-2"><i class="fa-solid fa-comments me-2"></i><?= e($t('مراجعات المحكمين','Reviewer Feedback')) ?></div>

          <?php if(!empty($reviews)): ?>
            <?php foreach($reviews as $r): ?>
              <div class="p-3 rounded-4 border mb-2">
                <div class="d-flex justify-content-between flex-wrap gap-2">
                  <div class="fw-bold"><?= e($t('جولة','Round')) ?>: <?= e((string)($r['round_no']??1)) ?></div>
                  <div class="text-muted small"><?= e($t('الدرجة','Score')) ?>: <?= e((string)($r['score']??'')) ?></div>
                </div>
                <?php if(!empty($r['recommendation'])): ?>
                  <div class="text-muted small mt-1"><?= e($t('التوصية','Recommendation')) ?>: <span class="badge bg-secondary"><?= e((string)$r['recommendation']) ?></span></div>
                <?php endif; ?>
                <?php if(!empty($r['comments'])): ?>
                  <div class="text-muted mt-2"><?= e((string)$r['comments']) ?></div>
                <?php endif; ?>
                <?php if(!empty($r['file_path'])): ?>
                  <div class="mt-2"><a target="_blank" href="<?= e(base_path((string)$r['file_path'])) ?>"><?= e($t('ملف مرفق من المحكم','Reviewer attached file')) ?></a></div>
                <?php endif; ?>
              </div>
            <?php endforeach; ?>
          <?php else: ?>
            <div class="text-center text-muted py-4">—</div>
          <?php endif; ?>

          <hr>
          <div class="fw-bold mb-2"><?= e($t('ردّ على المراجعات (نص)','Response Letter (Text)')) ?></div>
          <form method="post" action="<?= e(base_path('author/submissions/response')) ?>" class="row g-2">
            <input type="hidden" name="_csrf" value="<?= e(csrf_token()) ?>">
            <input type="hidden" name="submission_id" value="<?= e((string)$sid) ?>">
            <div class="col-md-2"><input class="form-control" type="number" min="1" name="round_no" value="1"></div>
            <div class="col-md-10"><textarea class="form-control" name="response_text" rows="4" placeholder="<?= e($t('اكتب ردك هنا...','Write your response...')) ?>"></textarea></div>
            <div class="col-12 d-grid"><button class="btn btn-outline-primary"><?= e($t('حفظ الرد','Save response')) ?></button></div>
          </form>

          <?php if(!empty($responses)): ?>
            <div class="mt-3">
              <div class="text-muted small mb-2"><?= e($t('الردود السابقة','Previous responses')) ?></div>
              <?php foreach($responses as $rr): ?>
                <div class="p-3 rounded-4 border mb-2">
                  <div class="fw-bold"><?= e($t('الجولة','Round')) ?> <?= e((string)$rr['round_no']) ?> <span class="text-muted small">— <?= e((string)$rr['created_at']) ?></span></div>
                  <div class="text-muted mt-2"><?= e((string)($rr['response_text']??'')) ?></div>
                </div>
              <?php endforeach; ?>
            </div>
          <?php endif; ?>

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

    <!-- DECISION -->
    <div class="tab-pane fade" id="tab-decision">
      <div class="card border-0 shadow-sm">
        <div class="card-body">
          <div class="fw-bold mb-2"><i class="fa-solid fa-gavel me-2"></i><?= e($t('قرار اللجنة','Decision')) ?></div>
          <?php if(!empty($decision)): ?>
            <div class="mb-2"><span class="badge bg-secondary"><?= e((string)($decision['decision']??'')) ?></span></div>
            <div class="text-muted"><?= e((string)($decision['summary']??$decision['chair_comments']??'')) ?></div>
          <?php else: ?>
            <div class="text-center text-muted py-4">—</div>
          <?php endif; ?>
        </div>
      </div>
    </div>

    <!-- TIMELINE -->
    <div class="tab-pane fade" id="tab-timeline">
      <div class="card border-0 shadow-sm">
        <div class="card-body">
          <div class="fw-bold mb-2"><i class="fa-solid fa-timeline me-2"></i><?= e($t('سجل الإجراءات','Workflow Timeline')) ?></div>
          <?php if(!empty($timeline)): ?>
            <?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 else: ?>
            <div class="text-center text-muted py-4">—</div>
          <?php endif; ?>
        </div>
      </div>
    </div>

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