var windowId,sysUrl = top.sysUrl,toEdit;
$(function(){
	initComboboxs();
	initDefault();
	initDetailTab();
});

function initComboboxs(){
	$('#LIKEsbzt').combobox({
		 url:'xtclAction!query_Xtcl.do?csdl=vdState', 
		 textField:"csmc",  
		 valueField:"csdm",
		 panelWidth:148,
		 loadFilter:function(data){
				data.unshift({"csmc":"所有类型","csdm":""});
				return data;
			}
	});
	$('#LIKEsbzl').combobox({
		 url:'xtclAction!query_Xtcl.do?csdl=vdType&start=0&limit=999999',
		 textField:"csmc",  
		 valueField:"csdm",
		 panelWidth:148,
		 loadFilter:function(data){
				data.unshift({"csmc":"所有类型","csdm":""});
				return data;
			}
	})
	$('#LIKEcjbh').combobox({
		 url:'sbcjAction!query_Sbcj.do?page=1&rows=999999',
		 textField:"cjmc",  
		 valueField:"cjbh",
		 panelWidth:148,
		 loadFilter:function(data){
				data.unshift({"cjmc":"所有类型","cjbh":""});
				return data;
			} 
	});
}
//默认
function initDefault(){
	initGrid();
}
//查询条件
function getQueryParams(){
	var sParam = {};
	/*
	sParam.tableparam = tableparam;
	
	*/
	sParam.LIKEsbbh = $('#LIKEsbbh').textbox("getValue");
	sParam.LIKEsbmc = $('#LIKEsbmc').textbox("getValue");
	sParam.LIKEsbzt = $('#LIKEsbzt').combobox("getValue")=="请选择"?"":$('#LIKEsbzt').textbox("getValue");
	sParam.LIKEsbzl = $('#LIKEsbzl').textbox("getValue");
	sParam.LIKEsbip = $('#LIKEsbip').textbox("getValue");
	sParam.LIKEcjbh = $('#LIKEcjbh').textbox("getValue")=="请选择"?"":$('#LIKEcjbh').textbox("getValue");
	if(sParam.LIKEsbzl =='请选择') sParam.LIKEsbzl="";
	sParam.order = " order by xx.cjrq desc ";
	return sParam;
}
//加载列表
function initGrid(){
	var toolbar = [{
        text:'查询',
        iconCls:'icon-search',
        handler:function(){doSearch();}
    },{
        text:'增加',
        iconCls:'icon-add',
        handler:function(){doAdd();}
    },{
        text:'修改',
        iconCls:'icon-edit',
        handler:function(){doEdit();}
    },{
        text:'删除',
        iconCls:'icon-delete',
        handler:function(){doDelete();}
    },{
    	text:'扩展信息',
        iconCls:'icon-more',
        handler:function(){nhywjksbtzxx();}
    }/*,{
        text:'Excel模板下载',
        iconCls:'icon-download',
        handler:function(){download();}
    },{
        text:'Excel导入',
        iconCls:'icon-update',
        handler:function(){ShowUploadFileDialog();}
    },{
        text:'重置',
        iconCls:'icon-refresh',
        handler:doReset
    }*/];

	$('#tableGrid').datagrid({
		toolbar : toolbar,
		singleSelect:true,
		rownumbers:false,
		url : 'sbxxAction!listSbxxByPage.do',
		queryParams:getQueryParams(),
		onDblClickRow:showChildFrame,
		columns : [[
		    {field: 'A',checkbox:'true',width:30},
			{field:'sbbh',title:'设备编号',hidden:false,width:155},
			{field:'sbmc',title:'设备名称',hidden:false,width:120, 
				formatter:function showClick(value,record,num){
					return "<a href='javascript:void(0);' class='ahref' onclick='doUpdate("+num+")' >"+value+"</a>";
				}
 			},
			{field:'cjbh',title:'cjbh',hidden:true,width:80},
			{field:'sbip',title:'IP地址',hidden:false,width:105},
			{field:'cjmc',title:'设备厂商',hidden:false,width:60},
			{field:'sbzl',title:'设备类型',hidden:true,width:70},
			{field:'xzmc',title:'行政目录',hidden:true,width:80},
			{field:'xzbh',title:'xzbh',hidden:true,width:80},
			{field:'csmc',title:'类型名称',hidden:false,width:80},
			{field:'sbzt',title:'sbzt',hidden:true,width:70},
			{field:'sbztname',title:'设备状态',hidden:false,width:60}
		]],
		onLoadSuccess:function(data){
			if(data && data.rows && data.rows.length){
				$("#tableGrid").datagrid("selectRow",0);
				setchecked(data);
			}else{
				$("#spsrtd").attr("src", "nhywsbcsglxx.jsp?fromSbxx=1");
			}
		}
	});
	doSearch();
}

