var windowId,proName = window.location.pathname.split("/")[1],sysUrl = "http://"+window.location.host+"/"+proName,toEdit;
$(function(){
	initDefault();	
});
//默认
function initDefault(){
	$('#LIKEbjfl').combobox({
		 url:'xtclAction!query_Xtcl.do?csdl=alarmKind&start=0&limit=999999',
		 textField:"csmc",  
		 valueField:"csdm",
		 panelWidth:148,
		 loadFilter:function(data){
				data.unshift({"csmc":"全部","csdm":""});
				return data;
			}
	})
	$('#LIKEgjsbdy').combobox({
		 url:'xtclAction!query_Xtcl.do?csdl=alarmDeviceDefine&start=0&limit=999999',
		 textField:"csmc",  
		 valueField:"csdm",
		 panelWidth:148,
		 loadFilter:function(data){
				data.unshift({"csmc":"全部","csdm":""});
				return data;
			}
	})
	initGrid();
}
//查询条件
function getQueryParams(){
	var sParam = {};
	sParam.LIKEgjbh = $('#LIKEgjbh').textbox("getValue");
	sParam.LIKEgjmc = $('#LIKEgjmc').textbox("getValue");
	sParam.LIKEbjfl = $('#LIKEbjfl').combobox("getValue");
	sParam.LIKEgjsbdy = $('#LIKEgjsbdy').combobox("getValue");
	if(sParam.LIKEbjfl =='请选择') sParam.LIKEbjfl="";
	if(sParam.LIKEgjsbdy =='请选择') sParam.LIKEgjsbdy="";
	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
    }];
	$('#tableGrid').datagrid({
		toolbar : toolbar,
		singleSelect:true,
		rownumbers:true,
		queryParams:getQueryParams(),
		url : 'gjbhxxAction!listGjbhxxByPage.do',
		columns : [[
		    {field:'xh',title:'xh',hidden:true,width:120, 
				formatter:function showClick(value,record,num){ 
					return "<a  href='javascript:void(0);' class='ahref' onclick='doUpdate("+num+")'>"+value+"</a>";
				}
 			},
			{field:'gjbh',title:'告警编号',hidden:false,width:120},
			{field:'gjmc',title:'告警名称',hidden:false,width:120},
			{field:'bjflmc',title:'告警分类',hidden:false,width:120},
			{field:'gjsbdymc',title:'告警来源',hidden:false,width:120},
			{field:'bz',title:'描述',hidden:false,width:180}
		]]
	});
}

//删除
function doDelete(){
	var selRows = $('#tableGrid').datagrid('getSelected'); 
	if(!selRows){
		$.showMsg('请先选择记录! ');
		 return;
	}
	top.$.dlg.confirm('确认删除?',function(){
		$.ajax({
			 type:'post',
			 dataType : 'json',
			 url:'gjbhxxAction!delete_Gjbhxx.do',
			 data:{xh	:	selRows.xh},
			 success:function(flag){
				 if(flag == "success"){
					 $.showMsg("操作成功！");
					 doSearch();
				 }
			 }
		 });
	});
}

//查询
function doSearch(){	
	$('#tableGrid').datagrid('load',getQueryParams());
}

//增加
function doAdd(){
	toEdit=false;
	windowId = top.$.createWin({
		 title	: 	'增加信息',
		 width	:  	650,
		 height	: 	220,
		 buttons:  [{
				     text:$.fn.textbox.defaults.ok,
				     handler:saveNodeInfo
			 	     }],
		 url	:	sysUrl+"/yw/alarmtaskmanage/alarmcode/gjbhxx_show.jsp"
	 });
}

//修改
function doUpdate(index){
	toEdit=true;
	var selRows = $('#tableGrid').datagrid('getRows')[index];
	windowId = top.$.createWin({
		 title	: 	'修改信息',
		 width	:  	650,
		 data   :   selRows,
		 height	: 	300,
		 buttons:  [{
				     text:$.fn.textbox.defaults.ok,
				     handler:saveNodeInfo
			 	     }],
		 url	:	sysUrl+"/yw/alarmtaskmanage/alarmcode/gjbhxx_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(jq("#bjfl").combobox('getValue')=="请选择"){
		$.showMsg('报警分类不能为空! ');
		return;
	}
	if(jq("#gjsbdy").combobox('getValue')=="请选择"){
		$.showMsg('告警来源不能为空! ');
		return;
	}
	model.bz = jq("#bz").val();
	if(toEdit){//修改
		model.XGTJxh = jq("#xh").val();
		$.ajax({
			 type:'post',
			 dataType : 'json',
			 url:'gjbhxxAction!update_Gjbhxx.do',
			 data:model,
			 success:function(value){
				 if(value=="success"){
					 $.showMsg("操作成功！");
					 top.$("#"+windowId).dialog('close');
					 doSearch();
				 }
			 }
		 });
	}else{
		$.ajax({
			 type:'post',
			 dataType : 'json',
			 url:'gjbhxxAction!insert_Gjbhxx.do',
			 data:model,
			 success:function(value){
				 if(value =='exist'){
						$.showMsg('告警编号重复！');
						return ;
					}
				 if(value=="success"){
					 $.showMsg("操作成功！");
					 top.$("#"+windowId).dialog('close');
					 doSearch();
				 }
			 }
		 });
	}
}

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