var sxUtil = sxUtil || {};
sxUtil['jqGrid']=function(grid_selector,option,resize){
	var op = {
		datatype: 'json',
		mtype : 'post',
		multiselect: true,
		viewrecords : true,
		emptyrecords:'暂无数据',
		rownumbers: true,//显示排序号
		rownumWidth:'70',
		scrollOffset:1,//垂直滚动条宽度
		rowNum:10,
		rowList:[10,20,30],
		pagerpos: 'left',
		sortorder:'desc',
		loadtext:'数据读取中....',
		pgtext : "{0}",
		recordtext: "第 {0} - {1} 条 / 共 {2} 条",
		loadComplete:function(data){
			// 无数据时列表中间显示'暂无数据'字样
			if( data.records<=0 ){
				if( $(grid_selector).parent().find('.tableNorecords').length <= 0 ){
					$(grid_selector).parent().append('<div class="tableNorecords">暂无数据...</div>');
				}
			}else{
				$('.tableNorecords').remove();
			};
			// 页数小于等于1页时, 切换上下页按钮不亮, 添加页码元素
			$(grid_selector).parents('.ui-jqgrid').find('.ui-pager-table').attr('datatotalrow',data.total);
			$(grid_selector).parents('.ui-jqgrid').find('.ui-pg-input').parents('tr').css('position','relative');
			$(grid_selector).parents('.ui-jqgrid').find('.ui-pg-input').parent().append('<p class="tiaozhi">跳至<span style="margin-left:0.43rem;">页</span></p>')
			$(grid_selector).parents('.ui-jqgrid').find('.ui-pg-selbox').find('option').each(function(i,ele){
				$(ele).text( $(ele).text().split(' ')[0]+' 条/页' )
			});
			if( data.total<=1 ){
				$(grid_selector).parents('.ui-jqgrid').find('.ui-icon-seek-next').parent().addClass('ui-state-disabled');
				$(grid_selector).parents('.ui-jqgrid').find('.ui-icon-seek-prev').parent().addClass('ui-state-disabled');
				$(grid_selector).parents('.ui-jqgrid').find('.ui-pg-input').parent().append('<span class="newPageEle active" style="margin-left:1px">1</span>')
			}else{
				if( data.total<=10 ){//页数小于等于10
					for( var i=0;i<data.total;i++ ){
						if( (i+1) == data.page ){
							$(grid_selector).parents('.ui-jqgrid').find('.ui-pg-input').parent().append('<span class="newPageEle active">'+(i+1)+'</span>');
						}else{
							$(grid_selector).parents('.ui-jqgrid').find('.ui-pg-input').parent().append('<span class="newPageEle">'+(i+1)+'</span>');
						};
					};
					i = null;
				}else{//大于10页
					for( var i=0;i<data.total;i++ ){
						//data.page页 一定要显示出来  并且 data.page 页的左右相邻的4页一定要显示出来
						//当右侧相邻的页数小于等于5页就到尽头时,则都显示出来,大于5页时,则显示4页+...+最后一页
						//当左侧相邻的页数小于等于5页就到尽头时,则都显示出来,大于5页时,则显示第一页+...+4页

						if( (i+1) < data.page ){//data.page的左侧
							if( data.page > 6 ){//左侧相邻的页数大于5页
								if( (i+1) == 1 ){
									$(grid_selector).parents('.ui-jqgrid').find('.ui-pg-input').parent().append('<span class="newPageEle">'+(i+1)+'</span>');
									$(grid_selector).parents('.ui-jqgrid').find('.ui-pg-input').parent().append('<i>.....</i>');
								};
								var leftFourPage = [data.page-1,data.page-2,data.page-3,data.page-4];
								if( leftFourPage.indexOf( (i+1) ) != -1 ){
									$(grid_selector).parents('.ui-jqgrid').find('.ui-pg-input').parent().append('<span class="newPageEle">'+(i+1)+'</span>');
								};
							}else{//左侧相邻的页数小于等于5页
								$(grid_selector).parents('.ui-jqgrid').find('.ui-pg-input').parent().append('<span class="newPageEle">'+(i+1)+'</span>');
							};
						}else if( (i+1) == data.page ){//显示 data.page 页
							$(grid_selector).parents('.ui-jqgrid').find('.ui-pg-input').parent().append('<span class="newPageEle active">'+(i+1)+'</span>');
						}else{//data.page的右侧
							if( data.total-data.page <= 5 ){//右侧相邻的页数小于等于5页
								$(grid_selector).parents('.ui-jqgrid').find('.ui-pg-input').parent().append('<span class="newPageEle">'+(i+1)+'</span>');
							}else{//右侧相邻的页数大于5页
								var rightFourPage = [data.page+1,data.page+2,data.page+3,data.page+4,data.total];
								if( rightFourPage.indexOf( (i+1) ) != -1 ){
									$(grid_selector).parents('.ui-jqgrid').find('.ui-pg-input').parent().append('<span class="newPageEle">'+(i+1)+'</span>');
								};
								if( (i+1) == (data.page+5) ){
									$(grid_selector).parents('.ui-jqgrid').find('.ui-pg-input').parent().append('<i>.....</i>');
								};
							};
						};
					};
					i = null;
				};
			};
			//序号列填值
			for( var i=0;i<$(grid_selector).find("tbody tr.ui-widget-content").length;i++){
				if($('[role=listbox]').val()) {
					$(grid_selector).find("tbody tr.ui-widget-content").eq(i).find('[aria-describedby='+(grid_selector).split('#')[1]+'_orderNumber]').html( '<div class="noHover">'+(parseInt((data.page-1)*$('[role=listbox]').val())+parseInt(i+1))+'</div>' );
				}else {
					$(grid_selector).find("tbody tr.ui-widget-content").eq(i).find('[aria-describedby='+(grid_selector).split('#')[1]+'_orderNumber]').html( '<div class="noHover">'+parseInt(i+1)+'</div>' );
				}

			};
			i = null;

			if( $('.remittanceplanDetailList').length>0 ){
				//账后等列表页里的列表页有多个表格的高度计算
				for( var i=0;i<$(".remittanceplanDetailList").length;i++ ){
					if( $(".remittanceplanDetailList").eq(i).css('left') == '0px' ){
						$("#formForProjectListBox").css('height',($(".remittanceplanDetailList").eq(i).innerHeight()+$('.projectListTab').innerHeight()+50)+'px');
						break;
					}
				};
				i= null;
			}else{
				//立项管理等列表页有多个表格的高度计算
				setTimeout(function(){
					for( var i=0;i<$(".formForProjectList").length;i++ ){
						if( $(".formForProjectList").eq(i).css('left') == '0px' ){
							$("#formForProjectListBox").css('height',($(".formForProjectList").eq(i).innerHeight()+$('.projectListTab').innerHeight()+50)+'px');
							break;
						}
					};
					i= null;
				},10)

			}

		}
	};

	op = $.extend(op,option);

	var parent_column = $(grid_selector).closest('[class*="col-"]');


	if(resize == true){
		//resize to fit page size
		$(window).on('resize.jqGrid', function () {
			var clientHeight = document.documentElement.clientHeight - 238;
			$(grid_selector).jqGrid( 'setGridWidth', !parent_column.width() ? 500 : parent_column.width());
			//$(grid_selector).jqGrid( 'setGridHeight', clientHeight < 300 ? 300 : clientHeight);
	    });

		//resize on sidebar collapse/expand
		$(document).on('settings.ace.jqGrid' , function(ev, event_name, collapsed) {
			if( event_name === 'sidebar_collapsed' || event_name === 'main_container_fixed' ) {
				//setTimeout is for webkit only to give time for DOM changes and then redraw!!!
				setTimeout(function() {
					var clientHeight = document.documentElement.clientHeight - 238;
					$(grid_selector).jqGrid( 'setGridWidth', !parent_column.width() ? 500 : parent_column.width());
					//$(grid_selector).jqGrid( 'setGridHeight', clientHeight < 300 ? 300 : clientHeight);
				}, 20);
			}
	    });
	}

	//输入页码限制及跳转
	$("body").off('keyup').on('keyup','.ui-pg-input',function(event){
		if( event.keyCode == 13 ){
			if( !(/^[1-9]\d*$/.test($(this).val())) ){
				new $.zui.Messager('请输入正确的页数', {
				    type: 'danger'
				 }).show();
				return;
			};
			if( parseInt($(this).val()) > parseInt($(this).parents('tbody').find(".newPageEle:last").text())  ){
				new $.zui.Messager('请输入不大于最大页码的数字', {
				    type: 'danger'
				 }).show();
				return;
			};
			$(grid_selector).jqGrid('setGridParam',{
	            url:op.url,
	            page:$(this).val()
	        }).trigger("reloadGrid");
		}
	});

	// 点击页码跳转
	$("body").off('click').on('click','.newPageEle',function(){
		$(grid_selector).jqGrid('setGridParam',{
            url:op.url,
            page:$(this).text()
        }).trigger("reloadGrid");
	});

	$(grid_selector).jqGrid(op);

	$(window).triggerHandler('resize.jqGrid');

}
