var windowId,proName = window.location.pathname.split("/")[1],sysUrl = "http://"+window.location.host+"/"+proName,toEdit;
var mlId = "-1";
var mlName = "";
var nNode = {};
$(function(){
	initDefault();	
});
//默认
function initDefault(){
	initGrid();
}
//查询条件
function getQueryParams(){
	var sParam = {};
	return sParam;
}
//加载列表
function initGrid(){
	$("#ruleTree").empty();
	var params = {};
	params.taskname = $("#LIKEtaskname").val();
	$.ajax({
		type : "post",
		url  : 'ywvideotaskcatalogAction!listYwvideotaskcatalogByPage.do',
		dateType : "JSON",
		data : params,
		success : function(res){
			if(res && res.length>0){
				var treeArr = [];
				for(i in res ){
					var objP = {};
					objP.id = res[i].catalogid;
					objP.text = res[i].catalogname;
					objP.pid = "-1";
					if(res[i].children && res[i].children.length>0){
						var childrens = res[i].children;
						var objArr = [];
						for(j in childrens ){
							var objC = {};
							objC.id = childrens[j].taskid;
							objC.text = childrens[j].taskname+"-"+childrens[j].detectionswitchname;
							objC.pid = objP.id;
							objC.parentname = objP.text;
							objArr.push(objC);
						}
						objP.children = objArr;
					}
					treeArr.push(objP);
				}
				$("#ruleTree").tree(
						{
							data:treeArr,
					    	onClick:function(node){
					    		if(node.pid=="-1"){
					    			mlId = node.id;
					    			mlName = node.text;
					    		}else{
					    			mlId = node.pid;
					    			mlName = node.parentname;
					    		}
					    		nNode = node;
					    		if(node && node.pid!="-1"){
									setchecked(node);
								}
						 	},
							onDblClick:function(node){
								nNode = node;
								if(node && node.pid!="-1"){
									setchecked(node);
								}
							},
							onLoadSuccess:function(){
								setchecked("");
								
							}
					});
			}else{
				$("#ruleTree").append("<li style=\"margin: 50px 200px;\">暂无数据</li>");
			}
		},
		error : function(res){
		}
	})
}
//增加任务
function doAddTask(){
	if(mlId=="-1"){
		$.showMsg("请先选择目录！");
		return;
	}
	windowId = top.$.createWin({
		title	: 	'增加任务',
		width	:  	750,
		height	: 	600,
		data    : {mlId:mlId,mlName:mlName},
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:insertTask
		}],
		url	:	sysUrl+"/yw/videomanage/videotask/ywvideotask/ywvideotask_show.jsp"
	 });
}
//增加规则
function doAddRule(){
	if(nNode.pid=="-1"){
		$.showMsg("请先选择任务！");
		return;
	}
	windowId = top.$.createWin({
		title	: 	'增加规则',
		width	:  	750,
		height	: 	600,
		data    : {taskid:nNode.id},
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:insertRule
		}],
		url	:	sysUrl+"/yw/videomanage/videotask/ywvideotaskrule/ywvideotaskrule_show.jsp"
	});
}
//启用
function doStart(){
	var param = {};
	param.detectionswitch = 1;
	if(nNode.pid=="-1"){
		param.XGTJcatalogid = nNode.id;
	}else{
		param.XGTJtaskid = nNode.id;
	}
	$.ajax({
		type:'post',
		dataType : 'json',
		url:'ywvideotaskAction!update_Ywvideotask.do',
		data:param,
		success:function(value){
			if(value=="success"){
				$.showMsg("操作成功！");
				doSearch();
			}else if(value=="same"){
				$.showMsg("已存在！");
			}else{
				$.showMsg("操作失败！");
			}
		}
	});
}
//停止
function doStop(){
	var param = {};
	param.detectionswitch = 0;
	if(nNode.pid=="-1"){
		param.XGTJcatalogid = nNode.id;
	}else{
		param.XGTJtaskid = nNode.id;
	}
	$.ajax({
		type:'post',
		dataType : 'json',
		url:'ywvideotaskAction!update_Ywvideotask.do',
		data:param,
		success:function(value){
			if(value=="success"){
				$.showMsg("操作成功！");
				doSearch();
			}else if(value=="same"){
				$.showMsg("已存在！");
			}else{
				$.showMsg("操作失败！");
			}
		}
	});
}
//增加目录
function doAddCatalog(){
	windowId = top.$.createWin({
		title	: 	'增加信息',
		width	:  	650,
		height	: 	300,
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:insertCatalog
		}],
		url	:	sysUrl+"/yw/videomanage/videotask/ywvideotaskcatalog/ywvideotaskcatalog_show.jsp"
	 });
}
function insertTask(){
	var win		=	$.getTopWindowById(windowId);
	var jq		=	win.$;
	var model   =   $.getFormValues("childform",windowId);
	if(!win.$('#childform').form('enableValidation').form('validate')){
      return;
	}
	doSearch();
}
function insertRule(){
	return;
	var win		=	$.getTopWindowById(windowId);
	var jq		=	win.$;
	var model   =   $.getFormValues("childform",windowId);
	if(!win.$('#childform').form('enableValidation').form('validate')){
		return;
	}
	doSearch();
}
function insertCatalog(){
	var win		=	$.getTopWindowById(windowId);
	var jq		=	win.$;
	var model   =   $.getFormValues("childform",windowId);
	if(!win.$('#childform').form('enableValidation').form('validate')){
		return;
	}
	$.ajax({
		type:'post',
		dataType : 'json',
		url:'ywvideotaskcatalogAction!insert_Ywvideotaskcatalog.do',
		data:model,
		success:function(value){
			if(value=="success"){
				$.showMsg("操作成功！");
				top.$("#"+windowId).dialog('close');
				doSearch();
			}else if(value=="same"){
				$.showMsg("已存在！");
			}else{
				$.showMsg("操作失败！");
			}
		}
	});
}
//选中目录展示通道列表
function setchecked(data){
	var taskId = ""
	if(data){
		taskId = data.id;
	}
	$("#ywvideotaskbysetlist").prop("src",'../ywvideotaskbysetlist/ywvideotaskbysetlist.jsp?taskId='+taskId);		
}
//删除
function doDelete(){
	var selRows = $('#tableGrid').datagrid('getSelected'); 
	if(!selRows){
		$.showMsg('请先选择记录! ');
		return;
	}
	top.$.dlg.confirm('确认删除?',function(){
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywvideotaskcatalogAction!delete_Ywvideotaskcatalog.do',
			data:{
				catalogid	:	selRows.catalogid

			},
			success:function(flag){
				if(flag == "success"){
					$.showMsg("操作成功！");
					doSearch();
				}else{
					$.showMsg("操作失败！");
				}
			}
		});
	});
}
//批量删除
function doDelete_batch(){
	var selRows = $('#tableGrid').datagrid('getChecked'); 
	var keyword = [];
	 if(selRows===null){
		$.showMsg('请先选择一行! ');
		 return;
	 }else{
		for(var i=0; i<selRows.length; i++){
			keyword.push(selRows[i].catalogid);
		}
		top.$.dlg.confirm('你确认要删除吗?',function(){ 
		    $.ajax({
				 type:'post',
				 dataType : 'json',
				 url:'ywvideotaskcatalogAction!batch_delete_Ywvideotaskcatalog.do',
				 data:{keyword	:	keyword.join(",")},
				 success:function(flag){
				 	if(!$.isNull(flag)){
						$.showMsg("操作成功！");
					 	doSearch();
					}else{
						$.showMsg("操作失败！");
					}
				}
			 });
		}); 
	}
}
//查询
function doSearch(){	
	initGrid();
}
//增加
function doAdd(){
	toEdit=false;
	windowId = top.$.createWin({
		title	: 	'增加信息',
		width	:  	650,
		height	: 	300,
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/im/ywvideotaskcatalog/ywvideotaskcatalog_show.jsp"
	 });
}
//修改
function doUpdate(index){
	toEdit=true;
	var selRows = $('#tableGrid').datagrid('getRows')[index];
	windowId = top.$.createWin({
		title	: 	'修改信息',
		width	:  	650,
		height	: 	300,
		data   	:   selRows,
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/im/ywvideotaskcatalog/ywvideotaskcatalog_show.jsp"
	});
}
//保存
function saveNodeInfo(){
	var win		=	$.getTopWindowById(windowId);
	var jq		=	win.$;
	var model   =   $.getFormValues("childform",windowId);
	if(!win.$('#childform').form('enableValidation').form('validate')){
      return;
	}
	if(toEdit){//修改
		model.XGTJcatalogid = jq('#catalogid').val();

		//model.checkparam = " and xh='"+model.xh+"' ";
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywvideotaskcatalogAction!update_Ywvideotaskcatalog.do',
			data:model,
			success:function(value){
				if(value=="success"){
					$.showMsg("操作成功！");
					top.$("#"+windowId).dialog('close');
					doSearch();
				}else if(value=="same"){
					$.showMsg("已存在！");
				}else{
					$.showMsg("操作失败！");
				}
			}
		});
	}else{
		//model.checkparam = " and xh='"+model.xh+"' ";
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywvideotaskcatalogAction!insert_Ywvideotaskcatalog.do',
			data:model,
			success:function(value){
				if(value=="success"){
					$.showMsg("操作成功！");
					top.$("#"+windowId).dialog('close');
					doSearch();
				}else if(value=="same"){
					$.showMsg("已存在！");
				}else{
					$.showMsg("操作失败！");
				}
			}
		});
	}
}
//打开修改
function doEdit(){
	var selIndex = $('#tableGrid').datagrid('getRowIndex', $("#tableGrid").datagrid('getSelected'))
	if(selIndex==-1){
		$.showMsg('请先选择一行! ');
		 return;
	}else{
		doUpdate(selIndex);
	}		
}
//模板下载
function doTemplate(){
	var baseUrl = $.proPath();
	var downFile = baseUrl+"/down/down.xls";
	window.location.href= downFile;
}
//导入
function doImport(){
	toEdit=false;
	windowId = top.$.createWin({
		 id		:	$.uniqueId(),
		 title	: 	'导入',
		 width	:  	400,
		 height	: 	130,
		 url	:	sysUrl+"/im/xx/ywvideotaskcatalog_imp.jsp"
	 });
}
//导出
function doExport(){
	var model = getQueryParams();
	$.ajax({
		type : 'post',
		dataType : 'json',
		data : model,
		url : 'ywvideotaskcatalogAction!count_Ywvideotaskcatalog.do',
		success : function(value) {
			if(!$.isNull(value)){
				countExport(value);
			}else{
				$.showMsg("操作失败！");
			}
		}
	});
}
// 导出传参条件
function countExport(totalSize) {
	var limit = 5000;
	if (totalSize > 5000) {
		top.$.dlg.confirm('记录过多支持前5000条,确定要将记录导出Excel为表格吗?',function(){ 
			execExport();
		});		
	} else {
		top.$.dlg.confirm('确认导出?',function(){ 
			execExport();
		})
	}
}
// 调用后台
function execExport() {
	var model = getQueryParams();
	var downParam = "";
	document.excelForm.action = 'ywvideotaskcatalogAction!exp_Ywvideotaskcatalog.do?'+downParam;
	document.excelForm.submit(); 
}
