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

<div class="d-flex justify-content-between align-items-start mb-4 flex-wrap gap-2">
  <div>
    <a href="<?= base_url('admin/documents') ?>" class="btn btn-sm btn-outline-secondary mb-2">
      <i class="bi bi-arrow-left me-1"></i>Kembali
    </a>
    <h1 class="section-title mb-0"><?= esc(character_limiter($doc['title'], 70)) ?></h1>
  </div>
  <div class="d-flex gap-2 flex-wrap">
    <a href="<?= base_url('admin/documents/' . $doc['id'] . '/edit') ?>" class="btn btn-sm btn-outline-secondary">
      <i class="bi bi-pencil me-1"></i>Edit
    </a>
    <?php if (in_array($doc['status'], ['submitted','review'])): ?>
      <!-- Approve -->
      <form method="post" action="<?= base_url('admin/documents/' . $doc['id'] . '/approve') ?>" class="d-inline"
            onsubmit="return confirm('Terbitkan dokumen ini?')">
        <?= csrf_field() ?>
        <button type="submit" class="btn btn-sm btn-success">
          <i class="bi bi-check-circle me-1"></i>Terbitkan
        </button>
      </form>
    <?php endif; ?>
    <?php if ($doc['status'] === 'published'): ?>
      <form method="post" action="<?= base_url('admin/documents/' . $doc['id'] . '/unpublish') ?>" class="d-inline"
            onsubmit="return confirm('Tarik dokumen dari publikasi?')">
        <?= csrf_field() ?>
        <button type="submit" class="btn btn-sm btn-outline-warning">
          <i class="bi bi-eye-slash me-1"></i>Tarik
        </button>
      </form>
    <?php endif; ?>
    <form method="post" action="<?= base_url('admin/documents/' . $doc['id'] . '/delete') ?>" class="d-inline"
          onsubmit="return confirm('Hapus dokumen 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>
  </div>
</div>

<div class="row g-4">
  <div class="col-lg-8">
    <div class="card-stt p-4 mb-3">
      <div class="d-flex justify-content-between align-items-start mb-3">
        <span class="doc-type-badge"><?= esc($doc['type_name']) ?></span>
        <span class="status-badge status-<?= esc($doc['status']) ?>"><?= esc(ucfirst($doc['status'])) ?></span>
      </div>

      <table class="table table-sm small mb-3">
        <tr><td class="text-muted w-25">Penulis</td><td><?= esc($doc['author_name']) ?></td></tr>
        <?php if (!empty($doc['nim'])): ?>
          <tr><td class="text-muted">NIM</td><td><?= esc($doc['nim']) ?></td></tr>
        <?php endif; ?>
        <?php if (!empty($doc['program_name'])): ?>
          <tr><td class="text-muted">Program Studi</td><td><?= esc($doc['program_name']) ?></td></tr>
        <?php endif; ?>
        <?php if (!empty($doc['supervisor'])): ?>
          <tr><td class="text-muted">Pembimbing</td><td><?= esc($doc['supervisor']) ?></td></tr>
        <?php endif; ?>
        <tr><td class="text-muted">Tahun</td><td><?= esc($doc['year']) ?></td></tr>
        <tr><td class="text-muted">Dikutip</td><td><?= $doc['citations_count'] ?> kali</td></tr>
        <tr><td class="text-muted">Dilihat</td><td><?= number_format($doc['view_count']) ?></td></tr>
        <tr><td class="text-muted">Diunduh</td><td><?= number_format($doc['download_count']) ?></td></tr>
      </table>

      <?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; ?>
    </div>

    <?php if (!empty($refs)): ?>
      <div class="card-stt p-3 mb-3">
        <h6 class="fw-semibold mb-2">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['title'] ?? '') ?>
              <?php endif; ?>
            </li>
          <?php endforeach; ?>
        </ol>
      </div>
    <?php endif; ?>

    <!-- Revision / Reject forms -->
    <?php if (in_array($doc['status'], ['submitted','review'])): ?>
      <div class="row g-3">
        <div class="col-md-6">
          <div class="card-stt p-3 border border-warning">
            <h6 class="fw-semibold small mb-2"><i class="bi bi-pencil-square me-1"></i>Minta Revisi</h6>
            <form method="post" action="<?= base_url('admin/documents/' . $doc['id'] . '/revision') ?>">
              <?= csrf_field() ?>
              <textarea name="admin_note" class="form-control form-control-sm mb-2" rows="3"
                        placeholder="Catatan revisi untuk penulis..." required></textarea>
              <button type="submit" class="btn btn-sm btn-warning w-100">
                <i class="bi bi-arrow-return-left me-1"></i>Kembalikan untuk Revisi
              </button>
            </form>
          </div>
        </div>
        <div class="col-md-6">
          <div class="card-stt p-3 border border-danger">
            <h6 class="fw-semibold small mb-2"><i class="bi bi-x-circle me-1"></i>Tolak</h6>
            <form method="post" action="<?= base_url('admin/documents/' . $doc['id'] . '/reject') ?>">
              <?= csrf_field() ?>
              <textarea name="admin_note" class="form-control form-control-sm mb-2" rows="3"
                        placeholder="Alasan penolakan..."></textarea>
              <button type="submit" class="btn btn-sm btn-danger w-100"
                      onclick="return confirm('Tolak dokumen ini?')">
                <i class="bi bi-x-circle me-1"></i>Tolak Dokumen
              </button>
            </form>
          </div>
        </div>
      </div>
    <?php endif; ?>
  </div>

  <div class="col-lg-4">
    <?php if (!empty($doc['cover_img'])): ?>
      <img src="<?= base_url('covers/' . esc($doc['cover_img'])) ?>"
           class="img-fluid rounded mb-3 shadow-sm">
    <?php endif; ?>

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

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