<?php require __DIR__ . '/../layouts/header.php'; $lang=current_lang(); ?>
<?php
$r = $row ?? [];
$sid = (int)($r['id'] ?? 0);
$title = (string)($lang==='ar' ? ($r['title_ar'] ?? $r['title_en'] ?? '') : ($r['title_en'] ?? $r['title_ar'] ?? ''));
$status = (string)($r['status'] ?? '');
$confTitle = (string)($lang==='ar' ? ($r['conf_title_ar'] ?? '') : ($r['conf_title_en'] ?? ''));
$canRevUpload = in_array($status, ['revision_minor','revision_major','camera_ready_required'], true);
?>
<div class="container" style="max-width:1150px">
  <div class="d-flex justify-content-between align-items-center mb-3 flex-wrap gap-2">
    <div>
      <h4 class="mb-0"><i class="fa-solid fa-file-circle-check me-2"></i><?= e($lang==='ar'?'تفاصيل البحث':'Submission Details') ?> #<?= e((string)$sid) ?></h4>
      <div class="text-muted small"><?= e($confTitle) ?> • <span class="badge bg-secondary"><?= e($status) ?></span></div>
    </div>
    <div class="d-flex gap-2 flex-wrap">
      <a class="btn btn-outline-secondary btn-rounded" href="<?= e(base_path('author/submissions')) ?>"><?= e($lang==='ar'?'رجوع':'Back') ?></a>
      <a class="btn btn-outline-primary btn-rounded" href="<?= e(base_path('author/work/edit?id='.(int)$sid)) ?>"><i class="fa-solid fa-pen me-2"></i><?= e($lang==='ar'?'تعديل':'Edit') ?></a>
      <form method="post" action="<?= e(base_path('author/work/withdraw')) ?>" class="d-inline">
        <input type="hidden" name="_csrf" value="<?= e(csrf_token()) ?>">
        <input type="hidden" name="id" value="<?= e((string)$sid) ?>">
        <button class="btn btn-outline-danger btn-rounded" onclick="return confirm('Withdraw?')"><i class="fa-solid fa-box-archive me-2"></i><?= e($lang==='ar'?'سحب البحث':'Withdraw') ?></button>
      </form>
      <?php if ($canRevUpload): ?>
        <button class="btn btn-primary btn-rounded" data-bs-toggle="modal" data-bs-target="#revModal">
          <i class="fa-solid fa-upload me-2"></i><?= e($lang==='ar'?'رفع نسخة معدلة':'Upload Revision') ?>
        </button>
      <?php endif; ?>
    </div>
  </div>

  <div class="card border-0 shadow-sm mb-3">
    <div class="card-body">
      <div class="fw-bold mb-1"><?= e($title) ?></div>
      <div class="text-muted small"><?= e($lang==='ar'?'يمكنك متابعة الملفات والتحكيم والقرارات من التبويبات أدناه.':'Track your files, reviews and decisions using tabs below.') ?></div>
    </div>
  </div>

  <ul class="nav nav-tabs" role="tablist">
    <li class="nav-item"><button class="nav-link active" data-bs-toggle="tab" data-bs-target="#tabMeta"><?= e($lang==='ar'?'البيانات':'Metadata') ?></button></li>
    <li class="nav-item"><button class="nav-link" data-bs-toggle="tab" data-bs-target="#tabFiles"><?= e($lang==='ar'?'الملفات':'Files') ?></button></li>
    <li class="nav-item"><button class="nav-link" data-bs-toggle="tab" data-bs-target="#tabReviews"><?= e($lang==='ar'?'التحكيم':'Reviews') ?></button></li>
    <li class="nav-item"><button class="nav-link" data-bs-toggle="tab" data-bs-target="#tabDec"><?= e($lang==='ar'?'القرارات':'Decisions') ?></button></li>
    <li class="nav-item"><button class="nav-link" data-bs-toggle="tab" data-bs-target="#tabTime"><?= e($lang==='ar'?'السجل':'Timeline') ?></button></li>
  </ul>

  <div class="tab-content border border-top-0 rounded-bottom p-3 bg-white shadow-sm">
    <div class="tab-pane fade show active" id="tabMeta">
      <div class="row g-3">
        <div class="col-md-6">
          <div class="text-muted small"><?= e($lang==='ar'?'العنوان (EN)':'Title (EN)') ?></div>
          <div class="fw-bold"><?= e((string)($r['title_en'] ?? '')) ?></div>
        </div>
        <div class="col-md-6">
          <div class="text-muted small"><?= e($lang==='ar'?'العنوان (AR)':'Title (AR)') ?></div>
          <div class="fw-bold"><?= e((string)($r['title_ar'] ?? '')) ?></div>
        </div>
        <div class="col-md-6">
          <div class="text-muted small"><?= e($lang==='ar'?'تاريخ التقديم':'Submitted at') ?></div>
          <div><?= e((string)($r['created_at'] ?? '')) ?></div>
        </div>
        <div class="col-md-6">
          <div class="text-muted small"><?= e($lang==='ar'?'الحالة الحالية':'Current status') ?></div>
          <div><span class="badge bg-secondary"><?= e($status) ?></span></div>
        </div>
      </div>
    </div>

    <div class="tab-pane fade" id="tabFiles">
      <?php if (empty($files)): ?>
        <div class="text-center text-muted py-4">—</div>
      <?php else: ?>
        <div class="table-responsive">
          <table class="table align-middle">
            <thead><tr><th><?= e($lang==='ar'?'التصنيف':'Category') ?></th><th><?= e($lang==='ar'?'الملف':'File') ?></th><th><?= e($lang==='ar'?'الجولة':'Round') ?></th><th><?= e($lang==='ar'?'تاريخ':'Created') ?></th></tr></thead>
            <tbody>
              <?php foreach ($files as $f): ?>
                <tr>
                  <td><span class="badge bg-light text-dark"><?= e((string)$f['file_category']) ?></span></td>
                  <td><a href="<?= e(base_path((string)$f['file_path'])) ?>" target="_blank"><?= e((string)($f['original_name'] ?? 'file')) ?></a></td>
                  <td><?= e((string)($f['round_no'] ?? '1')) ?></td>
                  <td class="text-muted"><?= e((string)($f['created_at'] ?? '')) ?></td>
                </tr>
              <?php endforeach; ?>
            </tbody>
          </table>
        </div>
      <?php endif; ?>
    </div>

    <div class="tab-pane fade" id="tabReviews">
      <?php if (empty($reviews)): ?>
        <div class="text-center text-muted py-4"><?= e($lang==='ar'?'لا توجد تقارير تحكيم متاحة بعد.':'No reviews available yet.') ?></div>
      <?php else: ?>
        <div class="accordion" id="revAcc">
          <?php foreach ($reviews as $i=>$rv): ?>
            <?php
              $cta = (string)($rv['comments_to_author'] ?? '');
              $rec = (string)($rv['recommendation'] ?? '');
            ?>
            <div class="accordion-item">
              <h2 class="accordion-header">
                <button class="accordion-button <?= $i===0?'':'collapsed' ?>" data-bs-toggle="collapse" data-bs-target="#rv<?= $i ?>">
                  <?= e($lang==='ar'?'تقرير تحكيم':'Review') ?> #<?= e((string)($rv['id'] ?? '')) ?>
                  <?php if ($rec!==''): ?> <span class="badge bg-secondary ms-2"><?= e($rec) ?></span><?php endif; ?>
                </button>
              </h2>
              <div id="rv<?= $i ?>" class="accordion-collapse collapse <?= $i===0?'show':'' ?>" data-bs-parent="#revAcc">
                <div class="accordion-body">
                  <div class="text-muted small mb-2"><?= e($lang==='ar'?'ملاحظات للمؤلف':'Comments to author') ?></div>
                  <div style="white-space:pre-wrap"><?= e($cta) ?></div>
                </div>
              </div>
            </div>
          <?php endforeach; ?>
        </div>
      <?php endif; ?>
    </div>

    <div class="tab-pane fade" id="tabDec">
      <?php if (empty($decisions)): ?>
        <div class="text-center text-muted py-4">—</div>
      <?php else: ?>
        <div class="table-responsive">
          <table class="table align-middle">
            <thead><tr><th><?= e($lang==='ar'?'الجولة':'Round') ?></th><th><?= e($lang==='ar'?'القرار':'Decision') ?></th><th><?= e($lang==='ar'?'ملاحظات':'Comments') ?></th><th><?= e($lang==='ar'?'تاريخ':'Date') ?></th></tr></thead>
            <tbody>
              <?php foreach ($decisions as $d): ?>
                <tr>
                  <td><?= e((string)($d['round_no'] ?? '1')) ?></td>
                  <td><span class="badge bg-primary"><?= e((string)($d['decision'] ?? '')) ?></span></td>
                  <td style="white-space:pre-wrap"><?= e((string)($d['chair_comments'] ?? '')) ?></td>
                  <td class="text-muted"><?= e((string)($d['decided_at'] ?? $d['created_at'] ?? '')) ?></td>
                </tr>
              <?php endforeach; ?>
            </tbody>
          </table>
        </div>
      <?php endif; ?>
    </div>

    <div class="tab-pane fade" id="tabTime">
      <div class="text-muted small mb-2"><?= e($lang==='ar'?'سيظهر هنا سجل تغيّر الحالات والتحديثات القادمة.':'Status changes will appear here.') ?></div>
      <?php
        // optional timeline if table exists
        $hist = [];
        try {
          $db = \App\Core\Database::getInstance();
          $st = $db->prepare("SELECT * FROM submission_status_history WHERE submission_id=:id ORDER BY id DESC LIMIT 100");
          $st->execute([':id'=>$sid]);
          $hist = $st->fetchAll();
        } catch (\Throwable $e) {}
      ?>
      <?php if (empty($hist)): ?>
        <div class="text-center text-muted py-4">—</div>
      <?php else: ?>
        <div class="list-group">
          <?php foreach ($hist as $h): ?>
            <div class="list-group-item d-flex justify-content-between gap-3">
              <div>
                <div class="fw-bold"><?= e((string)($h['old_status'] ?? '')) ?> → <?= e((string)$h['new_status']) ?></div>
                <div class="text-muted small"><?= e((string)($h['note'] ?? '')) ?></div>
              </div>
              <div class="text-muted small"><?= e((string)($h['created_at'] ?? '')) ?></div>
            </div>
          <?php endforeach; ?>
        </div>
      <?php endif; ?>
    </div>
  </div>
</div>

<?php if ($canRevUpload): ?>
<div class="modal fade" id="revModal" tabindex="-1">
  <div class="modal-dialog">
    <div class="modal-content">
      <form method="post" action="<?= e(base_path('author/revision/upload')) ?>" enctype="multipart/form-data">
        <input type="hidden" name="_csrf" value="<?= e(csrf_token()) ?>">
        <input type="hidden" name="submission_id" value="<?= e((string)$sid) ?>">
        <input type="hidden" name="round_no" value="1">
        <div class="modal-header">
          <h5 class="modal-title"><?= e($lang==='ar'?'رفع نسخة معدلة':'Upload Revision') ?></h5>
          <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
        </div>
        <div class="modal-body">
          <label class="form-label"><?= e($lang==='ar'?'ملف النسخة المعدلة (PDF/DOCX)':'Revision file (PDF/DOCX)') ?></label>
          <input class="form-control" type="file" name="revision" required>
        </div>
        <div class="modal-footer">
          <button class="btn btn-primary"><?= e($lang==='ar'?'رفع':'Upload') ?></button>
        </div>
      </form>
    </div>
  </div>
</div>
<?php endif; ?>

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