var gridSelected,gridAll;
var alarmruleid = $.getUrlParam("alarmruleid");
var alarmtype = $.getUrlParam("alarmtype");
var alarmsource = $.getUrlParam("alarmsource");

$(function() {
//	console.log("video==========");
	$('#leftDeviceId').combobox({
		 url:'ywvideochannelsetAction!query_Ywvideochannelset.do',
		 textField:"channelsetname",  
		 valueField:"channelsetid",
		 panelWidth:148,
		 value:"" 
	});
	
	 initButtonEvents();
	 initSelectedUser();
	 initAllUser();
});
function initButtonEvents(){	
	
	$("#buttonAll").bind('click', function(){
		searchAllUser();
    });
	$("#buttonSelected").bind('click', function(){
		searchSelectedUser();
   });
}
 
function initSelectedUser(){
	
	gridSelected = $('#leftDevice').datagrid({
		singleSelect:false,
		queryParams:getSelectedGridParam(),
		url :"ywalarmrulefordeviceAction!get_leftDevice.do",
		columns : [ [ 
		{ 
			field: 'A',
			checkbox:'true',
			width:30
		},{
			title: "",
			field: "id",
			hidden:true,
			width: 150
		},{
			title: "集合名称",
			field: "channelsetname",
			width: 135
		},{
			title: "通道名称",
			field: "deviceName",
			width: 135
		}, {
			title: "通道信息",
			field: "linkdevicenum",
			width: 150
		}]]
	});
	gridSelected.datagrid("getPager").pagination({ 
        showPageList: false,                   
        showRefresh: false,
        layout:['first','prev','next','last']
	});
}

function initAllUser(){
	gridAll = $('#rightDevice').datagrid({
		singleSelect:false,
		queryParams:getAllGridParam(),
		url :"ywalarmrulefordeviceAction!listYwalarmrulefordeviceByPage.do",
		columns : [[ 
		{ 
			field: 'A',
			checkbox:'true',
			width:30
		},{
			title: "",
			field: "relateid",
			hidden:true,
			width: 120
		},
		{
			title: "通道名称",
			field: "linkdevicename",
			width: 167
		},
		{
			title: "通道信息",
			field: "linkdevicenum",
			width: 150
		}] ]
	});
	gridAll.datagrid("getPager").pagination({ 
        showPageList: false,                   
        showRefresh: false,
        layout:['first','prev','next','last']
	});
}

function searchSelectedUser(){
	gridSelected && gridSelected.datagrid('load',  getSelectedGridParam());
}

function searchAllUser(){
	gridAll && gridAll.datagrid('load',  getAllGridParam());
}
function searchSelectedUserAndAllUser(){
	searchSelectedUser();
	searchAllUser();
}

function getSelectedGridParam(){
	
	return{
		alarmsource:alarmsource,
		alarmtype : alarmtype,
		deviceName : $('#leftDeviceName').textbox('getValue'),
		deviceId : $('#leftDeviceId').combobox('getValue')
	}
}

function getAllGridParam(){
	return {
		alarmsource:alarmsource,
		alarmtype : alarmtype,
		LIKElinkdevicename : $('#rightDeviceName').textbox('getValue'),
		tableparam : " and alarmsource = '"+alarmsource+"' and alarmtype='"+alarmtype+"'",
	}
}

function getSelectedRow(grid ,selectall){
	!! selectall && grid.datagrid("selectAll");
	var rows = grid.datagrid("getSelections");
	if(rows.length == 0){
    	top.$.dlg.alertInfo(!! selectall ? '暂无记录！' : '请先勾选记录！');
        return;
    }
    return rows;
}

function goLeft(){
	
	var rows  = getSelectedRow(gridAll);
	doDelete(rows);

}

function goLeftAll(){
	var rows  = getSelectedRow(gridAll,true);
	top.$.dlg.confirm('确定移除所有设备?',function(){
		doDelete(rows);
	 });
}

function goRight(){
	
	doInsert(getSelectedRow(gridSelected));
	searchSelectedUserAndAllUser();
}

function goRightAll(){
	
	var rows  = getSelectedRow(gridSelected,true);
	top.$.dlg.confirm('确定添加所有设备?',function(){
		doInsert(rows);
	});
}
function doInsert(rows){
//	console.log(rows);
	if(rows && rows.length){
		rows.forEach(function(item){
			$.ajax({
		         type:'post',
		         url : 'ywalarmrulefordeviceAction!insert_Ywalarmrulefordevice.do', 
		         data: {'linkdeviceid':item.id, 'linkdevicenum':item.linkdevicenum, 
		        	 	'linkchannelnum':item.linkchannelnum,'linkdevicename':item.deviceName,
		        	 	'alarmruleid':alarmruleid,'alarmsource':alarmsource,'alarmtype':alarmtype}
		    });
		})
		
		searchSelectedUserAndAllUser();
	}
}

function doDelete(rows){
	if(rows && rows.length){
		rows.forEach(function(item){
			$.ajax({
		         type:'post',
		         url : 'ywalarmrulefordeviceAction!delete_Ywalarmrulefordevice.do', 
		         data: {"relateid":item.relateid}
		    });
		})		
		searchSelectedUserAndAllUser();
		
	}
}