<?php

$param = $option['param'];

$pass = '';

if ($param) {
    foreach ($param as $key => $value) {
        $pass .= "$value/";
    }
}

$counter   = (int) $option['counter'];
$limit     = (int) $option['limit'];
$totalrow  = (int) $option['totalrow'];
$last_page = $totalrow / $limit;
$type      = (int) $option['type'];
$splitpage = ceil($totalrow / $limit);

$html = '';

$iof       = floor($counter / $limit) * $limit;
$next      = $counter + 1;
$totalrow2 = ceil($totalrow / $limit) - 1;
$limitone  = $iof + $limit;

$splitpage = ceil($totalrow / $limit);


if ($splitpage > $limit) {
    if ($limitone > $splitpage) {
        $limitof = $splitpage;
    } else {
        $limitof = $limitone;
    }
} else {
    $limitof = $splitpage;
}

if ($counter) {
    $limitof++;
    $iof--;
}

$lastpage        = $totalrow2 + 1;
$section_page    = 20;
$section         = ceil($lastpage / $section_page);
$current_page    = $counter;
$current_section = ceil($current_page / $section_page);

if (!$current_section) {
    $current_section = 1;
}
$final_page = $current_section * $section_page;
$start_page = $final_page - 20;

if ($current_section == $section) {
    $final_page = $lastpage;
}

if (!$counter) {
    $start_page = 0;
}

//echo $section.'-'.$current_section.'-'.$start_page.'-'.$final_page;
//return;
$prevPage = $final_page - 1;

if ($current_section == 1) {
    $url = '#';
} else {
    $url = $option['paginate_url'] . '/' . $start_page . '/' . $pass;
}
$iof       = 0;
$splitpage = ceil($totalrow / $limit);
$html .= '<li class="page-item"><a class="page-link" href="' . $url . '" aria-label="Previous"><span aria-hidden="true">«</span><span class="sr-only">Previous</span></a></li>';
for ($i = $start_page,$i1 = 0;$i <= $final_page; $i++,$i1++) {
    $pages = $i + 1;

    if ($pages && $i < $lastpage) {
        $url = $option['paginate_url'] . '/' . $i . '/' . $pass;

        if ($i != $counter) {
            $html .= "<li class='page-item '><a href='" . $url . "' class='page-link' onclick='return paginate(" . $i . ")'  >" . $pages . '</a></li>';
        } else {
            $html .= "<li class='page-item active'><a href='#'  class='page-link selected'>" . $pages . '</a></li>';
        }
    }
}//end of for
$nextPage = $final_page + 1;

if ($nextPage < $lastpage) {
    $url = $option['paginate_url'] . '/' . $nextPage . '/' . $pass;
} else {
    $url = '#';
}
$html .= '<li class="page-item"><a class="page-link" href="' . $url . '" aria-label="Next"><span aria-hidden="true">»</span><span class="sr-only">Next</span></a></li>';
echo $html;
