<?php
class Page
{
	var $page_str  = false;
	var $page_cfg  = array();
	var $page_lang = array();
	var $page_size = 20;
	function __construct($page_lang=array())
	{
		//parent::__construct();
		if(isset($_REQUEST["page_num"]) && $_REQUEST["page_num"] > 1 ){
			$this -> page_cfg['page_num'] = intval($_REQUEST["page_num"]);
		}else{
			$this -> page_cfg['page_num'] = 1;	
		}
		$this -> get_limit_num( $this -> page_cfg['page_num'] );
		$this -> page_cfg['page_size'] = 20 ;
		$this -> page_cfg['limit_num'] = 0 ;
		if(!empty($page_lang)){
			$this -> page_lang = $page_lang;
		}else{
			$this -> page_lang['txt_total']  	= 'All:';
			$this -> page_lang['txt_result']  	= 'Record';
			$this -> page_lang['txt_at']  		= 'At';
			$this -> page_lang['txt_page']  	= 'Page';
			$this -> page_lang['txt_go_page']  	= 'Go';
		}
		//var_dump($this -> page_lang);
	}
	
	function get_limit_num( $page_num = 1 )
	{	
		if( isset($this->page_num) &&  $this->page_num > 1 )
		{
			$this -> page_cfg['page_num']  = $page_num ;
			$this -> page_cfg['limit_num'] = ceil( ( $this -> page_cfg['page_num'] -1 ) * $this -> page_cfg['page_size'] );
		}
	}

	function set_page_size( $size  = 20 )
	{
		$this -> page_cfg['page_size'] = $size;	
		$this -> page_cfg['limit_num'] = ceil( ( $this -> page_cfg['page_num'] -1 ) * $this -> page_cfg['page_size'] );
	}

	function get_limit_val()
	{
		return $this -> page_cfg['limit_num']." , ".$this -> page_cfg['page_size'];
	}
	//当前页码，总记录数，每次显示条数
	function htmlpage_str( $url = '',  $result_count='', $endArray = array() )
	{
		$page_count = ceil($result_count / $this -> page_cfg['page_size']);
		$numBegin = 1;
		$numEnd   = 0 ;
		
		if( ( $this -> page_cfg['page_num']  <= ($page_count - 3) ) and  ( $page_count > 3 ) and  ($this -> page_cfg['page_num']  <= $page_count ) )
		{
			$numEnd = $this -> page_cfg['page_num'] + 3 ;
		}else{
			$numEnd = $page_count;
		}
		
		if( ( $numBegin  <= ($this -> page_cfg['page_num'] -3) ) and (($this -> page_cfg['page_num'] -3) > 0 ) )
		{
			$numBegin = $this -> page_cfg['page_num'] - 3;
		}else{ 
			$numBegin = $numBegin;
		}
		$endurl = '';
		
		
		foreach((array)$endArray as $key => $val)
		{
			$endurl .= '&'.$key.'='.$val;
		}
		
		$this -> page_str  = " <div align='right' id='pagediv'>  ";
				
		if($this -> page_cfg['page_num'] > 4)
		{
			$this -> page_str .= '<a href="'.$url.'-1'.$endurl.'.html"  class="page_first" >第一页</a>';
		}
		
		for($numBegin; $numBegin <= $numEnd;  $numBegin++)
		{
			if($numBegin==$this -> page_cfg['page_num'])
			{
				if($page_count ==1 and $this -> page_cfg['page_num']==1)
				{
					//单页不显示一页
				}else{
					$this -> page_str .= '<b>'.$numBegin.'</b>&nbsp;';
				}
			}else{
				$this -> page_str .= '<a href="'.$url.'-'.$numBegin.$endurl.'.html" title="第'.$numBegin.'页" >'.$numBegin.'</a>&nbsp;';
			}
		}
		if($this -> page_cfg['page_num'] < ($page_count -3) )
		{
			$this -> page_str .= '<a href="'.$url.'-'.$page_count.$endurl.'"  class="page_end" >最末页</a>';
		}

		$this -> page_str .=  "&nbsp;&nbsp;</div>";
		//显示隐藏数据
		$this -> page_str .= '<input type="hidden" name="PGURL" id="PGURL" value="'.$url.'"  />';

		return $this -> page_str;
		
	}


