<?php
class PaginationJson {
	public $total = 0;
	public $page = 1;
	public $limit = 20;
	public $num_links = 10;
	public $text = 'Showing {start} to {end} of {total} ({pages} Pages)';
	public $text_first = '<i class="fas fa-step-backward"></i>';
	public $text_last = '<i class="fas fa-step-forward"></i>';
	public $text_next = '<i class="fas fa-chevron-right"></i>';
	public $text_prev = '<i class="fas fa-chevron-left"></i>';
	public $style_links = '';
	public $style_results = '';
	private $defaultClassJs = ' btn-default btn-small-square btn-white right-margin-x-small bottom-margin-x-small';
	public $classjs = '';


    public function render() {

        $this->classjs .= $this->defaultClassJs;

		$total = $this->total;

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

		if (!(int)$this->limit) {
			$limit = 10;
		} else {
			$limit = $this->limit;
		}

		$num_links = $this->num_links;
		$num_pages = ceil($total / $limit);

		$output = '';

		if ($page > 1) {
            $output .= '<a class="first-page '.$this->classjs.'" data-page="1">' . $this->text_first . '</a>';
            $output .= '<a class="prev-page '.$this->classjs.'" data-page="'.($page-1).'">' . $this->text_prev . '</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;
				}
			}


			$output .= '<div class="flex flex-row">';
			for ($i = $start; $i <= $end; $i++) {
				if ($page == $i) {
					$output .= '<span class="btn-default btn-small-square btn-primary right-margin-x-small bottom-margin-x-small">' . $i . '</span> ';
				} elseif ( ( ($i==$page-1) || ($i==$page+1) ) && ($page > 1) ){
					$output .= ' <a class="'.$this->classjs.'" data-page="'.$i.'"><span>' . $i . '</span></a> ';
				} elseif( ($page==1) && ($i<5) ){
					$output .= ' <a class="'.$this->classjs.'" data-page="'.$i.'"><span>' . $i . '</span></a> ';
				}else{
					$output .= ' <a class="'.$this->classjs.' hidden-sm-down" data-page="'.$i.'"><span>' . $i . '</span></a> ';
				}
			}
			$output .= '</div>';

		}

   	    if ($page < $num_pages) {
            $output .= '<a class="next-page '.$this->classjs.'" data-page="'.($page+1).'">' . $this->text_next . '</a>';
            $output .= '<a class="last-page '.$this->classjs.'" data-page="'.$num_pages.'">' . $this->text_last . '</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;
	}
}
