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

<!-- Stat cards -->
<div class="row g-3 mb-4">
  <?php
    $cards = [
      ['label'=>'Total Dokumen',   'value'=>$docStats['total'],     'icon'=>'bi-file-earmark-text', 'color'=>'#efbb20'],
      ['label'=>'Diterbitkan',     'value'=>$docStats['published'], 'icon'=>'bi-check-circle',      'color'=>'#357b49'],
      ['label'=>'Menunggu Tinjau', 'value'=>$docStats['pending'],   'icon'=>'bi-hourglass-split',   'color'=>'#0693e3'],
      ['label'=>'Total Pengguna',  'value'=>$userCounts['total'],   'icon'=>'bi-people',            'color'=>'#7c4dff'],
    ];
  ?>
  <?php foreach ($cards as $c): ?>
    <div class="col-6 col-lg-3">
      <div class="card-stt p-3 d-flex align-items-center gap-3">
        <div class="rounded-circle d-flex align-items-center justify-content-center"
             style="width:44px;height:44px;background:<?= $c['color'] ?>22;">
          <i class="bi <?= $c['icon'] ?>" style="color:<?= $c['color'] ?>;font-size:1.25rem;"></i>
        </div>
        <div>
          <div class="fw-bold fs-5 text-dark"><?= number_format($c['value']) ?></div>
          <div class="text-muted small"><?= $c['label'] ?></div>
        </div>
      </div>
    </div>
  <?php endforeach; ?>
</div>

<?php if ($docStats['pending'] > 0): ?>
  <div class="alert alert-warning py-2 mb-4">
    <i class="bi bi-exclamation-triangle me-2"></i>
    Ada <strong><?= $docStats['pending'] ?></strong> karya yang menunggu ditinjau.
    <a href="<?= base_url('admin/documents?status=submitted') ?>" class="fw-semibold ms-2">Tinjau sekarang →</a>
  </div>
<?php endif; ?>

<div class="row g-4">
  <!-- Top cited -->
  <div class="col-lg-6">
    <div class="card-stt p-3">
      <h6 class="fw-semibold mb-3"><i class="bi bi-quote me-2 text-gold"></i>Paling Banyak Dikutip</h6>
      <?php if (empty($topCited)): ?>
        <p class="text-muted small">Belum ada data.</p>
      <?php else: ?>
        <ol class="ps-3 mb-0">
          <?php foreach ($topCited as $d): ?>
            <li class="mb-2">
              <a href="<?= base_url('admin/documents/' . $d['id']) ?>" class="fw-semibold text-decoration-none small">
                <?= esc(character_limiter($d['title'], 60)) ?>
              </a>
              <span class="text-muted small"> — <?= $d['citations_count'] ?> kutipan</span>
            </li>
          <?php endforeach; ?>
        </ol>
      <?php endif; ?>
    </div>
  </div>

  <!-- Top downloaded -->
  <div class="col-lg-6">
    <div class="card-stt p-3">
      <h6 class="fw-semibold mb-3"><i class="bi bi-download me-2 text-gold"></i>Paling Banyak Diunduh</h6>
      <?php if (empty($topDl)): ?>
        <p class="text-muted small">Belum ada data.</p>
      <?php else: ?>
        <ol class="ps-3 mb-0">
          <?php foreach ($topDl as $d): ?>
            <li class="mb-2">
              <a href="<?= base_url('admin/documents/' . $d['id']) ?>" class="fw-semibold text-decoration-none small">
                <?= esc(character_limiter($d['title'], 60)) ?>
              </a>
              <span class="text-muted small"> — <?= number_format($d['download_count']) ?> unduhan</span>
            </li>
          <?php endforeach; ?>
        </ol>
      <?php endif; ?>
    </div>
  </div>

  <!-- Recent activity -->
  <div class="col-12">
    <div class="card-stt p-3">
      <div class="d-flex justify-content-between align-items-center mb-3">
        <h6 class="fw-semibold mb-0"><i class="bi bi-activity me-2 text-gold"></i>Aktivitas Terkini</h6>
        <a href="<?= base_url('admin/logs') ?>" class="small text-muted">Lihat semua →</a>
      </div>
      <div class="table-responsive">
        <table class="table table-sm table-stt mb-0">
          <thead><tr><th>Pengguna</th><th>Aksi</th><th>Dokumen</th><th>Waktu</th></tr></thead>
          <tbody>
            <?php foreach ($recentLogs as $log): ?>
              <tr>
                <td class="small"><?= esc($log['user_name'] ?? 'Tamu') ?></td>
                <td><span class="badge bg-secondary"><?= esc($log['activity_type']) ?></span></td>
                <td class="small text-muted"><?= esc(character_limiter($log['document_title'] ?? '—', 50)) ?></td>
                <td class="small text-muted"><?= date('d M H:i', strtotime($log['created_at'])) ?></td>
              </tr>
            <?php endforeach; ?>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>

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