function initDetailTab(){
	detailtab = $("#detailtab").tabs();
}
//重置查询
function doReset(){
	$('#LIKEsbbh').textbox("setValue","");
	$('#LIKEsbmc').textbox("setValue","");
	$('#LIKEsbzt').combobox("setValue","");
	$('#LIKEsbzl').combobox("setValue","");
	$('#LIKEsbip').textbox("setValue","");
	$('#LIKEcjbh').combobox("setValue","");
	$('#tableGrid').datagrid('load',getQueryParams());
}
//删除
function doDelete(){
	var selRows = $('#tableGrid').datagrid('getSelected'); 
	if(selRows===null){
		 $.showMsg('请先选择记录! ');
		 return;
	}
	top.$.dlg.confirm('确认删除?',function(){
		$.ajax({
			 type:'post',
			 dataType : 'json',
			 url:'sbxxAction!delete_Sbxx.do',
			 data:{sbbh	:	selRows.sbbh},
			 success:function(flag){
				 if(flag == 'success'){
					 doSearch();
				 }
			 }
		 });
	});
}

//批量删除
function batch_deleteUser(){
	var selRows = $('#searchGrid').datagrid('getChecked'); 
	var keyword = "";
	 if(selRows===null){
		 $.showMsg('请先选择一行! ');
		 return;
	 }else{
		for(var i=0; i<selRows.length; i++){
			if(i == selRows.length -1){
				bianhao += selRows[i].KEYWORD;	
			}else{
				bianhao += selRows[i].KEYWORD+",";	
			}
		}
		top.$.dlg.confirm('你确认要删除吗?',function(){ 
		    $.ajax({
				 type:'post',
				 dataType : 'json',
				 url:'sbxxAction!batch_deletesbxxA.do',
				 data:{keyword	:	keyword},
				 success:function(flag){
					 doSearch();
				 }
			 });
		}); 
	}
}

//扩展信息
function nhywjksbtzxx() {
	var selIndex = $('#tableGrid').datagrid('getRowIndex', $("#tableGrid").datagrid('getSelected'))
	if(selIndex==-1){
		 $.showMsg('请先选择一行! ');
		 return;
	}else{
		var selRows = $('#tableGrid').datagrid('getRows')[selIndex];
		windowId = top.$.createWin({
			 id			:	$.uniqueId(),
			 title		: 	'编辑设备',
			 width		:  	900,
			 records    :   selRows,
			 height		: 	650,
			onClose:function(){
				var win		=	$.getTopWindowById(windowId);
				win.unloadWindow();
			},
			 buttons	:  [{
					     text:$.fn.textbox.defaults.ok,
					     handler:saveNodeInfo
				 	     }],
			 url	:	sysUrl+"/yw/nhyw/nhywjksbtzxx/nhywjksbtzxx.jsp"
		 });
	}
}

//导出
function doExport(){}
////excel上传
function ShowUploadFileDialog() {
	toEdit=false;
	windowId = top.$.createWin({
		 id		:	$.uniqueId(),
		 title	: 	'设备信息导入',
		 width	:  	400,
		 height	: 	130,
		onClose:function(){
			var win		=	$.getTopWindowById(windowId);
			win.unloadWindow();
		},
		 url	:	sysUrl+"/yw/nhyw/nhywdzjkgl/goUpload.jsp"
	 });
}
//Excel模板下载
function download(){
	var selRows = $('#tableGrid').datagrid('getSelected'); 
	if(selRows===null){
		 $.showMsg('请先选择记录! ');
		 return;
	}
	console.log(selRows);
	/*top.$.dlg.confirm('确认导出?',function(){ 
		document.excelForm.action = 'sbxxAction!templateDownload.do?sbbh='+selRows.sbbh;
		document.excelForm.submit();
	});*/
	document.excelForm.action = 'sbxxAction!templateDownload.do?sbbh='+selRows.sbbh;
	document.excelForm.submit();
}
function setchecked(data){
	var selRows = $('#tableGrid').datagrid('getSelected');
	if(selRows){
		showChildFrame(selRows);
	}
}

//查询
function doSearch(){
	$('#tableGrid').datagrid('load',getQueryParams());
}
//增加
function doAdd(){
	toEdit=false;
	windowId = top.$.createWin({
		 id		:	$.uniqueId(),
		 title	: 	'编辑设备',
		 width	:  	650,
		 height	: 	590,
		onClose:function(){
			var win		=	$.getTopWindowById(windowId);
			win.unloadWindow();
		},
		 buttons:  [{
				     text:$.fn.textbox.defaults.ok,
				     handler:saveNodeInfo
			 	     }],
		 url	:	sysUrl+"/yw/nhyw/nhywdzjkgl/nhywazxxgl_show.jsp"
	 });
}

