<?php

class Pager {

	public $each_disNums; //每页显示的条目数 
	public $nums; //总条目数 
	public $current_page; //当前被选中的页 
	public $sub_pages; //每次显示的页数 
	public $pageNums; //总页数 
	public $page_array = array(); //用来构造分页的数组 
	public $subPage_link; //每个分页的链接 
	public $langs = array(); // 语言包

	/**
	 *
	 * __construct是SubPages的构造函数，用来在创建类的时候自动运行.
	 * @$each_disNums   每页显示的条目数
	 * @nums     总条目数
	 * @current_num     当前被选中的页
	 * @sub_pages       每次显示的页数
	 * @subPage_link    每个分页的链接
	 * @subPage_type    显示分页的类型
	 *
	 * 当@subPage_type=1的时候为普通分页模式
	 *    example：   共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [下页] [尾页]
	 *    当@subPage_type=2的时候为经典分页样式
	 *     example：   当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页]
	 */
	function __construct() {
		// $this->each_disNums = intval($each_disNums);
		// $this->nums = intval($nums);
		// if (!$current_page) {
		// 	$this->current_page = 1;
		// } else {
		// 	$this->current_page = intval($current_page);
		// }
		// $this->sub_pages = intval($sub_pages);
		// $this->pageNums = ceil($nums / $each_disNums);
		// $this->subPage_link = "?page=";
		$this->each_disNums = 20;
		$this->subPage_type = 2;
		$this->sub_pages = 10;
	}

	public function init($argv = array()) {
		isset($argv['each_disNums']) &&  $this->each_disNums = (int)$argv['each_disNums'];
		isset($argv['nums']) && $this->nums = (int)$argv['nums'];
		isset($argv['current_page']) && $this->current_page = (int)$argv['current_page'];
		isset($argv['sub_pages']) && $this->sub_pages = (int)$argv['sub_pages'];
		isset($argv['subPage_link']) && $this->subPage_link = $argv['subPage_link'];
		!isset($argv['lang']) && $argv['lang'] = 'zh';
		$this->langs = $this->getLangs($argv['lang']);
		if (!$this->current_page) {
			$this->current_page = 1;
		} 
		// $this->pageNums = ceil($this->nums / $this->each_disNums);
	}

	/*
	  用来给建立分页的数组初始化的函数。
	 */

	function initArray() {
		for ($i = 0; $i < $this->sub_pages; $i++) {
			$this->page_array[$i] = $i;
		}
		return $this->page_array;
	}

	/*
	  construct_num_Page该函数使用来构造显示的条目
	  即使：[1][2][3][4][5][6][7][8][9][10]
	 */

	function construct_num_Page() {
		if ($this->pageNums < $this->sub_pages) {
			$current_array = array();
			for ($i = 0; $i < $this->pageNums; $i++) {
				$current_array[$i] = $i + 1;
			}
		} else {
			$current_array = $this->initArray();
			if ($this->current_page <= 3) {
				for ($i = 0; $i < count($current_array); $i++) {
					$current_array[$i] = $i + 1;
				}
			} elseif ($this->current_page <= $this->pageNums && $this->current_page > $this->pageNums - $this->sub_pages + 1) {
				for ($i = 0; $i < count($current_array); $i++) {
					$current_array[$i] = ($this->pageNums) - ($this->sub_pages) + 1 + $i;
				}
			} else {
				for ($i = 0; $i < count($current_array); $i++) {
					$current_array[$i] = $this->current_page - 2 + $i;
				}
			}
		}

		return $current_array;
	}


