<?php
use Icinga\Web\Url;

if ($this->pageCount <= 1) return;

$fromto = $this->translate('%d to %d of %d');
$total = $this->totalItemCount;
$limit = $this->itemCountPerPage;
$title_prev = sprintf(
    $fromto,
    ($this->current - 2) * $limit + 1,
    ($this->current - 1) * $limit,
    $total
);

$title_next = sprintf(
    $fromto,
    ($this->current) * $limit + 1,
    ($this->current + 1) * $limit,
    $total
);

?>
<ul class="pagination pagination-sm">
  <!-- Previous page link -->

<?php if (isset($this->previous)): ?>
  <li>
    <a href="<?= Url::fromRequest()->overwriteParams(array('page' => $this->previous))->getAbsoluteUrl() ?>"
         title="<?= $title_prev ?>">« <?= $this->translate('Prev') ?>
    </a>
  </li>
<?php else: ?>
  <li class="disabled"><span>« <?= $this->translate('Prev') ?></span></li>
<?php endif ?>

    <!-- Numbered page links -->
<?php
foreach ($this->pagesInRange as $page):

    $start = ($page - 1) * $limit + 1;
    $end = $page * $limit;
    if ($end > $total) {
        $end = $total;
    }
    $title = sprintf($fromto, $start, $end, $total);
    $class = $page === $this->current ? ' class="active"' : '';

    if ($page === '...'): ?>
    <li class="disabled"><span>...</span></li>
<?php else: ?>
    <li <?= $class ?>>
      <a href="<?= Url::fromRequest()->overwriteParams(array('page' => $page))->getAbsoluteUrl() ?>" title="<?= $title ?>">
        <?= $page ?>
      </a>
    </li>
<?php
    endif;
endforeach;
?>
<!-- Next page link -->
<? if (isset($this->next)): ?>
  <li>
    <a href="<?= Url::fromRequest()->overwriteParams(array('page' => $this->next))->getAbsoluteUrl() ?>"
         title="<?= $title_next ?>"><?= t('Next') ?> »
    </a>
  </li>
<? else: ?>
  <li class="disabled"><span><?= t('Next') ?> »</span></li>
<? endif ?>
</ul>
