var windowId,sysUrl = top.sysUrl,toEdit;
var winId	= $.getUrlParam("winId");
$(function(){
	initGrid();
});

//查询条件
function getQueryParams(){
	var sParam = {};
	sParam.LIKEip = $('#LIKEip').textbox("getValue");
	sParam.LIKEwg = $('#LIKEwg').textbox("getValue");
	sParam.LIKEvlan = $('#LIKEvlan').textbox("getValue");
	sParam.LIKEym = $('#LIKEym').textbox("getValue");
	return sParam;
}
//加载列表
function initGrid(){
	var toolbar = [{
		text	:	'查询',
		iconCls	:	'icon-search',
		handler	:	doSearch
	},{
		text	:	'增加',
		iconCls	:	'icon-add',
		handler	:	doAdd
	},{
		text	:	'修改',
		iconCls	:	'icon-cut',
		handler	:	doEdit
	},{
		text	:	'删除',
		iconCls	:	'icon-delete',
		handler	:	doDelete
	},
		{
        text	:	'模板',
        iconCls	:	'icon-export-excel',
        handler	:	doTemplate
    },{
        text	:	'导入',
        iconCls	:	'icon-update',
        handler	:	doImport
    }
    ,
		{
        text	:	'导出',
        iconCls	:	'icon-download',
        handler	:	doExport
    }
    ];
	$('#tableGrid').datagrid({
		toolbar : toolbar,
		singleSelect:true,
		rownumbers:true,
		queryParams:getQueryParams(),
		url : 'nhywsbipglAction!listNhywsbipglByPage.do',
		columns : [[
			{
				field: 'A',
				checkbox:'true',
				width:30
			},
			{field:'qs',title:'期数',hidden:false,width:120},
			{field:'zj',title:'镇街',hidden:false,width:120},
			{field:'lhybh',title:'六合一编号',hidden:false,width:120},
			{field:'jczhptbh',title:'集成指挥平台编号',hidden:false,width:120},
			{field:'sjkkbh',title:'卡口编号——市局',hidden:false,width:120},
			{field:'zdkkbh',title:'卡口编号_支队',hidden:false,width:120},
			{field:'zpdybm',title:'抓拍单元编码',hidden:false,width:120},
			{field:'kkmc',title:'卡口名称 ',hidden:false,width:120},
			{field:'zmptmc',title:'中盟平台名称',hidden:false,width:120},
			{field:'sbh',title:'设备号',hidden:false,width:120},
			{field:'fx',title:'方向',hidden:false,width:120},
			{field:'ip',title:'IP',hidden:false,width:120},
			{field:'ym',title:'新掩码',hidden:false,width:120},
			{field:'wg',title:'新网关',hidden:false,width:120},
			{field:'vlan',title:'vlan编号',hidden:false,width:120},
			{field:'lkdm',title:'路口代码',hidden:false,width:120},
			{field:'lddm',title:'路段代码',hidden:false,width:120},
			{field:'dlms',title:'道路米数',hidden:false,width:120},
			{field:'jd',title:'经度',hidden:false,width:120},
			{field:'wd',title:'纬度',hidden:false,width:120},
			{field:'dlmc',title:'道路名称',hidden:false,width:120},
			{field:'fxlx',title:'方向类型',hidden:false,width:120},
			{field:'xzqh',title:'行政区划',hidden:false,width:120},
			{field:'ldlx',title:'路段类型',hidden:false,width:120},
			{field:'kkwzlx',title:'卡口位置类型',hidden:false,width:120},
			{field:'kkyylx',title:'卡口应用类型',hidden:false,width:120},
			{field:'jpljtj',title:'具备拦截条件',hidden:false,width:120},
			{field:'zpfxlx',title:'抓拍方向类型',hidden:false,width:120},
			{field:'sfbjkk',title:'是否边界卡口',hidden:false,width:120},
			{field:'ljdy',title:'邻界地域',hidden:false,width:120},
			{field:'cdbh',title:'车道编号',hidden:false,width:120},
			{field:'dcxs',title:'打车限速',hidden:false,width:120},
			{field:'xcxs',title:'小车限速',hidden:false,width:120},
			{field:'zdxs',title:'最低限速',hidden:false,width:120},
			{field:'cdzs',title:'车道总数',hidden:false,width:120},
			{field:'cj',title:'厂家',hidden:false,width:120},
			{field:'dkh',title:'端口号',hidden:false,width:120},
			{field:'yhm',title:'用户名',hidden:false,width:120},
			{field:'yhmm',title:'用户密码',hidden:false,width:120},
			{field:'sblx',title:'设备类型',hidden:false,width:120}
		]]
	});
}
//删除
function doDelete(){
	var selRows = $('#tableGrid').datagrid('getSelected');
	if(!selRows){
		$.showMsg('请先选择记录! ');
		return;
	}
	top.$.dlg.confirm('确认删除?',function(){
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'nhywsbipglAction!delete_Nhywsbipgl.do',
			data:{
				id	:selRows.id
			},
			success:function(flag){
				if(flag == "success"){
					$.showMsg("操作成功！");
					doSearch();
				}else{
					$.showMsg("操作失败！");
				}
			}
		});
	});
}
//批量删除
function doDelete_batch(){
	var selRows = $('#tableGrid').datagrid('getChecked');
	var keyword = [];
	if(selRows===null){
		$.showMsg('请先选择一行! ');
		return;
	}else{
		for(var i=0; i<selRows.length; i++){
			keyword.push(selRows[i].id);
		}
		top.$.dlg.confirm('你确认要删除吗?',function(){
			$.ajax({
				type:'post',
				dataType : 'json',
				url:'nhywsbipglAction!batch_delete_Nhywsbipgl.do',
				data:{keyword	:	keyword.join(",")},
				success:function(flag){
					if(!$.isNull(flag)){
						$.showMsg("操作成功！");
						doSearch();
					}else{
						$.showMsg("操作失败！");
					}
				}
			});
		});
	}
}
//查询
function doSearch(){
	$('#tableGrid').datagrid('load',getQueryParams());
}
//增加
function doAdd(){
	toEdit=false;
	windowId = top.$.createWin({
		title	: 	'增加信息',
		width	:  	650,
		height	: 	500,
		onClose:function(){
			var win		=	$.getTopWindowById(windowId);
			win.unloadWindow();
		},
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/yw/nhyw/nhywsbipgl/nhywsbipgl_show.jsp"
	});
}
//修改
function doUpdate(index){
	toEdit=true;
	var selRows = $('#tableGrid').datagrid('getRows')[index];
	windowId = top.$.createWin({
		title	: 	'修改信息',
		width	:  	650,
		height	: 	800,
		data   	:   selRows,
		onClose:function(){
			var win		=	$.getTopWindowById(windowId);
			win.unloadWindow();
		},
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/yw/nhyw/nhywsbipgl/nhywsbipgl_show.jsp"
	});
}
//保存
function saveNodeInfo(){
	var win		=	$.getTopWindowById(windowId);
	var jq		=	win.$;
	var model   =   $.getFormValues("childform",windowId);
	console.log(model);
	if(!win.$('#childform').form('enableValidation').form('validate')){
		return;
	}
	if(toEdit){//修改
		model.XGTJid = jq('#id').val();
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'nhywsbipglAction!update_Nhywsbipgl.do',
			data:model,
			success:function(value){
				if(value=="success"){
					$.showMsg("操作成功！");
					top.$("#"+windowId).dialog('close');
					doSearch();
				}else if(value=="same"){
					$.showMsg("已存在！");
				}else{
					$.showMsg("操作失败！");
				}
			}
		});
	}else{
		//model.checkparam = " and xh='"+model.xh+"' ";
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'nhywsbipglAction!insert_Nhywsbipgl.do',
			data:model,
			success:function(value){
				if(value=="success"){
					$.showMsg("操作成功！");
					top.$("#"+windowId).dialog('close');
					doSearch();
				}else if(value=="same"){
					$.showMsg("已存在！");
				}else{
					$.showMsg("操作失败！");
				}
			}
		});
	}
}
//打开修改
function doEdit(){
	var selIndex = $('#tableGrid').datagrid('getRowIndex', $("#tableGrid").datagrid('getSelected'))
	if(selIndex==-1){
		$.showMsg('请先选择一行! ');
		return;
	}else{
		doUpdate(selIndex);
	}
}
//模板下载
function doTemplate(){
	var baseUrl = $.proPath();
	var downFile = baseUrl+"/down/ip.xlsx";
	window.location.href= downFile;
}
//导入
function doImport(){
	windowId = top.$.createWin({
		id		:	$.uniqueId(),
		title	: 	'导入',
		width	:  	500,
		height	: 	400,
		onClose:function(){
			var win		=	$.getTopWindowById(windowId);
			win.unloadWindow();
		},
		url	:	sysUrl+"/yw/nhyw/nhywsbipgl/nhywsbipglemp.jsp"
	});
}
//导出
function doExport(){
	var model = getQueryParams();
	$.ajax({
		type : 'post',
		dataType : 'json',
		data : model,
		url : 'nhywsbipglAction!count_Nhywsbipgl.do',
		success : function(value) {
			if(!isNull(value)){
				countExport(value);
			}else{
				$.showMsg("操作失败！");
			}
		}
	});
}
// 导出传参条件
function countExport(totalSize) {
	var limit = 5000;
	if (totalSize > 5000) {
		top.$.dlg.confirm('记录过多支持前5000条,确定要将记录导出Excel为表格吗?',function(){
			execExport();
		});
	} else {
		top.$.dlg.confirm('确认导出?',function(){
			execExport();
		})
	}
}
// 调用后台
function execExport() {
	var  ip = $('#LIKEip').textbox("getValue");
	var wg = $('#LIKEwg').textbox("getValue");
	var vlan = $('#LIKEvlan').textbox("getValue");
	var ym = $('#LIKEym').textbox("getValue");

	document.excelForm.para
	document.excelForm.action = 'nhywsbipglAction!exp_Nhywsbipgl.do?LIKEip='+ip+"&LIKEwg="+wg+"&LIKEvlan="+vlan+"&LIKEym="+ym;
	document.excelForm.submit();
}
function isNull(str){
	if(str==null || str=='' || str==" " || str=="null" || str == undefined){
		return true;
	}else{
		return false;
	}
}