<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>

<div class="d-flex justify-content-between align-items-start mb-4 flex-wrap gap-2">
  <div>
    <nav aria-label="breadcrumb" class="mb-1">
      <ol class="breadcrumb small mb-0">
        <li class="breadcrumb-item"><a href="<?= base_url('my-works') ?>">Karya Saya</a></li>
        <li class="breadcrumb-item active"><?= esc(character_limiter($doc['title'], 50)) ?></li>
      </ol>
    </nav>
  </div>
  <div class="d-flex gap-2">
    <?php if (in_array($doc['status'], ['draft', 'revision'])): ?>
      <a href="<?= base_url('submit/' . $doc['id'] . '/edit') ?>" class="btn btn-sm btn-outline-secondary">
        <i class="bi bi-pencil me-1"></i>Edit
      </a>
    <?php endif; ?>
    <?php if (in_array($doc['status'], ['draft', 'revision'])): ?>
      <form method="post" action="<?= base_url('submit/' . $doc['id'] . '/send') ?>" class="d-inline"
            onsubmit="return confirm('Kirim karya ini untuk ditinjau admin?')">
        <?= csrf_field() ?>
        <button type="submit" class="btn btn-sm btn-primary">
          <i class="bi bi-send me-1"></i>Kirim untuk Ditinjau
        </button>
      </form>
    <?php endif; ?>
    <?php if (in_array($doc['status'], ['draft', 'revision'])): ?>
      <form method="post" action="<?= base_url('submit/' . $doc['id'] . '/delete') ?>" class="d-inline"
            onsubmit="return confirm('Hapus karya ini secara permanen?')">
        <?= csrf_field() ?>
        <button type="submit" class="btn btn-sm btn-outline-danger">
          <i class="bi bi-trash me-1"></i>Hapus
        </button>
      </form>
    <?php endif; ?>
  </div>
</div>

<div class="row g-4">
  <div class="col-lg-8">
    <!-- Admin note -->
    <?php if (!empty($doc['admin_note']) && $doc['status'] === 'revision'): ?>
      <div class="alert alert-warning mb-3">
        <strong><i class="bi bi-exclamation-triangle me-2"></i>Catatan Revisi dari Admin:</strong>
        <p class="mb-0 mt-1"><?= nl2br(esc($doc['admin_note'])) ?></p>
      </div>
    <?php endif; ?>
    <?php if ($doc['status'] === 'rejected' && !empty($doc['admin_note'])): ?>
      <div class="alert alert-danger mb-3">
        <strong><i class="bi bi-x-circle me-2"></i>Alasan Penolakan:</strong>
        <p class="mb-0 mt-1"><?= nl2br(esc($doc['admin_note'])) ?></p>
      </div>
    <?php endif; ?>

    <div class="card-stt p-4">
      <span class="doc-type-badge mb-2 d-inline-block"><?= esc($doc['type_name']) ?></span>
      <h2 class="fs-5 fw-bold mb-2"><?= esc($doc['title']) ?></h2>
      <p class="text-muted small mb-3">
        <?= esc($doc['author_name']) ?> · <?= esc($doc['year']) ?>
        <?php if (!empty($doc['program_name'])): ?> · <?= esc($doc['program_name']) ?><?php endif; ?>
      </p>

      <?php if (!empty($doc['abstract'])): ?>
        <h6 class="fw-semibold small text-uppercase text-muted mb-1">Abstrak</h6>
        <p class="small mb-3" style="line-height:1.7;"><?= esc($doc['abstract']) ?></p>
      <?php endif; ?>

      <?php if (!empty($doc['keywords'])): ?>
        <p class="small mb-3">
          <span class="fw-semibold">Kata Kunci:</span>
          <?php foreach (explode(',', $doc['keywords']) as $kw): ?>
            <span class="badge bg-light text-dark border ms-1"><?= esc(trim($kw)) ?></span>
          <?php endforeach; ?>
        </p>
      <?php endif; ?>
    </div>

    <?php if (!empty($refs)): ?>
      <div class="card-stt p-3 mt-3">
        <h6 class="fw-semibold mb-2"><i class="bi bi-list-ol me-2"></i>Referensi (<?= count($refs) ?>)</h6>
        <ol class="ps-3 mb-0">
          <?php foreach ($refs as $ref): ?>
            <li class="small text-muted mb-1">
              <?php if ($ref['reference_type'] === 'internal'): ?>
                <span class="badge bg-gold text-dark me-1" style="font-size:.65rem;">Internal</span>
                <?= esc($ref['cited_title'] ?? $ref['title'] ?? '') ?>
              <?php else: ?>
                <?= esc($ref['raw_text'] ?? ($ref['author'] ? $ref['author'] . '. ' : '') . ($ref['title'] ?? '') . ($ref['year'] ? ' (' . $ref['year'] . ')' : '')) ?>
              <?php endif; ?>
            </li>
          <?php endforeach; ?>
        </ol>
      </div>
    <?php endif; ?>
  </div>

  <div class="col-lg-4">
    <div class="card-stt p-3">
      <h6 class="fw-semibold mb-3">Status Pengajuan</h6>
      <div class="text-center mb-3">
        <span class="status-badge status-<?= esc($doc['status']) ?>" style="font-size:.85rem;padding:6px 16px;">
          <?= esc(ucfirst($doc['status'])) ?>
        </span>
      </div>
      <table class="table table-sm small mb-0">
        <tr><td class="text-muted">Dibuat</td><td><?= date('d M Y', strtotime($doc['created_at'])) ?></td></tr>
        <?php if (!empty($doc['submitted_at'])): ?>
          <tr><td class="text-muted">Dikirim</td><td><?= date('d M Y', strtotime($doc['submitted_at'])) ?></td></tr>
        <?php endif; ?>
        <?php if (!empty($doc['published_at'])): ?>
          <tr><td class="text-muted">Diterbitkan</td><td><?= date('d M Y', strtotime($doc['published_at'])) ?></td></tr>
        <?php endif; ?>
      </table>
    </div>

    <?php if ($doc['status'] === 'published'): ?>
      <div class="card-stt p-3 mt-3">
        <a href="<?= base_url('documents/' . $doc['id']) ?>" class="btn btn-outline-primary w-100 btn-sm">
          <i class="bi bi-eye me-1"></i>Lihat di Repositori
        </a>
      </div>
    <?php endif; ?>
  </div>
</div>

<?= $this->endSection() ?>