//修改
function doUpdate(index){
	toEdit=true;
	var selRows = $('#tableGrid').datagrid('getRows')[index];
	windowId = top.$.createWin({
		 id			:	$.uniqueId(),
		 title		: 	'编辑设备',
		 width		:  	650,
		 records    :   selRows,
		 height		: 	590,
		onClose:function(){
			var win		=	$.getTopWindowById(windowId);
			win.unloadWindow();
		},
		 buttons	:  [{
				     text:$.fn.textbox.defaults.ok,
				     handler:saveNodeInfo
			 	     }],
		 url	:	sysUrl+"/yw/nhyw/nhywdzjkgl/nhywazxxgl_show.jsp"
	 });
}

function saveNodeInfo(){
	var frame	=	top.$("#"+windowId).children('iframe')[0];
	var win		=	frame.contentWindow||frame.window;
	var jq		=	win.$;
	var model   = $.getFormValues("childform",windowId);
	//alert(win.$("#sflx").prop("checked"));
	model.sflx = win.$("#sflx").prop("checked")?1:0;
	if(!model.cjbh){
		$.showMsg('请选择设备厂商！');
		return ;
	}
	
	if(!model.sbmc){
		$.showMsg('请填写设备名称！');
		return ;
	}
	if(!model.bz){
		$.showMsg('请填写RTSP信息！');
		return ;
	}
	
	if(!model.sbzt || model.sbzt_text !=='正常') model.sbzt = 1 ;
	else model.sbzt = 0 ;
	
	/*if(!model.sbip){
		$.showMsg('请填写设备IP地址！');
		return ;
	}*/
	if(!model.sbdk){
		$.showMsg('请填写设备端口号！');
		return ;
	}
	if(!model.sbdlzh){
		$.showMsg('请填写设备登录账号！');
		return ;
	}
	if(!model.sbdlmm){
		$.showMsg('请填写设备登录密码！');
		return ;
	}
	if(!model.zdzls){
		$.showMsg('请填写直连数！');
		return ;
	}
	if(model.zdzls<=0){
		$.showMsg('请填写正确的直连数！');
		return ;
	}
	 
	if(!win.$('#childform').form('enableValidation').form('validate')){
      return;
	}
	if(!model.spsrtds ||isNaN(model.spsrtds) || 1*model.spsrtds < 0 || 1*model.spsrtds > 128){
		$.showMsg('视频输入通道数为0-128的整数！');
		return; 
	}
	if(!toEdit){
		if(!model.tdlx||model.tdlx ==='请选择' ||model.tdlx===''){
			$.showMsg('请选择默认通道类型！');
			return ;
		}
	}
	if(!model.spsctds ||isNaN(model.spsctds) || 1*model.spsctds < 0 || 1*model.spsctds > 128){
		$.showMsg('视频输出通道数为0-128的整数！');
		return ;
	}
	
	if(!model.bjsrtds ||isNaN(model.bjsrtds) || 1*model.bjsrtds < 0 || 1*model.bjsrtds > 128){
		$.showMsg('告警输入通道数为0-128的整数！');
		return ;
	}
	if(!model.bjsctds ||isNaN(model.bjsctds) || 1*model.bjsctds < 0 || 1*model.bjsctds > 128){
		$.showMsg('告警输出通道数为0-128的整数！');
		return ;
	}
	if(isNaN(model.mlczxy))model.mlczxy = "" ;
	if(!model.dlfwzbh||model.dlfwzbh =='请选择' ||model.dlfwzbh===''){
		model.dlfwzbh = "";
	}
	model.ms =	jq('#ms').val();
	$.showMsg('执行成功');
	if(toEdit){//修改
		var selRows = $("#tableGrid").datagrid('getSelected');
		model.XGTJsbbh = selRows.sbbh;
		$.ajax({
			 type:'post',
			 dataType : 'json',
			 url:'sbxxAction!update_Sbxx.do',
			 data:model,
			 success:function(value){
				 if(value=='success'){
					 top.$("#"+windowId).dialog('close'); doSearch();
				 }
			 }
		 });
	}else{
		$.ajax({
			 type:'post',
			 dataType : 'json',
			 url:'sbxxAction!insert_Sbxx.do',
			 data:model,
			 success:function(value){
				 if(value =='exist'){
					 $.showMsg('设备编号重复！');
					 return ;
				 }
				 if(value=='success'){
					 top.$("#"+windowId).dialog('close'); doSearch();
				 }
			 }
		 });
	}
}

//打开修改
function doEdit(){
	var selIndex = $('#tableGrid').datagrid('getRowIndex', $("#tableGrid").datagrid('getSelected'))
	if(selIndex==-1){
		 $.showMsg('请先选择一行! ');
		 return;
	}else{
		doUpdate(selIndex);
	}		
}

function showChildFrame(row,x){
	if(!x) x ={};
	$("#spsrtd").attr("src", "nhywsbcsglxx.jsp?sbbh="+ (x['sbbh'] || row['sbbh']));
}