var gridSelected,gridAll;
var winId	= $.getUrlParam("winId");
var records = top.$("#"+winId).panel("options").data;
$(function() {
	 initButtonEvents();
	 initSelectedUser();
	 initAllUser();
});
function initButtonEvents(){
	$("#buttonAll").bind('click', function(){
		searchAllUser();
    });
	$("#buttonSelected").bind('click', function(){
		searchSelectedUser();
   });
}
 
function initSelectedUser(){
	gridSelected = $('#selectedchannelset').datagrid({
		singleSelect:true,
		queryParams:getSelectedGridParam(),
		url :'ywvideochannelsetAction!query_Ywvideochannelset_taskselect.do',
		columns : [ [ 
		{ 
			field: 'A',
			checkbox:'true',
			width:30
		},
		{
			title: "",
			field: "channelsetid",
			hidden:true,
			width: 150
		},
		{
			title: "集合名称",
			field: "channelsetname",
			width: 150
		}, {
			title: "备注",
			field: "remarks",
			width: 150
		}] ]
	});
	gridSelected.datagrid("getPager").pagination({ 
        showPageList: false,                   
        showRefresh: false,
        layout:['first','prev','next','last']
	});
}

function initAllUser(){
	gridAll = $('#allChannelset').datagrid({
		singleSelect:true,
		queryParams:getAllGridParam(),
		url :'ywvideochannelsetAction!query_Ywvideochannelset_taskunselect.do',
		columns : [[ 
		{ 
			field: 'A',
			checkbox:'true',
			width:30
		},{
			title: "",
			field: "channelsetid",
			hidden:true,
			width: 150
		},
		{
			title: "集合名称",
			field: "channelsetname",
			width: 150
		},
		{
			title: "备注",
			field: "remarks",
			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{
		taskid : records.taskid,
		LIKEchannelsetname : $('#channelsetname').textbox('getValue'),
	}
}

function getAllGridParam(){
	return {
		taskid : records.taskid,
		LIKEchannelsetname : $('#channelsetnameAll').textbox('getValue'),
	}
}

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 goRight(){
	var data=$('#selectedchannelset').datagrid('getData');
	if(data.total>0){
		$.showMsg("已选择集合！");
		return ;
	}
	doInsert(getSelectedRow(gridAll));
	searchSelectedUserAndAllUser();
}

function goRightAll(){
	var rows  = getSelectedRow(gridAll,true);
	top.$.dlg.confirm('确定添加所有用户?',function(){
		doInsert(rows);
	});
}

function goLeft(){
	var rows  = getSelectedRow(gridSelected);
	top.$.dlg.confirm('确定移除所选集合?',function(){ 
		doDelete(rows);
	});
}

function goLeftAll(){
	var rows  = getSelectedRow(gridSelected,true);
	top.$.dlg.confirm('确定移除所有用户?',function(){
		doDelete(rows);
	 });
	
}
function doInsert(rows){
	if(rows && rows.length){
		for(var i = 0; rows[i]; i++){
			$.ajax({
		         type:'post',
		         url : 'ywvideotaskAction!update_Ywvideotask.do', 
		         data: {channelsetid:rows[i]["channelsetid"],channelsetname:rows[i]["channelsetname"],XGTJtaskid:records.taskid},
		         success:function(res){
		        	 if(res){
		        		 searchSelectedUserAndAllUser();
		        	 }
		         }
		    });
		}
	}
}

function doDelete(rows){
	if(rows && rows.length){
		for(var i = 0; rows[i]; i++){
			$.ajax({
		         type:'post',
		         url : 'ywvideotaskAction!delete_channelset_Ywvideotask.do', 
		         data: {taskid:records.taskid},
		         success:function(res){
		        	 if(res){
		        		 searchSelectedUserAndAllUser();
		        	 }
		         }
		    });
		}
	}
}