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

<div class="row g-4">
  <!-- Main doc info -->
  <div class="col-lg-8">
    <nav aria-label="breadcrumb" class="mb-3">
      <ol class="breadcrumb small">
        <li class="breadcrumb-item"><a href="<?= base_url('/') ?>">Repositori</a></li>
        <li class="breadcrumb-item active"><?= esc(character_limiter($doc['title'], 60)) ?></li>
      </ol>
    </nav>

    <span class="doc-type-badge mb-2 d-inline-block"><?= esc($doc['type_name']) ?></span>
    <h1 class="fs-4 fw-bold text-dark mb-1"><?= esc($doc['title']) ?></h1>
    <p class="text-muted mb-1">
      <i class="bi bi-person me-1"></i><?= esc($doc['author_name']) ?>
      <?php if (!empty($doc['nim'])): ?> · NIM <?= esc($doc['nim']) ?><?php endif; ?>
    </p>
    <p class="text-muted small mb-3">
      <?= esc($doc['year']) ?>
      <?php if (!empty($doc['program_name'])): ?> · <?= esc($doc['program_name']) ?><?php endif; ?>
      <?php if (!empty($doc['supervisor'])): ?> · Pembimbing: <?= esc($doc['supervisor']) ?><?php endif; ?>
    </p>

    <!-- Stats -->
    <div class="d-flex gap-4 mb-4 text-muted small">
      <span><i class="bi bi-eye me-1"></i><?= number_format($doc['view_count']) ?> dilihat</span>
      <span><i class="bi bi-download me-1"></i><?= number_format($doc['download_count']) ?> unduhan</span>
      <span><i class="bi bi-quote me-1"></i><?= number_format($doc['citations_count']) ?> dikutip</span>
    </div>

    <!-- Abstract -->
    <?php if (!empty($doc['abstract'])): ?>
      <div class="card-stt p-3 mb-4">
        <h6 class="fw-semibold mb-2"><i class="bi bi-text-paragraph me-2"></i>Abstrak</h6>
        <p class="text-muted small mb-0" style="line-height:1.7;"><?= esc($doc['abstract']) ?></p>
      </div>
    <?php endif; ?>

    <!-- Keywords -->
    <?php if (!empty($doc['keywords'])): ?>
      <div class="mb-4">
        <span class="fw-semibold small">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; ?>
      </div>
    <?php endif; ?>

    <!-- References -->
    <?php if (!empty($references)): ?>
      <div class="card-stt p-3 mb-4">
        <h6 class="fw-semibold mb-3"><i class="bi bi-list-ol me-2"></i>Daftar Referensi</h6>
        <ol class="mb-0 ps-3">
          <?php foreach ($references as $i => $ref): ?>
            <li class="small text-muted mb-2" style="line-height:1.6;">
              <?php if ($ref['reference_type'] === 'internal' && !empty($ref['cited_document_id'])): ?>
                <a href="<?= base_url('documents/' . $ref['cited_document_id']) ?>" class="text-decoration-none fw-semibold">
                  <?= esc($ref['cited_title'] ?? $ref['title']) ?>
                </a>
                <?php if (!empty($ref['cited_author'])): ?> — <?= esc($ref['cited_author']) ?><?php endif; ?>
                <?php if (!empty($ref['cited_year'])): ?> (<?= esc($ref['cited_year']) ?>)<?php endif; ?>
                <span class="badge bg-gold text-dark ms-1" style="font-size:.65rem;">Internal</span>
              <?php else: ?>
                <?php
                  $parts = [];
                  if (!empty($ref['author'])) $parts[] = esc($ref['author']);
                  if (!empty($ref['year'])) $parts[] = '(' . esc($ref['year']) . ')';
                  if (!empty($ref['title'])) $parts[] = '<em>' . esc($ref['title']) . '</em>';
                  if (!empty($ref['journal'])) $parts[] = esc($ref['journal']);
                  if (!empty($ref['volume'])) $parts[] = 'Vol. ' . esc($ref['volume']);
                  if (!empty($ref['pages'])) $parts[] = 'p. ' . esc($ref['pages']);
                  echo implode(', ', $parts) ?: esc($ref['raw_text'] ?? '');
                ?>
                <?php if (!empty($ref['doi'])): ?>
                  · <a href="https://doi.org/<?= esc($ref['doi']) ?>" target="_blank" rel="noopener" class="text-muted">
                    DOI: <?= esc($ref['doi']) ?>
                  </a>
                <?php elseif (!empty($ref['url'])): ?>
                  · <a href="<?= esc($ref['url']) ?>" target="_blank" rel="noopener" class="text-muted">
                    <i class="bi bi-link-45deg"></i> Link
                  </a>
                <?php endif; ?>
              <?php endif; ?>
            </li>
          <?php endforeach; ?>
        </ol>
      </div>
    <?php endif; ?>

    <!-- Cited by -->
    <?php if (!empty($cited_by)): ?>
      <div class="card-stt p-3">
        <h6 class="fw-semibold mb-3"><i class="bi bi-diagram-2 me-2"></i>Dikutip oleh</h6>
        <ul class="list-unstyled mb-0">
          <?php foreach ($cited_by as $c): ?>
            <li class="mb-2 small">
              <a href="<?= base_url('documents/' . $c['citing_document_id']) ?>" class="fw-semibold text-decoration-none">
                <?= esc($c['title']) ?>
              </a>
              <span class="text-muted"> — <?= esc($c['author_name']) ?> (<?= esc($c['year']) ?>)</span>
            </li>
          <?php endforeach; ?>
        </ul>
      </div>
    <?php endif; ?>
  </div>

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

    <div class="card-stt p-3 mb-3">
      <h6 class="fw-semibold mb-3">Aksi</h6>
      <?php if (auth()->loggedIn()): ?>
        <a href="<?= base_url('documents/' . $doc['id'] . '/read') ?>" class="btn btn-primary w-100 mb-2">
          <i class="bi bi-book me-2"></i>Baca Online
        </a>
        <a href="<?= base_url('documents/' . $doc['id'] . '/download') ?>" class="btn btn-outline-primary w-100">
          <i class="bi bi-download me-2"></i>Unduh PDF
        </a>
      <?php else: ?>
        <a href="<?= base_url('login') ?>" class="btn btn-primary w-100 mb-2">
          <i class="bi bi-box-arrow-in-right me-2"></i>Masuk untuk Membaca
        </a>
      <?php endif; ?>
    </div>

    <div class="card-stt p-3">
      <h6 class="fw-semibold mb-2">Detail</h6>
      <table class="table table-sm small mb-0">
        <tr><td class="text-muted">Jenis</td><td><?= esc($doc['type_name']) ?></td></tr>
        <tr><td class="text-muted">Tahun</td><td><?= esc($doc['year']) ?></td></tr>
        <?php if (!empty($doc['program_name'])): ?>
          <tr><td class="text-muted">Prodi</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; ?>
        <?php if (!empty($doc['publisher'])): ?>
          <tr><td class="text-muted">Penerbit</td><td><?= esc($doc['publisher']) ?></td></tr>
        <?php endif; ?>
        <tr><td class="text-muted">Diterbitkan</td><td><?= !empty($doc['published_at']) ? date('d M Y', strtotime($doc['published_at'])) : '-' ?></td></tr>
      </table>
    </div>
  </div>
</div>

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