<?php
/**
 * Navigasi halaman.
 * @var array $p hasil Model::paginate()
 */
if (empty($p) || ($p['total_halaman'] ?? 1) <= 1 && ($p['total'] ?? 0) <= ($p['per_halaman'] ?? 50)) {
    if (!empty($p['total'])) {
        echo '<div class="d-flex justify-content-between align-items-center px-3 py-2 border-top small text-body-secondary">'
           . '<span>Menampilkan ' . nf($p['dari']) . '–' . nf($p['sampai']) . ' dari ' . nf($p['total']) . ' data</span></div>';
    }
    return;
}
$cur = (int)$p['halaman'];
$last = (int)$p['total_halaman'];
$win = 2;
$from = max(1, $cur - $win);
$to   = min($last, $cur + $win);
?>
<div class="d-flex flex-wrap justify-content-between align-items-center gap-2 px-3 py-2 border-top">
  <div class="small text-body-secondary">
    Menampilkan <strong><?= nf($p['dari']) ?>–<?= nf($p['sampai']) ?></strong> dari <strong><?= nf($p['total']) ?></strong> data
  </div>
  <div class="d-flex align-items-center gap-2">
    <label class="small text-body-secondary d-none d-md-inline" for="perHalaman">Baris</label>
    <select class="form-select form-select-sm" id="perHalaman" style="width:auto"
            onchange="window.location='<?= attr(urlCurrent(['hal' => 1], ['per'])) ?>' + (window.location.search.includes('?') ? '&' : '?') + 'per=' + this.value">
      <?php foreach ([25, 50, 100, 250, 500] as $n): ?>
        <option value="<?= $n ?>" <?= (int)$p['per_halaman'] === $n ? 'selected' : '' ?>><?= $n ?></option>
      <?php endforeach; ?>
    </select>
    <nav aria-label="Navigasi halaman">
      <ul class="pagination pagination-sm mb-0">
        <li class="page-item <?= $cur <= 1 ? 'disabled' : '' ?>">
          <a class="page-link" href="<?= attr(urlCurrent(['hal' => 1])) ?>" aria-label="Halaman pertama">&laquo;</a>
        </li>
        <li class="page-item <?= $cur <= 1 ? 'disabled' : '' ?>">
          <a class="page-link" href="<?= attr(urlCurrent(['hal' => max(1, $cur - 1)])) ?>">Sebelumnya</a>
        </li>
        <?php if ($from > 1): ?><li class="page-item disabled"><span class="page-link">…</span></li><?php endif; ?>
        <?php for ($i = $from; $i <= $to; $i++): ?>
          <li class="page-item <?= $i === $cur ? 'active' : '' ?>">
            <a class="page-link" href="<?= attr(urlCurrent(['hal' => $i])) ?>"><?= $i ?></a>
          </li>
        <?php endfor; ?>
        <?php if ($to < $last): ?><li class="page-item disabled"><span class="page-link">…</span></li><?php endif; ?>
        <li class="page-item <?= $cur >= $last ? 'disabled' : '' ?>">
          <a class="page-link" href="<?= attr(urlCurrent(['hal' => min($last, $cur + 1)])) ?>">Berikutnya</a>
        </li>
        <li class="page-item <?= $cur >= $last ? 'disabled' : '' ?>">
          <a class="page-link" href="<?= attr(urlCurrent(['hal' => $last])) ?>" aria-label="Halaman terakhir">&raquo;</a>
        </li>
      </ul>
    </nav>
  </div>
</div>
