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",
			 onBeforeClose: function () {
				KindEditor.remove('textarea[name="Contents"]');  //当窗口关闭前销毁KindEditor
			}
		 });
	}
}

//����
function doExport(){}
////excel�ϴ�
function ShowUploadFileDialog() {
	toEdit=false;
	windowId = top.$.createWin({
		 id		:	$.uniqueId(),
		 title	: 	'�豸��Ϣ����',
		onClose:function(){
			var win		=	$.getTopWindowById(windowId);
			win.unloadWindow();
		},
		 width	:  	400,
		 height	: 	130,
		 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']));
}