	//当前页码，总记录数，每次显示条数
	function buildpage_str( $url = '', $result_count='', $endArray = array() )
	{
		$page_count = ceil($result_count / $this -> page_cfg['page_size']);
		$numBegin = 1;
		$numEnd   = 0 ;
		//var_dump( $endArray );

		if( ( $this -> page_cfg['page_num']  <= ($page_count - 3) ) and  ( $page_count > 3 ) and  ($this -> page_cfg['page_num']  <= $page_count ) )
		{
			$numEnd = $this -> page_cfg['page_num'] + 3 ;
		}else{
			$numEnd = $page_count;
		}
		
		if( ( $numBegin  <= ($this -> page_cfg['page_num'] -3) ) and (($this -> page_cfg['page_num'] -3) > 0 ) )
		{
			$numBegin = $this -> page_cfg['page_num'] - 3;
		}else{ 
			$numBegin = $numBegin;
		}
		$endurl = '';
		
		

		
		$this -> page_str  = " <div align='right' id='pagediv'>  ";
		
		if( $GLOBALS["WebConfig"]["webhtml"] == 1 )
		{
				//'.$key.'-
				foreach((array)$endArray as $key => $val)
				{
					if($val!='') $endurl .= '-'.$val;  
				}
			
				if($this -> page_cfg['page_num'] > 4)
				{
					$this -> page_str .= '<a href="'.$url.'-1'.$endurl.'.html"  class="page_first" >第一页</a>';
				}
				
				for($numBegin; $numBegin <= $numEnd;  $numBegin++)
				{
					if($numBegin==$this -> page_cfg['page_num'])
					{
						if($page_count ==1 and $this -> page_cfg['page_num']==1)
						{
							//单页不显示一页
						}else{
							$this -> page_str .= '<b>'.$numBegin.'</b>&nbsp;';
						}
					}else{
						$this -> page_str .= '<a href="'.$url.'-'.$numBegin.$endurl.'.html" title="第'.$numBegin.'页" >'.$numBegin.'</a>&nbsp;';
					}
				}
				if($this -> page_cfg['page_num'] < ($page_count -3) )
				{
					$this -> page_str .= '<a href="'.$url.'-'.$page_count.$endurl.'.html"  class="page_end" >最末页</a>';
				}
				
		}else{
		
			foreach((array)$endArray as $key => $val)
			{
				$endurl .= '&'.$key.'='.$val;
			}
				
			if($this -> page_cfg['page_num'] > 4)
			{
				$this -> page_str .= '<a href="'.$url.'&page=1'.$endurl.'"  class="page_first" >第一页</a>';
			}
			
			for($numBegin; $numBegin <= $numEnd;  $numBegin++)
			{
				if($numBegin==$this -> page_cfg['page_num'])
				{
					if($page_count ==1 and $this -> page_cfg['page_num']==1)
					{
						//单页不显示一页
					}else{
						$this -> page_str .= '<b>'.$numBegin.'</b>&nbsp;';
					}
				}else{
					$this -> page_str .= '<a href="'.$url.'&page='.$numBegin.$endurl.'" title="第'.$numBegin.'页" >'.$numBegin.'</a>&nbsp;';
				}
			}
			if($this -> page_cfg['page_num'] < ($page_count -3) )
			{
				$this -> page_str .= '<a href="'.$url.'&page='.$page_count.$endurl.'"  class="page_end" >最末页</a>';
			}

			}
		
		$this -> page_str .=  "&nbsp;&nbsp;</div>";
		//显示隐藏数据
		$this -> page_str .= '<input type="hidden" name="PGURL" id="PGURL" value="'.$url.'"  />';
		
		
		return $this -> page_str;
		
	}
	
