/*
 * ots分页
 * js 
 * var page=new ots_page();
		page.url='/api/cs';
		page.first();
		page.pagesBtn='[data-page]';
		page.callback_pageshow=function (data) {
			scope.list=data.data;
			scope.pages=data.pages;
			scope.pageGroup=data.pageGroup;
			scope.npks=data.npks;
			scope.$apply();
		};
		jQuery(document).on('click','.next',function () {
		    page.next();          
		})
		jQuery(document).on('click','.prev',function () {
		    page.prev();          
		})
		
	html
	<button class="prev"  ng-if="pageGroup>0">上一页{{prev}}</button>
		<ul class="page">
			<li ng-repeat="v in pages" data-page='{{v}}'>{{v}}</li>
		</ul>
		<button class="next" ng-if="npks">下一页{{pageGroup}}</button>
		<ul>
			<li ng-repeat="v in list">{{v.pk4}} - {{v.card}}</li>
		</ul>
	PHP
	$npk=I('npk',false);
		if($npk){
		    $spk=base64_decode($npk);
			$spk=json_decode($spk,true);
		}else{
			$spk = array (
			    'pk1' => array('type' => 'INF_MAX'),
			    'pk2' => '',
			    'pk3' => '',
			    'pk4' => '',
			);
		}
		
		$epk = array (
		    'pk1' => array('type' => 'INF_MIN'),
		    'pk2' => '',
		    'pk3' => '',
		    'pk4' => '',
		);
		
		
		$re=O('card')->select(array(
			'spk'=>$spk,
			'epk'=>$epk,
			'filter'=>array('status'=>array('eq',0)),
			'limit'=>'500',
			'field'=>'',
			'page'=>true
		));
		$data=array();
		if($re['next_start_primary_key']){
		    $data['npk']=base64_encode(json_encode($re['next_start_primary_key']));
		}else{
			$data['npk']='';
		}
		$data['spk']=base64_encode(json_encode($spk));
		
		$data['data']=$re['rows'];
		if(IS_AJAX){
		    $this->ajaxReturn($data);
		}
 */
function ots_page(){
	var obj=new Object();
	obj.npks=[];
    obj.spks=[];
    obj.pageGroup=0;
    obj.url='';
    obj.post={};
    obj.first=function () {
    	jQuery.ajax({
    		url:obj.url,
    	    data:obj.post,
    	    dataType: "json",
    	    type: "post",
    	    timeout: 10000,
    	    success: function(data) {
    	    	obj.npk=data.npk;
    	    	obj.npks.push(data.npk);
	    		obj.spks.push(data.spk);
    	    	obj.pageshow({data:data.data});
    	    },
    	    error: function(e) {
    	        console.log(JSON.stringify(e));
    	    }
    	});
    };
    obj.next=function () {
    	obj.pageNow=1;
    	obj.post.npk=obj.npks[obj.pageGroup];
    	jQuery.ajax({
    		url:obj.url,
    	    data:obj.post,
    	    dataType: "json",
    	    type: "post",
    	    timeout: 10000,
    	    success: function(data) {
    	    	if (data.npk!='') {
    	    		obj.npks.push(data.npk);
    	    	}
    	    	obj.pageGroup++;
    	    	obj.npk=data.npk;
    	    	obj.spks.push(data.spk);
    	    	obj.pageshow({data:data.data});
    	    },
    	    error: function(e) {
    	        console.log(JSON.stringify(e));
    	    }
    	});
    };
    obj.prev=function () {
    	obj.pageNow=1;
    	obj.post.npk=obj.spks[obj.pageGroup-1];
    	jQuery.ajax({
    		url:obj.url,
    	    data:obj.post,
    	    dataType: "json",
    	    type: "post",
    	    timeout: 10000,
    	    success: function(data) {
    	    	if (obj.pageGroup>0) {
    	    		obj.pageGroup--;
    	    	}
    	    	obj.npk=data.npk;
    	    	obj.pageshow({data:data.data});
    	    },
    	    error: function(e) {
    	        console.log(JSON.stringify(e));
    	    }
    	});
    };
    obj.pageshow=function () {
		var opts=typeof(arguments[0])!="undefined"?arguments[0]:{};
		obj.pageSize=typeof(obj.pageSize)!="undefined"?obj.pageSize:20;
		obj.pageNow=typeof(obj.pageNow)!="undefined"?obj.pageNow:1;
		obj.totalRs=opts.data.length;
		obj.pageNum=Math.ceil(obj.totalRs/obj.pageSize);
		obj.pages=[];
    	for (var i = 1; i <=obj.pageNum; i++) {
            obj.pages.push(i+obj.pageGroup*5);
        }
        obj.callback_pageshow({
        	data:opts.data.slice((obj.pageNow-1)*obj.pageSize,obj.pageNow*obj.pageSize),
        	pages:obj.pages,
        	pageGroup:obj.pageGroup,
        	npks:obj.npks,
        	npk:obj.npk
        });
    	
    	jQuery(document).on('click',obj.pagesBtn,function () {
    	    var the=jQuery(this);
    	    obj.pageNow=the.attr('data-page')-obj.pageGroup*obj.pageNum;
    	    obj.callback_pageshow({
    	    	data:opts.data.slice((obj.pageNow-1)*obj.pageSize,obj.pageNow*obj.pageSize),
    	    	pages:obj.pages,
            	pageGroup:obj.pageGroup,
            	npks:obj.npks,
            	npk:obj.npk
    	    });
    	});
    };
	return obj;
}