<?php
class Pagination
{
    public $total = 0;
    public $page = 1;
    public $limit = 20;
    public $num_links = 10;
    public $url = '';
    public $text = '';

    public $is_app = false;

    public function render($json = false, $isVue = false)
    {
        if ($this->is_app) {
            $next = '<i class="material-icons">chevron_right</i>';
            $forward = '<i class="material-icons">fast_forward</i>';
            $backward = '<i class="material-icons">fast_rewind</i>';
            $back = '<i class="material-icons">chevron_left</i>';
        } else {
            $next = '<i class="fas fa-chevron-right"></i>';
            $forward = '<i class="fas fa-step-forward"></i>';
            $backward = '<i class="fas fa-step-backward"></i>';
            $back = '<i class="fas fa-chevron-left"></i>';
        }

        $total = $this->total;
        $page = $this->page < 1 ? 1 : $this->page;
        $limit = !(int)$this->limit ? 10 : $this->limit;
        $num_links = $this->num_links;
        $num_pages = ceil($total / $limit);

        //função para retonar o array de paginas em json que é usado na paginação por scroll
        if ($json) {
            $output = [];
            if ($num_pages > 0) {
                for ($i = 1; $i <= $num_pages; $i++) {
                    $isVue ? $output[] = $i : $output[] = str_replace('{page}', $i, $this->url);
                }
            }
            return $isVue ? $output : json_encode($output);
        }

        $output = '';

        if ($page > 1) {
            $output .= '<a href="' . str_replace('{page}', 1, $this->url) . '" class="first-page btn-default btn-small-square btn-white right-margin-x-small bottom-margin-x-small">' . $backward . '</a>';
            $output .= '<a href="' . str_replace('{page}', $page - 1, $this->url) . '" class="prev-page btn-default btn-small-square btn-white right-margin-x-small bottom-margin-x-small">' . $back . '</a> ';
        }

        if ($num_pages > 1) {
            if ($num_pages <= $num_links) {
                $start = 1;
                $end = $num_pages;
            } else {
                $start = $page - floor($num_links / 2);
                $end = $page + floor($num_links / 2);

                if ($start < 1) {
                    $end += abs($start) + 1;
                    $start = 1;
                }

                if ($end > $num_pages) {
                    $start -= ($end - $num_pages);
                    $end = $num_pages;
                }
            }

            // if ($start > 1) {
            // 	$output .= ' ... ';
            // }

            $output .= '<div class="flex flex-row">';
            for ($i = $start; $i <= $end; $i++) {
                if ($page == $i) {
                    $output .= '<button type="button" class="btn-default btn-small-square btn-primary right-margin-x-small bottom-margin-x-small"><span>' . $i . '</span></button>';
                } else {
                    $output .= '<a href="' . str_replace('{page}', $i, $this->url) . '" class="btn-default btn-small-square btn-white right-margin-x-small bottom-margin-x-small hidden-sm-down"><span>' . $i . '</span></a>';
                }
            }
            $output .= '</div>';

            // if ($end > $num_pages) {
            // 	$output .= ' ... ';
            // }
        }

        if ($page < $num_pages) {
            $output .= '<a href="' . str_replace('{page}', $page + 1, $this->url) . '" class="next-page btn-default btn-small-square btn-white right-margin-x-small bottom-margin-x-small">' . $next . '</a>';
            $output .= '<a href="' . str_replace('{page}', $num_pages, $this->url) . '" class="last-page btn-default btn-small-square btn-white right-margin-x-small bottom-margin-x-small">' . $forward . '</a>';

        }

        $find = array(
            '{start}',
            '{end}',
            '{total}',
            '{pages}'
        );

        $replace = array(
            ($total) ? (($page - 1) * $limit) + 1 : 0,
            ((($page - 1) * $limit) > ($total - $limit)) ? $total : ((($page - 1) * $limit) + $limit),
            $total,
            $num_pages
        );

        $render = '<div class="flex flex-row middle-xs center-xs between-sm">';
        $render .= $output ? '<div class="flex flex-row">' . $output . '</div>' : '';
        $render .= '<span class="text-small text-gray text-center vertical-padding-small">' . str_replace($find, $replace, $this->text) . '</span>';
        $render .= '</div>';


        return $render;
    }
}
