var windowId,proName = window.location.pathname.split("/")[1],sysUrl = "http://"+window.location.host+"/"+proName,toEdit;
$(function(){
	initSelect();
	initDefault();	
});
//默认
function initDefault(){
	initGrid();
}
//查询条件
function getQueryParams(){
	var sParam = {};
	sParam.taskname = $("#taskname").textbox("getValue");
	sParam.tasktype = $("#tasktype").textbox("getValue");
	sParam.tsaklevel = $("#tsaklevel").textbox("getValue");
	sParam.taskmodel = $("#taskmodel").textbox("getValue");
	//sParam.detectionswitch = $("#detectionswitch").textbox("getValue");
	sParam.order = "order by createtime desc ";
	return sParam;
}
//加载列表
function initGrid(){	
	var toolbar = [{
    	text	:	'查询',
        iconCls	:	'icon-search',
        handler	:	doSearch
    },{
        text	:	'增加',
        iconCls	:	'icon-add',
        handler	:	doAddTask
    },{
        text	:	'修改',
        iconCls	:	'icon-cut',
        handler	:	doEdit
    },{
        text	:	'删除',
        iconCls	:	'icon-delete',
        handler	:	doDelete_batch
    },{
        text	:	'设置规则',
        iconCls	:	'icon-add',
        handler	:	doSetRule
    },{
    	text	:	'选择集合',
    	iconCls	:	'icon-add',
    	handler	:	doChannelSet
    },{
        text	:	'启用',
        iconCls	:	'icon-start',
        handler	:	doStart
    },{
        text	:	'停止',
        iconCls	:	'icon-end',
        handler	:	doStop
    }];
	$('#tableGrid').datagrid({
		toolbar : toolbar,
		singleSelect:false,
		rownumbers:true,
		queryParams:getQueryParams(),
		url : 'ywvideotaskAction!listYwvideotaskMy.do',
		columns : [[
		    {
				field: 'A',
				checkbox:'true',
				width:30
			},
		    {field:'taskid',title:'任务id',hidden:false,width:120/*, 
				formatter:function showClick(value,record,num){ 
					return "<a  href='javascript:void(0);' class='ahref' onclick='doUpdate("+num+")'>"+value+"</a>";
				}
 			*/},
			{field:'taskname',title:'任务名称',hidden:false,width:120},
			{field:'tasktypename',title:'任务类型',hidden:false,width:120},
			{field:'tsaklevelname',title:'任务优先级',hidden:false,width:80},
			{field:'taskmodelname',title:'任务模式',hidden:false,width:80},
			{field:'ruleid',title:'是否设置规则',hidden:false,width:85,
				formatter: function(value,row,index){
                    if (value == null){
                        return '否';
                    } else {
                        return '是';
                    }
                }
			},
			{field:'channelsetname',title:'选择集合',hidden:false,width:100},
			{field:'daystarttime',title:'按天开始时间',hidden:true,width:120},
			{field:'daystarttimebyhour',title:'按天间隔时间(小时)',hidden:true,width:120},
			{field:'channeltotals',title:'通道总数',hidden:true,width:120},
			{field:'mondayruntime',title:'周一运行时间',hidden:true,width:120},
			{field:'mondayrunbyhour',title:'周一运行间隔时间',hidden:true,width:120},
			{field:'tuesdayruntime',title:'周二运行时间',hidden:true,width:120},
			{field:'tuesdayrunbyhour',title:'周二运行间隔时间',hidden:true,width:120},
			{field:'wednesdayuntime',title:'周三运行时间',hidden:true,width:120},
			{field:'wednesdayunbyhour',title:'周三运行间隔时间',hidden:true,width:120},
			{field:'thursdayruntime',title:'周四运行时间点',hidden:true,width:120},
			{field:'thursdayrunbyhour',title:'周四运行间隔时间',hidden:true,width:120},
			{field:'fridayruntime',title:'周五运行时间',hidden:true,width:120},
			{field:'fridayrunbyhour',title:'周五运行间隔时间',hidden:true,width:120},
			{field:'saturdatruntime',title:'周六运行时间',hidden:true,width:120},
			{field:'saturdatrunbyhour',title:'周六运行间隔时间',hidden:true,width:120},
			{field:'sundayruntime',title:'周天运行时间',hidden:true,width:120},
			{field:'sundayrunbyhour',title:'周天运行间隔时间',hidden:true,width:120},
			{field:'mondayswitch',title:'周一运行开关',hidden:true,width:120},
			{field:'tuesdayswitch',title:'周二运行开关',hidden:true,width:120},
			{field:'wednesdayswitch',title:'周三运行开关',hidden:true,width:120},
			{field:'thursdayswitch',title:'周四运行开关点',hidden:true,width:120},
			{field:'fridayswitch',title:'周五运行开关',hidden:true,width:120},
			{field:'saturdayswitch',title:'周六运行开关',hidden:true,width:120},
			{field:'sundayswitch',title:'周天运行开关',hidden:true,width:120},
			{field:'detectionswitchname',title:'是否启用',hidden:false,width:80},
			{field:'extend1name',title:'审核状态',hidden:false,width:80},
			{field:'extend2',title:'审核人',hidden:false,width:120},
			{field:'extend3',title:'审核时间',hidden:false,width:130},
			{field:'detectionstarttime',title:'检测开始时间',hidden:true,width:130},
			{field:'taskdescribe',title:'任务描述',hidden:true,width:120},
			{field:'createaccount',title:'创建人员',hidden:true,width:120},
			{field:'createtime',title:'创建日期',hidden:false,width:150},
			{field:'modifyaccount',title:'修改人员',hidden:true,width:120},
			{field:'modifytime',title:'修改日期',hidden:true,width:120},
			{field:'remarks',title:'备注',hidden:true,width:120},
			
			{field:'extend4',title:'扩展4',hidden:true,width:120}
		]]
	});
}
//删除
function doDelete(){
	var selRows = $('#tableGrid').datagrid('getSelected'); 
	if(!selRows){
		$.showMsg('请先选择记录! ');
		return;
	}
	top.$.dlg.confirm('确认删除?',function(){
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywvideotaskAction!delete_Ywvideotask.do',
			data:{
				taskid	:	selRows.taskid

			},
			success:function(flag){
				if(flag == "success"){
					$.showMsg("操作成功！");
					doSearch();
				}else{
					$.showMsg("操作失败！");
				}
			}
		});
	});
}
//批量删除
function doDelete_batch(){
	var selRows = $('#tableGrid').datagrid('getChecked'); 
	var keyword = [];
	 if(selRows===null || !selRows || selRows.length<1 || selRows==[]){
		$.showMsg('请先选择一行! ');
		 return;
	 }else{
		for(var i=0; i<selRows.length; i++){
			keyword.push(selRows[i].taskid);
		}
		top.$.dlg.confirm('你确认要删除吗?',function(){ 
		    $.ajax({
				 type:'post',
				 dataType : 'json',
				 url:'ywvideotaskAction!batch_delete_Ywvideotask.do',
				 data:{keyword	:	keyword.join(",")},
				 success:function(flag){
					 if(flag=="success"){
						$.showMsg("操作成功！");
					 	doSearch();
					}else{
						$.showMsg("操作失败！");
					}
				}
			 });
		}); 
	}
}
//查询
function doSearch(){	
	$('#tableGrid').datagrid('load',getQueryParams());
}
//增加
function doAdd(){
	toEdit=false;
	windowId = top.$.createWin({
		title	: 	'增加信息',
		width	:  	650,
		height	: 	300,
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/im/ywvideotask/ywvideotask_show.jsp"
	 });
}
//修改
function doUpdate(index){
	toEdit=true;
	var selRows = $('#tableGrid').datagrid('getRows')[index];
	windowId = top.$.createWin({
		title	: 	'修改信息',
		width	:  	650,
		height	: 	400,
		data   	:   selRows,
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/yw/videomanage/videotask/ywvideotask/ywvideotask_show_new.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.XGTJtaskid = jq('#taskid').val();
		//model.checkparam = " and xh='"+model.xh+"' ";
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywvideotaskAction!update_Ywvideotask.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:'ywvideotaskAction!insert_Ywvideotask.do',
			data:model,
			success:function(value){
				if(value=="success"){
					$.showMsg("操作成功！");
					top.$("#"+windowId).dialog('close');
					doSearch();
				}else if(value=="same"){
					$.showMsg("已存在！");
				}else{
					$.showMsg("操作失败！");
				}
			}
		});
	}
}
//设置规则或者关联通道集合界面操作完成后关闭弹窗事件
function saveRuleOrSet(){
	top.$("#"+windowId).dialog('close');
	doSearch();
}
//打开修改
function doEdit(){
	var selIndex = $('#tableGrid').datagrid('getRowIndex', $("#tableGrid").datagrid('getSelected'));
	var selRows = $('#tableGrid').datagrid('getChecked');
	if(selRows.length > 1){
		$.showMsg('请选择一条记录进行数据修改! ');
		return;
	}else{
		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/ywvideotask_imp.jsp"
	 });
}
//选择集合
function doChannelSet(){
	var selRows = $('#tableGrid').datagrid('getSelected'); 
	var selChecked = $('#tableGrid').datagrid('getChecked'); 
	if(selChecked.length > 1){
		$.showMsg('请选择一条记录关联集合! ');
		return;
	}else{
		if(!selRows){
			$.showMsg('请选择一条记录选择集合! ');
			return;
		}
		windowId = top.$.createWin({
			title	: 	'增加通道集合',
			width	:  	800,
			height	: 	500,
			data 	: selRows,
			buttons :  [{
				text:$.fn.textbox.defaults.ok,
				handler:saveRuleOrSet
				//handler:saveNodeInfo
			}],
			url	:	sysUrl+"/yw/videomanage/videotask/ywvideotaskbysetlist/addChannelSet.jsp"
		 });
	}
}
//导出
function doExport(){
	var model = getQueryParams();
	$.ajax({
		type : 'post',
		dataType : 'json',
		data : model,
		url : 'ywvideotaskAction!count_Ywvideotask.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 = 'ywvideotaskAction!exp_Ywvideotask.do?'+downParam;
	document.excelForm.submit(); 
}
//启用
function doStart(){
	var selRows = $('#tableGrid').datagrid('getChecked'); 
	var keyword = [];
	 if(selRows===null || !selRows || selRows.length<1 || selRows==[]){
		$.showMsg('请先选择一行! ');
		 return;
	 }else{
		for(var i=0; i<selRows.length; i++){
			if(selRows[i].extend1!=1){
				$.showMsg("请选择审批通过的计划！");
				return ;
			}
			keyword.push(selRows[i].taskid);
		}
		var param = {};
		param.detectionswitch = 1;
		param.keyword = keyword.join(",");
		top.$.dlg.confirm('您确认要启用吗?',function(){ 
			$.ajax({
				type:'post',
				dataType : 'json',
				url:'ywvideotaskAction!batch_update_Ywvideotask.do',
				data:param,
				success:function(value){
					if(value=="success"){
						$.showMsg("操作成功！");
						doSearch();
					}else if(value=="same"){
						$.showMsg("已存在！");
					}else{
						$.showMsg("操作失败！");
					}
				}
			});
		}); 
	}
}
//停止
function doStop(){
	var selRows = $('#tableGrid').datagrid('getChecked'); 
	var keyword = [];
	 if(selRows===null || !selRows || selRows.length<1 || selRows==[]){
		$.showMsg('请先选择一行! ');
		 return;
	 }else{
		for(var i=0; i<selRows.length; i++){
			keyword.push(selRows[i].taskid);
		}
		var param = {};
		param.detectionswitch = 0;
		param.keyword = keyword.join(",");
		top.$.dlg.confirm('您确认要启用吗?',function(){ 
			$.ajax({
				type:'post',
				dataType : 'json',
				url:'ywvideotaskAction!batch_update_Ywvideotask.do',
				data:param,
				success:function(value){
					if(value=="success"){
						$.showMsg("操作成功！");
						doSearch();
					}else if(value=="same"){
						$.showMsg("已存在！");
					}else{
						$.showMsg("操作失败！");
					}
				}
			});
		}); 
	}
}
//设置规则
function doSetRule(){
	var row = $("#tableGrid").datagrid('getSelected');
	var selRows = $('#tableGrid').datagrid('getChecked'); 
	if(selRows.length > 1){
		$.showMsg('请选择一条记录设置规则! ');
	}else{
		if(!row || row==null){
			$.showMsg("请选择一条记录设置规则！");
			return;
		}else{
			$.ajax({
				type:'post',
				dataType : 'json',
				url:'ywvideotaskruleAction!query_Ywvideotaskrule.do',
				data:{taskid:row.taskid},
				success:function(res){
					windowId = top.$.createWin({
						title	: 	'设置规则',
						width	:  	750,
						height	: 	600,
						data    : res,
						buttons:  [{
							text:$.fn.textbox.defaults.ok,
							handler:setRule
						}],
						url	:	sysUrl+"/yw/videomanage/videotask/ywvideotaskrule/ywvideotaskrule_show.jsp?taskid="+row.taskid
					 });
				}
			});
		}
	}
}
function setRule(){
	var win		=	$.getTopWindowById(windowId);
	var jq		=	win.$;
	var model   =   $.getFormValues("childform",windowId);
	if(!win.$('#childform').form('enableValidation').form('validate')){
      return;
	}
	doSearch();
}

//初始化下拉框
function initSelect(){
	//加载下拉框
	$.ajax({
		url:" xtclAction!query_Xtcl_task.do",
		type:"post",
		dataType:"JSON",
		data : {"csdl":"ywVideoTaskType,ywVideoTaskModel,ywVideoTaskLevel,ywTaskSwitch"},
		success:function(res){
			if(res && res.length>0){
				var taskTypeArr = [{"id":"","text":"全部","selected":true}];
				var taskModelArr = [{"id":"","text":"全部","selected":true}];
				var taskLevelArr = [{"id":"","text":"全部","selected":true}];
				var taskSwitchArr = [{"id":"","text":"全部","selected":true}];
				for(i in res){
					if(res[i].csdl=="ywVideoTaskType"){
						if(res[i].csdm=="1"){
							taskTypeArr.push({"id":res[i].csdm,"text":res[i].csmc})
						}else{
							taskTypeArr.push({"id":res[i].csdm,"text":res[i].csmc})
						}
					}else if(res[i].csdl=="ywVideoTaskModel"){
						if(res[i].csdm=="1"){
							taskModelArr.push({"id":res[i].csdm,"text":res[i].csmc})
						}else{
							taskModelArr.push({"id":res[i].csdm,"text":res[i].csmc})
						}
					}else if(res[i].csdl=="ywVideoTaskLevel"){
						if(res[i].csdm=="0"){
							taskLevelArr.push({"id":res[i].csdm,"text":res[i].csmc})
						}else{
							taskLevelArr.push({"id":res[i].csdm,"text":res[i].csmc})
						}
					}else{
						if(res[i].csdm=="0"){
							taskSwitchArr.push({"id":res[i].csdm,"text":res[i].csmc})
						}else{
							taskSwitchArr.push({"id":res[i].csdm,"text":res[i].csmc})
						}
						
					}
				}
				$("#tasktype").combobox( "loadData", taskTypeArr);
				    
				$("#tsaklevel").combobox("loadData", taskLevelArr);
				$("#taskmodel").combobox("loadData", taskModelArr);
				
				$("#detectionswitch").combobox("loadData",taskSwitchArr);
				$("#detectionswitch").combobox("loadData",taskSwitchArr);
			}
		}
	});
}

//编辑
function doEdit(){
	var row = $("#tableGrid").datagrid('getSelected');
	var selRows = $('#tableGrid').datagrid('getChecked'); 
	if(selRows.length > 1){
		$.showMsg('请选择一条记录进行修改! ');
	}else{
		if(!row || row==null){
			$.showMsg("请选择一条记录进行修改！");
			return;
		}else{
			row.flag = false;
			windowId = top.$.createWin({
				title	: 	'编辑任务',
				width	:  	750,
				height	: 	600,
				data    : row,
				buttons:  [{
					text:$.fn.textbox.defaults.ok,
					handler:refreshList
				}],
				url	:	sysUrl+"/yw/videomanage/videotask/ywvideotask/ywvideotask_show.jsp"
			 });	
		}
	}
}
//增加任务
function doAddTask(){
	var row = {};
	row.flag = true;
	windowId = top.$.createWin({
		title	: 	'增加任务',
		width	:  	650,
		height	: 	400,
		data    : row,
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:refreshList
		}],
		url	:	sysUrl+"/yw/videomanage/videotask/ywvideotask/ywvideotask_show.jsp"
	 });
}
function refreshList(){
	var win		=	$.getTopWindowById(windowId);
	var jq		=	win.$;
	var model   =   $.getFormValues("childform",windowId);
	if(!win.$('#childform').form('enableValidation').form('validate')){
      return;
	}
	doSearch();
}