<?php

class Pager
{

    public $path;
    public $page = 1;
    public $perPage = 15;
    public $offset;
    public $totalPages;
    public $total;
    public $lastPage;
    public $prev;
    public $next;
    public $method = "default";
    public $ajax_function = null;
    public $alternative = false;
    public $params;

    public function __construct($page = null, $perPage = null){
        if ($page != null) {
            $this->page = $page;

        }elseif($_GET['page'] != null){
            $this->page = clear($_GET['page']);
        }

        if(isset($perPage)){
            $this->perPage = $perPage;
        }

        $this->offset = ($this->page * $this->perPage) - $this->perPage;
    }

    /**
     * @param $pages Array de dados estraídos para aparecer na página corrente;
     * @return string
     */
    public function paginate($pages = null)
    {
        if(is_array($pages)){
            $this->total = count($pages);
        }
        if($this->total <= $this->perPage){
            return false;
        }

        $this->totalPages = $this->total / $this->perPage;

        $this->prev = $this->page - 1;
        $this->next = $this->page + 1;

        $this->totalPages = ceil($this->totalPages);

        if(!empty($this->params)){
            $this->params .= '&';
        }

        //criando link anterior
        if ($this->page > 1) {
            if ($this->method == "ajax") {
                $prev_link = "<a href=\"#\" onclick = \"$this->ajax_function('$this->path', $this->prev); return false;\"> <span>&lsaquo;</span> anterior </a>";
                $first_link = "<a href=\"#\" onclick = \"$this->ajax_function('$this->path', 1); return false;\"> <span>&lsaquo;&lsaquo;</span> </a>";
            } else {
                $prev_link = "<a href=\"$this->path?{$this->params}page=$this->prev\"> <span>&lsaquo;</span> anterior </a>";
                $first_link = "<a href=\"$this->path?{$this->params}page=1\"> <span>Primeiro</span> </a>";
            }
        } else {
            $prev_link = "";
        }

        //Criando link próximo
        if ($this->totalPages > $this->page) {
            if ($this->method == "ajax") {
                $next_link = "<a href=\"#\" onclick = \"$this->ajax_function('$this->path', $this->next); return false;\"> pr&oacute;ximo <span>&rsaquo;</span> </a>";
                $last_link = "<a href=\"#\" onclick = \"$this->ajax_function('$this->path', $this->totalPages); return false;\"> <span>&rsaquo;&rsaquo;</span> </a>";
            } else {
                $next_link = "<a href=\"$this->path?{$this->params}page=$this->next\"> pr&oacute;ximo <span>&rsaquo;</span> </a> ";
                $last_link = "<a href=\"$this->path?{$this->params}page=$this->totalPages\"> <span>... Último</span> </a> ";
            }
        } else {
            $next_link = "";
        }

        $this->lastPage = $this->path . "?page={$this->totalPages}";

        $max = $this->page + 3;

        if ($max >= $this->totalPages) {
            $max = $this->totalPages;
        }

        if ($this->page >= 6) {
            $value = $this->page;
        } else {
            $value = 1;
        }

        $panel = "";
        //criando o painel de navegação
        for ($x = $value; $x <= $max; $x++) {
            if ($x == $this->page) {
                $panel .= "<li class='active'><a href=\"javascript:void(0)\" class=\"current-page\"> $x</a></li> ";
            } else {
                if ($this->method == "ajax") {
                    $panel .= "<li><a href=\"#\" onclick = \"$this->ajax_function('$this->path', $x); return false;\"> $x </a>";
                } else {
                    $panel .= "<li> <a href=\"$this->path?{$this->params}page=$x\">$x</a>";
                }
            }
        }

        if($this->alternative){
            return $this->renderAlternative($panel, $first_link, $last_link);
        }else{
            return $this->render($panel, $first_link, $prev_link, $next_link, $last_link);
        }
    }

    public function render($panel, $first_link, $prev_link, $next_link, $last_link){
        return <<<PAG
            <ul>
                <li class="contador"> <a href="#"> Total: {$this->total} registros - Página: {$this->page} de {$this->totalPages}</a></li>
                <li class="first"> {$first_link}</li>
                <li class="ant"> {$prev_link}</li>
                {$panel} </li>
                <li class="prox"> {$next_link}  </li>
                <li class="last"> {$last_link}  </li>
            </ul>
PAG;
    }

    public function renderAlternative($panel, $first_link, $last_link){
        return <<<PAG
            <span class="bt-primeiro">{$first_link}</span>
            <ul>
                {$panel} </li>
            </ul>
            <span class="bt-ultimo">{$last_link}</span>
PAG;
    }

}