<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Pagination {
    public $back_btn;
    public $next_btn;
    public $next_page;
    public $prev_page;
    public $pageURL;
    public $pageno;
    public $total;

    private $records;
    private $pageSegment;
    private $offset;

    function __construct($records = 10, $pageSegment, $pageURL)
    {
        $this->records      = $records;
        $this->pageSegment  = $pageSegment;
        $this->pageURL      = $pageURL;

        $this->setPageNo();
        $this->setOffset();
    }

    private function setPageNo()
    {
        $this->pageno = ($this->pageSegment != null && is_numeric($this->pageSegment)) ? $this->pageSegment : 1;
    }

    private function setOffset()
    {
        $this->offset = ($this->pageno-1) * $this->records;
    }

    function setTotal($total)
    {
        $this->total = $total;
    }

    function getOffset()
    {
        return $this->offset;
    }

    function setButtons($isEmpty = true)
    {
        $next_page  = $this->pageno <= 5 ? 10 : $this->pageno + 5;
        $next_page  = $next_page >= $this->total ? $this->total : $next_page;
        $prev_page  = $this->pageno <= 6 ? 1 : $this->pageno - 5;
        $prev_page  = $next_page >= $this->total ? $this->total - 10 : $prev_page;
        $prev_page  = $prev_page <= 0 ? $prev_page = 1 : $prev_page;

        $this->back_btn     = $this->pageno <= 1 ? '#' : $this->pageURL.'/'.($this->pageno - 1);
        $this->next_btn     = $this->pageno >= $this->total ? '#' : $this->pageURL.'/'.($this->pageno + 1);
        $this->first_btn    = $this->pageURL.'/1';
        $this->total_btn    = $isEmpty ? $this->pageURL.'/1' : $this->pageURL.'/'.$this->total;
        $this->next_page    = $next_page;
        $this->prev_page    = $prev_page;
    }
}
?>
