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

<!-- Search bar -->
<div class="row mb-4">
  <div class="col-12">
    <h1 class="section-title mb-1">Repositori Digital</h1>
    <p class="text-muted small mb-3">Karya akademik STT El-Shadday — skripsi, jurnal, dan prosiding</p>
    <form method="get" action="<?= base_url('/') ?>" class="d-flex gap-2">
      <input type="text" name="q" class="form-control"
             placeholder="Cari judul, penulis, kata kunci..."
             value="<?= esc($filters['q'] ?? '') ?>">
      <button type="submit" class="btn btn-primary px-4">
        <i class="bi bi-search"></i>
      </button>
    </form>
  </div>
</div>

<div class="row g-4">
  <!-- Filters sidebar -->
  <div class="col-lg-3">
    <form method="get" action="<?= base_url('/') ?>" id="filterForm">
      <?php if (!empty($filters['q'])): ?>
        <input type="hidden" name="q" value="<?= esc($filters['q']) ?>">
      <?php endif; ?>

      <div class="card-stt p-3 mb-3">
        <p class="fw-semibold small text-muted mb-2 text-uppercase" style="letter-spacing:.5px">Jenis Dokumen</p>
        <?php foreach ($types as $t): ?>
          <div class="form-check">
            <input class="form-check-input" type="radio" name="type"
                   id="type_<?= $t['id'] ?>" value="<?= $t['id'] ?>"
                   <?= (string)($filters['type'] ?? '') === (string)$t['id'] ? 'checked' : '' ?>
                   onchange="document.getElementById('filterForm').submit()">
            <label class="form-check-label small" for="type_<?= $t['id'] ?>">
              <?= esc($t['name']) ?>
            </label>
          </div>
        <?php endforeach; ?>
        <?php if (!empty($filters['type'])): ?>
          <a href="<?= base_url('?q=' . urlencode($filters['q'] ?? '')) ?>" class="small text-muted mt-1 d-block">
            <i class="bi bi-x-circle me-1"></i>Hapus filter
          </a>
        <?php endif; ?>
      </div>

      <div class="card-stt p-3 mb-3">
        <p class="fw-semibold small text-muted mb-2 text-uppercase" style="letter-spacing:.5px">Program Studi</p>
        <select name="program" class="form-select form-select-sm"
                onchange="document.getElementById('filterForm').submit()">
          <option value="">Semua Prodi</option>
          <?php foreach ($programs as $p): ?>
            <option value="<?= $p['id'] ?>" <?= (string)($filters['program'] ?? '') === (string)$p['id'] ? 'selected' : '' ?>>
              <?= esc($p['name']) ?>
            </option>
          <?php endforeach; ?>
        </select>
      </div>

      <div class="card-stt p-3 mb-3">
        <p class="fw-semibold small text-muted mb-2 text-uppercase" style="letter-spacing:.5px">Tahun</p>
        <select name="year" class="form-select form-select-sm"
                onchange="document.getElementById('filterForm').submit()">
          <option value="">Semua Tahun</option>
          <?php foreach ($years as $y): ?>
            <option value="<?= $y ?>" <?= ($filters['year'] ?? '') == $y ? 'selected' : '' ?>><?= $y ?></option>
          <?php endforeach; ?>
        </select>
      </div>

      <div class="card-stt p-3">
        <p class="fw-semibold small text-muted mb-2 text-uppercase" style="letter-spacing:.5px">Urutkan</p>
        <select name="sort" class="form-select form-select-sm"
                onchange="document.getElementById('filterForm').submit()">
          <option value="latest"  <?= ($filters['sort'] ?? 'latest') === 'latest'  ? 'selected' : '' ?>>Terbaru</option>
          <option value="cited"   <?= ($filters['sort'] ?? '') === 'cited'   ? 'selected' : '' ?>>Paling Dikutip</option>
          <option value="popular" <?= ($filters['sort'] ?? '') === 'popular' ? 'selected' : '' ?>>Paling Dilihat</option>
          <option value="az"      <?= ($filters['sort'] ?? '') === 'az'      ? 'selected' : '' ?>>A – Z</option>
        </select>
      </div>
    </form>
  </div>

  <!-- Document grid -->
  <div class="col-lg-9">
    <div class="d-flex justify-content-between align-items-center mb-3">
      <span class="text-muted small">
        <?= number_format($result['total']) ?> dokumen ditemukan
        <?php if (!empty($filters['q'])): ?>
          untuk "<strong><?= esc($filters['q']) ?></strong>"
        <?php endif; ?>
      </span>
    </div>

    <?php if (empty($result['data'])): ?>
      <div class="text-center py-5 text-muted">
        <i class="bi bi-search fs-1 d-block mb-3 opacity-25"></i>
        <p>Tidak ada dokumen yang ditemukan.</p>
      </div>
    <?php else: ?>
      <div class="row row-cols-1 row-cols-md-2 row-cols-xl-3 g-3">
        <?php foreach ($result['data'] as $doc): ?>
          <div class="col">
            <a href="<?= base_url('documents/' . $doc['id']) ?>" class="text-decoration-none">
              <div class="doc-card p-0 overflow-hidden">
                <?php if (!empty($doc['cover_img'])): ?>
                  <img src="<?= base_url('covers/' . esc($doc['cover_img'])) ?>"
                       alt="Cover" class="doc-cover">
                <?php else: ?>
                  <div class="doc-cover-placeholder">
                    <i class="bi bi-file-earmark-text"></i>
                  </div>
                <?php endif; ?>
                <div class="p-3">
                  <div class="mb-1">
                    <span class="doc-type-badge"><?= esc($doc['type_name']) ?></span>
                  </div>
                  <h6 class="mb-1 text-dark fw-semibold" style="font-size:.9rem;line-height:1.3;">
                    <?= esc(character_limiter($doc['title'], 80)) ?>
                  </h6>
                  <p class="text-muted small mb-1"><?= esc($doc['author_name']) ?> · <?= esc($doc['year']) ?></p>
                  <?php if (!empty($doc['program_name'])): ?>
                    <p class="text-muted small mb-0"><i class="bi bi-mortarboard me-1"></i><?= esc($doc['program_name']) ?></p>
                  <?php endif; ?>
                  <div class="d-flex gap-3 mt-2 text-muted" style="font-size:.75rem;">
                    <span><i class="bi bi-eye me-1"></i><?= number_format($doc['view_count']) ?></span>
                    <span><i class="bi bi-download me-1"></i><?= number_format($doc['download_count']) ?></span>
                    <?php if ($doc['citations_count'] > 0): ?>
                      <span><i class="bi bi-quote me-1"></i><?= $doc['citations_count'] ?> dikutip</span>
                    <?php endif; ?>
                  </div>
                </div>
              </div>
            </a>
          </div>
        <?php endforeach; ?>
      </div>

      <!-- Pagination -->
      <?php
        $total   = $result['total'];
        $perPage = $result['perPage'];
        $page    = $result['page'];
        $pages   = (int) ceil($total / $perPage);
        $baseUrl = current_url() . '?' . http_build_query(array_filter(array_merge($filters, ['page' => ''])));
      ?>
      <?php if ($pages > 1): ?>
        <nav class="mt-4">
          <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_filter(array_merge($filters, ['page' => $i]))) ?>">
                  <?= $i ?>
                </a>
              </li>
            <?php endfor; ?>
          </ul>
        </nav>
      <?php endif; ?>
    <?php endif; ?>
  </div>
</div>

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