	//当前页码，总记录数，每次显示条数
	function newadminpage_str( $url = '', $result_count='', $endArray = array() )
	{
		$page_count = ceil($result_count / $this -> page_cfg['page_size']);
		$numBegin = 1;
		$numEnd   = 0 ;
		




		if( ( $this -> page_cfg['page_num']  <= ($page_count - 3) ) and  ( $page_count > 3 ) and  ($this -> page_cfg['page_num']  <= $page_count ) )
		{
			$numEnd = $this -> page_cfg['page_num'] + 3 ;
		}else{
			$numEnd = $page_count;
		}
		
		if( ( $numBegin  <= ($this -> page_cfg['page_num'] -3) ) and (($this -> page_cfg['page_num'] -3) > 0 ) )
		{
			$numBegin = $this -> page_cfg['page_num'] - 3;
		}else{ 
			$numBegin = $numBegin;
		}
		$endurl = '';
		
		

		
		$this -> page_str  = " <div align='right' id='pagediv'>  ";
		

		
			foreach((array)$endArray as $key => $val)
			{
				$endurl .= '&'.$key.'='.$val;
			}
				
			if($this -> page_cfg['page_num'] > 4)
			{
				$this -> page_str .= '<a href="'.$url.'&page_num=1'.$endurl.'"  class="page_first" >第一页</a>';
			}
			
			for($numBegin; $numBegin <= $numEnd;  $numBegin++)
			{
				if($numBegin==$this -> page_cfg['page_num'])
				{
					if($page_count ==1 and $this -> page_cfg['page_num']==1)
					{
						//单页不显示一页
					}else{
						$this -> page_str .= '<b>'.$numBegin.'</b>&nbsp;';
					}
				}else{
					$this -> page_str .= '<a href="'.$url.'&page_num='.$numBegin.$endurl.'" title="第'.$numBegin.'页" >'.$numBegin.'</a>&nbsp;';
				}
			}
			if($this -> page_cfg['page_num'] < ($page_count -3) )
			{
				$this -> page_str .= '<a href="'.$url.'&page_num='.$page_count.$endurl.'"  class="page_end" >最末页</a>';
			}
	


		$this -> page_str .=  "&nbsp;&nbsp;</div>";
		//显示隐藏数据
		$this -> page_str .= '<input type="hidden" name="PGURL" id="PGURL" value="'.$url.'"  />';
		
		
		return $this -> page_str;
		
	}
	
	
	//当前页码，总记录数，每次显示条数
	function get_admin_page( $result_count=''  , $act = 'info_list' ) // $type = 'js' or 'url'
	{

		if( $this-> page_size > 0 ){
			$this -> set_page_size( $this-> page_size );
		}
		$page_count = ceil( $result_count / $this -> page_cfg['page_size']);
		$numBegin = 1;
		$numEnd   = 0 ;

		
		if( ( $this -> page_cfg['page_num']  <= ($page_count - 3) ) and  ( $page_count > 3 ) and  ( $this -> page_cfg['page_num']  <= $page_count ) )
		{
			$numEnd = $this -> page_cfg['page_num'] + 3 ;
		}else{
			$numEnd = $page_count;
		}
		
		if( ( $numBegin  <= ($this -> page_cfg['page_num'] -3) ) and (($this -> page_cfg['page_num'] -3) > 0 ) )
		{
			$numBegin = $this -> page_cfg['page_num'] - 3;
		}else{ 
			$numBegin = $numBegin;
		}
		

		$this -> page_str  .= '<div class="action-page">';
		
        $this -> page_str  .= '<div style="float:right;width:100%;"><ul class="pagination pagination-sm" >';
				
		if($this -> page_cfg['page_num'] > 0)
		{
			$this -> page_str .= '<li><a href="javascript:go_to_page(1,\''.$act.'\')" aria-label="Previous"><span aria-hidden="true">«</span></a></li>';
		}
		
		for($numBegin; $numBegin <= $numEnd;  $numBegin++)
		{
			if($numBegin==$this -> page_cfg['page_num'])
			{
				$this -> page_str .= '<li class="active"><a href="#">'.$numBegin.' <span class="sr-only">(current)</span></a></li>';
			}else{
				$this -> page_str .= ' <li><a href="javascript:go_to_page('.$numBegin.',\''.$act.'\')">'.$numBegin.'</a></li>';
			}
		}
		if($page_count > 0 )
		{
			$this -> page_str .= ' <li><a href="javascript:go_to_page('.$page_count.',\''.$act.'\')" aria-label="Next"><span aria-hidden="true">»</span></a></li>';
		}


		$this -> page_str .=  "</ul>";
		
		
		$this -> page_str .= '<div class="row">
      <div class="col-xs-8 col-sm-8 page_br">
      '.$this -> page_lang['txt_total'].'<span style="font-weight:bold;color:#FF0000">'.$result_count.'</span>'.$this -> page_lang['txt_result'].'&nbsp;&nbsp;'.$this -> page_lang['txt_at'].'<span style="font-weight:bold;color:#FF0000">'.$this -> page_cfg['page_num'].'<span style="color:#000;font-weight:bold;">/</span>'.$page_count.'</span> '.$this -> page_lang['txt_page'].'
      </div>
      <div class="col-xs-4 col-sm-4 page_br">
        <input type="text" id="get_page" name="get_page" value=""  style="width:30px"><a href="javascript:go_to_page(document.getElementById(\'get_page\').value,\''.$act.'\')" class="btn btn-primary btn-xs">'.$this -> page_lang['txt_go_page'].'</a>
      </div>
    </div>';
		

		$this -> page_str .=  ' </div>';

		return $this -> page_str;
	}
}

///$Page  = new Page();
?>