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

<div class="d-flex justify-content-between align-items-center mb-4">
  <h1 class="section-title mb-0">Dokumen</h1>
  <a href="<?= base_url('admin/documents/new') ?>" class="btn btn-primary btn-sm">
    <i class="bi bi-plus-circle me-1"></i>Tambah Dokumen
  </a>
</div>

<!-- Filters -->
<form method="get" action="<?= base_url('admin/documents') ?>" class="card-stt p-3 mb-4">
  <div class="row g-2 align-items-end">
    <div class="col-md-4">
      <input type="text" name="q" class="form-control form-control-sm"
             placeholder="Cari judul, penulis..."
             value="<?= esc($filters['search'] ?? '') ?>">
    </div>
    <div class="col-md-2">
      <select name="status" class="form-select form-select-sm">
        <option value="">Semua Status</option>
        <?php foreach (['draft','submitted','review','revision','published','rejected'] as $s): ?>
          <option value="<?= $s ?>" <?= ($filters['status'] ?? '') === $s ? 'selected' : '' ?>>
            <?= ucfirst($s) ?>
          </option>
        <?php endforeach; ?>
      </select>
    </div>
    <div class="col-md-2">
      <select name="type" class="form-select form-select-sm">
        <option value="">Semua Jenis</option>
        <?php foreach ($types as $t): ?>
          <option value="<?= $t['id'] ?>" <?= ($filters['type'] ?? '') == $t['id'] ? 'selected' : '' ?>>
            <?= esc($t['name']) ?>
          </option>
        <?php endforeach; ?>
      </select>
    </div>
    <div class="col-md-2">
      <select name="program" class="form-select form-select-sm">
        <option value="">Semua Prodi</option>
        <?php foreach ($programs as $p): ?>
          <option value="<?= $p['id'] ?>" <?= ($filters['program'] ?? '') == $p['id'] ? 'selected' : '' ?>>
            <?= esc($p['name']) ?>
          </option>
        <?php endforeach; ?>
      </select>
    </div>
    <div class="col-md-2 d-flex gap-2">
      <button type="submit" class="btn btn-sm btn-primary">Filter</button>
      <a href="<?= base_url('admin/documents') ?>" class="btn btn-sm btn-outline-secondary">Reset</a>
    </div>
  </div>
</form>

<div class="card-stt overflow-hidden">
  <table class="table table-stt mb-0">
    <thead>
      <tr>
        <th>Judul</th>
        <th>Penulis</th>
        <th>Jenis</th>
        <th>Tahun</th>
        <th>Status</th>
        <th>Dikutip</th>
        <th></th>
      </tr>
    </thead>
    <tbody>
      <?php if (empty($result['data'])): ?>
        <tr><td colspan="7" class="text-center text-muted py-4">Tidak ada dokumen.</td></tr>
      <?php else: ?>
        <?php foreach ($result['data'] as $doc): ?>
          <tr>
            <td style="max-width:260px;">
              <a href="<?= base_url('admin/documents/' . $doc['id']) ?>" class="fw-semibold text-decoration-none">
                <?= esc(character_limiter($doc['title'], 70)) ?>
              </a>
            </td>
            <td class="small text-muted"><?= esc($doc['author_name']) ?></td>
            <td class="small text-muted"><?= esc($doc['type_name']) ?></td>
            <td class="small text-muted"><?= esc($doc['year']) ?></td>
            <td>
              <span class="status-badge status-<?= esc($doc['status']) ?>">
                <?= esc(ucfirst($doc['status'])) ?>
              </span>
            </td>
            <td class="small text-muted text-center"><?= $doc['citations_count'] ?></td>
            <td>
              <a href="<?= base_url('admin/documents/' . $doc['id']) ?>"
                 class="btn btn-sm btn-outline-secondary">
                <i class="bi bi-eye"></i>
              </a>
            </td>
          </tr>
        <?php endforeach; ?>
      <?php endif; ?>
    </tbody>
  </table>
</div>

<!-- Pagination -->
<?php
  $total   = $result['total'];
  $perPage = $result['perPage'];
  $page    = $result['page'];
  $pages   = (int) ceil($total / $perPage);
?>
<?php if ($pages > 1): ?>
  <nav class="mt-3">
    <ul class="pagination pagination-sm justify-content-center">
      <?php for ($i = 1; $i <= $pages; $i++): ?>
        <li class="page-item <?= $i === (int)$page ? 'active' : '' ?>">
          <a class="page-link" href="<?= current_url() . '?' . http_build_query(array_merge($filters, ['page' => $i])) ?>">
            <?= $i ?>
          </a>
        </li>
      <?php endfor; ?>
    </ul>
  </nav>
<?php endif; ?>

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