	/**
	 * @$each_disNums   每页显示的条目数
	 * @nums     总条目数
	 * @current_num     当前被选中的页
	 * @sub_pages       每次显示的页数
	 * @subPage_link    每个分页的链接
	 * @subPage_type    显示分页的类型
	 *
	 * 当@subPage_type=1的时候为普通分页模式
	 *    example：   共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [下页] [尾页]
	 *    当@subPage_type=2的时候为经典分页样式
	 *     example：   当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页]
	 */
	public function show($current_page, $nums, $subPage_link = '?page=') {

		$this->nums = intval($nums);
		if (!$current_page) {
			$this->current_page = 1;
		} else {
			$this->current_page = intval($current_page);
		}
		$this->pageNums = ceil($nums / $this->each_disNums);
		$this->subPage_link = $subPage_link;

		$subPageCss2Str = "";
		// $subPageCss2Str .= "<li class='disabled'><span>当前第" . $this->current_page . "/" . $this->pageNums . "页</span></li>";
		$subPageCss2Str .= sprintf($this->langs['pinfo'], $this->current_page, $this->pageNums);

		if ($this->current_page > 1) {
			$firstPageUrl = $this->subPage_link . "1";
			$prewPageUrl = $this->subPage_link . ($this->current_page - 1);
			// $subPageCss2Str .= "<li><a href='$firstPageUrl'>首页</a></li>";
			// $subPageCss2Str .= "<li><a href='$prewPageUrl'>上一页</a></li>";
			$subPageCss2Str .= sprintf($this->langs['firstPageLink'], $firstPageUrl);
			$subPageCss2Str .= sprintf($this->langs['prevPageLink'], $prewPageUrl);

		} else {
			// $subPageCss2Str .= "<li class='disabled'><span>首页</span></li>";
			// $subPageCss2Str .= "<li class='disabled'><span>上一页</span></li>";
			$subPageCss2Str .= $this->langs['firstPageDisabled'];
			$subPageCss2Str .= $this->langs['prevPageDisabled'];
		}

		$a = $this->construct_num_Page();
		for ($i = 0; $i < count($a); $i++) {
			$s = $a[$i];
			if ($s == $this->current_page) {
				// $subPageCss2Str .= "<li class='active'><span>" . $s . "</span></li>";
				$subPageCss2Str .= sprintf($this->langs['active'], $s);
			} else {
				$url = $this->subPage_link . $s;
				// $subPageCss2Str .= "<li><a href='$url'>" . $s . "</a></li>";
				$subPageCss2Str .= sprintf($this->langs['numLink'], $url, $s);
			}
		}

		if ($this->current_page < $this->pageNums) {
			$lastPageUrl = $this->subPage_link . $this->pageNums;
			$nextPageUrl = $this->subPage_link . ($this->current_page + 1);
			$subPageCss2Str .= sprintf($this->langs['nextPageLink'], $nextPageUrl);
			$subPageCss2Str .= sprintf($this->langs['lastPageLink'], $lastPageUrl);
		} else {
			$subPageCss2Str .= $this->langs['nextPageDisabled'];
			$subPageCss2Str .= $this->langs['lastPageDisabled'];
		}
		return $subPageCss2Str;
	}

	/**
	 * 语言包
	 * @param string $langtype
	 * @return array
	 */
	public function getLangs($langtype = 'zh') {
		$lang = array();
		$lang['zh'] = array(
			'pinfo' => "<li class='disabled'><span>当前第%s/%s页</span></li>",
			'firstPageLink' => "<li><a href='%s'>首页</a></li>",
			'prevPageLink' => "<li><a href='%s'>上一页</a></li>",
			'firstPageDisabled' => "<li class='disabled'><span>首页</span></li>",
			'prevPageDisabled' => "<li class='disabled'><span>上一页</span></li>",
			'active' => "<li class='active'><span>%s</span></li>",
			'numLink' => "<li><a href='%s'>%s</a></li>",
			'nextPageLink' =>  " <li><a href='%s'>下一页</a></li>",
			'lastPageLink' =>  "<li><a href='%s'>尾页</a></li>",
			'nextPageDisabled' =>  "<li class='disabled'><span>下一页</span></li>",
			'lastPageDisabled' =>  "<li class='disabled'><span>尾页</span></li>",
		);
		$lang['en'] = array(
			'pinfo' => "<li class='disabled'><span>Page %s/%s</span></li>",
			'firstPageLink' => "<li><a href='%s'>First</a></li>",
			'prevPageLink' => "<li><a href='%s'>Prev</a></li>",
			'firstPageDisabled' => "<li class='disabled'><span>First</span></li>",
			'prevPageDisabled' => "<li class='disabled'><span>Prev</span></li>",
			'active' => "<li class='active'><span>%s</span></li>",
			'numLink' => "<li><a href='%s'>%s</a></li>",
			'nextPageLink' =>  " <li><a href='%s'>Next</a></li>",
			'lastPageLink' =>  "<li><a href='%s'>Last</a></li>",
			'nextPageDisabled' =>  "<li class='disabled'><span>Next</span></li>",
			'lastPageDisabled' =>  "<li class='disabled'><span>Last</span></li>",
		);
		return isset($lang[$langtype]) ? $lang[$langtype] : $lang['en'];
	}

	/**
	 * 生成查询链接
	 * @param string $base
	 * @param array $param
	 * @return string
	 */
	public function buildUrl($base, $param = array()) {
		return $base . '?' . http_build_query($param) . '&page=';
	}

}
