function PlanRun(){}
PlanRun.init = function (){
	PlanRun();
}
//开启第一个节点任务--0:重新执行，1:确认执行, 2跳过执行，3提早执行，4手动执行，5延时执行
PlanRun.startPlanFirst = function(startPlanInfo){	
	if(PlanExec.planRecord.accidentName=="未处理"){
		
	}
	if(PlanExec.planRecord.accidentName=="处理中"){	
		//暂停
		//$("#planState").val("2");
		//$('#plan_btn_pause').css('display','');
		//$('#plan_btn_stop').css('display','');
		//PlanExec.loadPlanDeviceState();
		//return;
	}
	if(PlanExec.planRecord.accidentName=="已处理"){		
		//停止
		$("#planState").val("0");
		PlanExec.loadPlanDeviceState();
		return;
	}	
	$('#currentExecStep').val("1");
	$('#currentExecLinkage').val(startPlanInfo.linkageID);
	//第一个节点--自动，手动执行
	if(startPlanInfo.execModel=="0"){
		$('#plan_btn_start').css('display','none');
		$('#plan_btn_pause').css('display','');
		$('#plan_btn_stop').css('display','');
		PlanRun.planRunStep("1","1");
	}else{
		$('#plan_btn_start').css('display','');
		//手动启动
	}
}
//预案节点运行
PlanRun.planRunStep = function(num,runType){
	if(num == PlanExec.planExecRecord.length){
		PlanRun.execPlanStopStep();
		return;
	}
	$('#currentExecStep').val(num);
	$(".block").removeClass("active");
	$("#block"+ num +"").addClass("active");
	$("#currentExecType").val(runType);	
	if(PlanExec.planExecRecord){
		var planExecModel = PlanExec.planExecRecord[(num-1)];
		//手动执行提示
		if(planExecModel.execModel=="1"&&runType=="1"){
			$("#planState").val("2");
			$.showMsg("请手动执行！");
			return;
		}
		//延时执行
		if(planExecModel.execModel=="2"){				
			if(runType=="5"){
				//延时类型5，不进入定时
			}else{
				$('#delayTime').val(planExecModel.delayTime);
				setTimeout('PlanPushlet.planDelayTime()',1000); 
				return;
			}
		}			
		//预案节点开始日志
		var operateLog = {};
		var operateCommand = "";
		if(runType=="1"){
			operateCommand = "自动执行";
		}
		if(runType=="0"){
			operateCommand = "重新执行";
		}
		if(runType=="3"){
			operateCommand = "提前执行";
		}
		if(runType=="4"){
			operateCommand = "手动执行";
		}	
		if(runType=="5"){
			operateCommand = "延时执行";
		}	
		operateLog.operateCommand = operateCommand		
		operateLog.operateModule = planExecModel.linkageDeviceName;
		operateLog.operateContent = planExecModel.linkageDeviceName+'开始'+operateCommand+'';
		operateLog.operateState = '成功';
		operateLog.deviceType = '';
		operateLog.deviceId = '';
		operateLog.deviceName = '';
		PlanExec.insert_trafficplanexeclog(operateLog);
		console.log("=预案节点=["+num+"]["+planExecModel.linkageDeviceName+"]["+operateCommand+"]");
		//所有操作按钮灰掉
		$("#btn_again_step_"+num).removeClass('disable');
		$("#btn_confirm_step_"+num).removeClass('disable');
		$("#btn_skip_step_"+num).removeClass('disable');
		$("#btn_manual_step_"+num).removeClass('disable');
		$("#btn_advance_step_"+num).removeClass('disable');
		$("#btn_again_step_"+num).addClass('disable');		
		$("#btn_confirm_step_"+num).addClass('disable');		
		$("#btn_skip_step_"+num).addClass('disable');		
		$("#btn_manual_step_"+num).addClass('disable');		
		$("#btn_advance_step_"+num).addClass('disable');
		//下一个任务是确认执行
		var nextNum = parseInt(num)+parseInt(1);
		for(var zz=nextNum;zz<PlanExec.planExecRecord.length;zz++){
			var planExecModel_next = PlanExec.planExecRecord[zz-1];
			var tmp_html = $('#plan_exec_zt_'+zz).html();
			//根据等待中找到下一个节点
			if($("#btn_confirm_step_"+zz)&&tmp_html.indexOf("等待中")!=-1){
				//手动-自动--按钮样式切换
				if(planExecModel_next.execModel=="1"){
					$("#btn_skip_step_"+zz).removeClass('disable');
					$("#btn_skip_step_"+zz).removeClass('hidden');
					$("#btn_manual_step_"+zz).removeClass('hidden');
					//$("#btn_manual_step_"+zz).addClass('hidden');
					$("#btn_advance_step_"+zz).removeClass('hidden');
					$("#btn_advance_step_"+zz).addClass('hidden');
				}
				if(planExecModel_next.execModel=="0"){
					$("#btn_skip_step_"+zz).removeClass('disable');
					$("#btn_skip_step_"+zz).removeClass('hidden');
					$("#btn_manual_step_"+zz).removeClass('hidden');
					$("#btn_manual_step_"+zz).addClass('hidden');
					$("#btn_advance_step_"+zz).removeClass('hidden');
					$("#btn_advance_step_"+zz).addClass('hidden');
				}
				break;
			}	
		}		
		//改变执行状态
		$('#plan_exec_zt_'+num).html("<span class='icon-state icon-doing' ></span>正在执行");	
		PlanRun.planRunStepDevice(num,runType);
	}
}
//预案节点第一个设备执行
PlanRun.planRunStepDevice = function(num,runType){
	var planExecModel = PlanExec.planExecRecord[(num-1)];
	var hasDevice = false;
	for(var i=0;i<PlanExec.planDeviceRecord.length;i++){
		var planDeviceModel = PlanExec.planDeviceRecord[i];
		//同一预案节点的第一个设备
		if(planDeviceModel.linkageID==planExecModel.linkageID){
			var tmpKey = "_"+planDeviceModel.linkageID+"_"+planDeviceModel.planType+"_"+planDeviceModel.execOrder;
			$('#currentExecLinkage').val(planExecModel.linkageID);
			$('#currentDeviceStep').val(tmpKey);	
			PlanRun.planDeviceStep(planDeviceModel,runType);
			hasDevice = true;
			break;			
		}
	}
	//如果预案节点下没有设备,判断节点任务失败，进入下一节点
	if(!hasDevice){
		$('#currentDeviceState').val("执行失败");
		PlanRun.execPlanNextStep(runType);
	}
}
//预案设备执行发布,调用后台及AMQ
PlanRun.planDeviceStep = function(planDeviceModel,runType){
	if($("#planState").val()=="2"){
		$.showMsg("已暂停");
		return;
	}
	if($("#planState").val()=="0"){
		$.showMsg("已停止");
		return;
	}
	var tmpKey = "_"+planDeviceModel.linkageID+"_"+planDeviceModel.planType+"_"+planDeviceModel.execOrder;
	$('#currentDeviceStep').val(tmpKey);
	if($("#plan_linkage_statu_"+tmpKey)){
		$("#plan_linkage_statu_"+tmpKey).removeClass("state-wait");
		$("#plan_linkage_statu_"+tmpKey).addClass("state-doing");	
	}
	$(".plan_linkage_li").removeClass("active");	
	$("#plan_linkage_li_"+tmpKey).addClass("active");	
	console.log("=预案设备=["+$('#currentExecStep').val()+"]["+planDeviceModel.typeName+"]["+tmpKey+"]");
	var showTitle = "";
	if(planDeviceModel.planType=="1"||planDeviceModel.planType=="2"||planDeviceModel.planType=="3"||planDeviceModel.planType=="4"){
		if(planDeviceModel.planType=="1"){
			showTitle = "诱导屏发布中！";
		}
		if(planDeviceModel.planType=="2"){
			showTitle = "指示灯发布中";
		}
		if(planDeviceModel.planType=="2"){
			showTitle = "风机发布中";
		}
		var mm = {};
		mm.senderModuleName = "trafficLinkage";
		mm.eventId = PlanExec.planRecord.eventId;
		mm.alarmCode = PlanExec.planRecord.eventType;
		mm.deviceType = planDeviceModel.planType;
		mm.deviceId = planDeviceModel.linkageDeviceId;
		mm.execState = "4";
		mm.type = runType;
		//颜色(红、黄、绿)$文字
		mm.deviceInstruction = "红$姚鑫鹏手AMQ。。。。";//设备指令；		Led情报板指令：情报板显示文字。		交通诱导灯指令：1：禁止-红灯；2：通行-绿灯；3：黄灯；4：关灯。
		mm.createAppName = runType;		
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'amqTrafficLinkageDeviceAction!publish_TrafficEventLinkageAction.do',
			data:mm,
			success:function(value){
				if(value=="success"){
					$("#requestState").val("1");
					$.showMsg(showTitle);
				}
			}
		});
	}
	if(planDeviceModel.planType=="5"){
		showTitle = "视频上墙联动中！";
		var mm = {};
		mm.senderModuleName = "trafficLinkage";
		mm.alarmId = PlanExec.planRecord.eventId;
		mm.alarmCode = PlanExec.planRecord.eventType;
		mm.deviceType = planDeviceModel.planType;
		mm.deviceId = planDeviceModel.linkageDeviceId;
		mm.execState = "4";
		mm.createAppName = runType;		
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'amqTrafficLinkageDeviceAction!publish_VideoToWall.do',
			data:mm,
			success:function(value){
				if(value=="success"){
					$("#requestState").val("1");
					$.showMsg(showTitle);
				}
			}
		});
	}
	if(planDeviceModel.planType=="7"){
		showTitle = "语音播报中！";
		var mm = {};
		mm.senderModuleName = "trafficLinkage";
		mm.eventId = PlanExec.planRecord.eventId;
		mm.alarmCode = PlanExec.planRecord.eventType;
		mm.deviceType = planDeviceModel.planType;
		mm.deviceId = planDeviceModel.linkageDeviceId;
		mm.execState = "3";
		mm.createAppName = runType;
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'amqTrafficLinkageDeviceAction!publish_TrafficEventExecState_yybb.do',
			data:mm,
			success:function(value){
				if(value=="success"){
					$("#requestState").val("1");
					$.showMsg(showTitle);
				}
			}
		});
	}
	//预案执行设备日志信息
	var operateLog = {};
	var operateCommand = "";
	if(runType=="1"){
		operateCommand = "自动执行";
	}
	if(runType=="0"){
		operateCommand = "重新执行";
	}
	if(runType=="3"){
		operateCommand = "提前执行";
	}
	if(runType=="4"){
		operateCommand = "手动执行";
	}		
	operateLog.operateCommand = operateCommand		
	operateLog.operateModule = planDeviceModel.typeName;
	operateLog.operateContent = '['+planDeviceModel.linkageDeviceName+']['+planDeviceModel.typeName+']'+operateCommand+'';
	operateLog.operateState = '执行中';
	operateLog.deviceType = '';
	operateLog.deviceId = planDeviceModel.linkageDeviceId;
	operateLog.deviceName = planDeviceModel.linkageDeviceName;
	PlanExec.insert_trafficplanexeclog(operateLog);
}
//执行下一个流程
PlanRun.execPlanNextStep = function(runType){
	var currentExecStep = $('#currentExecStep').val();
	var currentDeviceState = $('#currentDeviceState').val();
	var tmpexecState = "";
	//先判断当前预案节点执行结果状态
	if(currentDeviceState=="执行失败"){
		tmpexecState = "执行失败";
		$("#plan_exec_zt_"+currentExecStep).html("<span class='icon-state icon-fail' ></span>执行失败");
		$('#currentDeviceState').val("");
	}else{
		tmpexecState = "执行成功";
		$("#plan_exec_zt_"+currentExecStep).html("<span class='icon-state icon-success' ></span>执行成功");
	}	
	var planExecModel = PlanExec.planExecRecord[(currentExecStep-1)];
	//完成当前预案节点的日志信息
	var operateLog = {};
	operateLog.operateCommand = "执行返回"		
	operateLog.operateModule = planExecModel.linkageDeviceName;
	operateLog.operateContent = planExecModel.linkageDeviceName+''+tmpexecState+'。';
	operateLog.operateState = '成功';
	operateLog.deviceType = '';
	operateLog.deviceId = '';
	operateLog.deviceName = '';
	PlanExec.insert_trafficplanexeclog(operateLog);	
	//按钮重置，只留重新执行按钮
	$("#btn_again_step_"+currentExecStep).removeClass('hidden');
	$("#btn_again_step_"+currentExecStep).removeClass('disable');
	$("#btn_confirm_step_"+currentExecStep).addClass('hidden');
	$("#btn_skip_step_"+currentExecStep).addClass('hidden');
	$("#btn_manual_step_"+currentExecStep).addClass('hidden');
	$("#btn_advance_step_"+currentExecStep).addClass('hidden');	
	//重置，提前节点任务判断执行，第三方手动操作增加的节点任务
	var execStepList = $("#execStepList").val();
	if(!execStepList){
		execStepList = "";
	}
	if(execStepList.indexOf(";")!=-1){
		var execStepList_s = execStepList.split(";");
		if(execStepList_s&&execStepList_s.length>0){
			var tmp_step_info = execStepList_s[0];
			if(tmp_step_info.indexOf("_")!=-1){
				var tmp_step_info_s = tmp_step_info.split("_");
				if(tmp_step_info_s&&tmp_step_info_s.length>0){
					var tmp_step_num = tmp_step_info_s[0];
					var tmp_step_runType = tmp_step_info_s[1];
					execStepList = execStepList.replace(tmp_step_info+";","");
					$("#execStepList").val(execStepList);
					$('#currentExecStep').val(tmp_step_num);
					PlanRun.planRunStep(tmp_step_num,tmp_step_runType);					
				}
			}
		}
	}else{
		var hasPlan = false;
		//顺序执行预案节点任务
		for(var zz=0;zz<PlanExec.planExecRecord.length;zz++){
			var tmp_step_num = parseInt(zz)+1;
			var tmp_html = $('#plan_exec_zt_'+tmp_step_num).html();
			if($("#btn_confirm_step_"+tmp_step_num)&&tmp_html.indexOf("等待中")!=-1){
				hasPlan = true;
				$('#currentExecStep').val(tmp_step_num);					
				PlanRun.planRunStep(tmp_step_num,"1");
				break;
			}
		}
		if(!hasPlan){
			//进入停止预案
			PlanRun.execPlanStopStep();
		}
	}
}
//预案手动增加执行节点，0:重新执行，1:确认执行, 2跳过执行，3提早执行，4手动执行，5延时执行----只有0,2,3,4的节点任务
PlanRun.planExecRunType = function(num,stepType,runType){
	var execStepList = $("#execStepList").val();
	if(!execStepList){
		execStepList = "";
	}
	var tmpexecState = "";
	if(runType=="0"){
		$("#btn_again_step_"+num).removeClass('disable');
		$("#btn_again_step_"+num).addClass('disable');
		//暂停状态下，再启动
		if($("#planState").val()==("2")||$("#planState").val()==("0")){
			$("#planState").val("1");
			PlanRun.planRunStep(num,"0");
		}else{
			execStepList = execStepList+num+"_"+runType+";";
		}
	}
	if(runType=="2"){
		$("#btn_again_step_"+num).removeClass('hidden');
		$("#btn_again_step_"+num).removeClass('disable');
		$("#btn_skip_step_"+num).removeClass('disable');
		$("#btn_skip_step_"+num).addClass('disable');
		$("#plan_exec_zt_"+num).html("<span class='icon-state icon-wait' ></span>已跳过");
		$("#btn_manual_step_"+num).removeClass('hidden');
		$("#btn_manual_step_"+num).addClass('hidden');
		$("#btn_advance_step_"+num).removeClass('hidden');
		$("#btn_advance_step_"+num).addClass('hidden');
	}
	if(runType=="3"){
		$("#btn_skip_step_"+num).removeClass('disable');
		$("#btn_skip_step_"+num).addClass('disable');
		$("#plan_exec_zt_"+num).html("<span class='icon-state icon-wait' ></span>提前中");
		$("#btn_manual_step_"+num).removeClass('disable');
		$("#btn_manual_step_"+num).addClass('disable');
		$("#btn_advance_step_"+num).removeClass('disable');
		$("#btn_advance_step_"+num).addClass('disable');
		execStepList = execStepList+num+"_"+runType+";";
	}
	if(runType=="4"){
		//暂停状态下，再启动
		if($("#planState").val()==("2")||$("#planState").val()==("0")){
			$("#planState").val("1");
			PlanRun.planRunStep(num,"4");
		}else{
			$("#btn_skip_step_"+num).removeClass('disable');
			$("#btn_skip_step_"+num).addClass('disable');
			$("#plan_exec_zt_"+num).html("<span class='icon-state icon-wait' ></span>手动执行中");
			$("#btn_manual_step_"+num).removeClass('disable');
			$("#btn_manual_step_"+num).addClass('disable');
			$("#btn_advance_step_"+num).removeClass('disable');
			$("#btn_advance_step_"+num).addClass('disable');
			execStepList = execStepList+num+"_"+runType+";";
		}
	}
	$("#execStepList").val(execStepList);
	//手动增加的操作日志信息
	var planExecModel = PlanExec.planExecRecord[(num-1)];
	var operateLog = {};
	operateLog.operateCommand = "调整流程"		
	operateLog.operateModule = planExecModel.linkageDeviceName;
	operateLog.operateContent = planExecModel.linkageDeviceName+'调整'+tmpexecState+'。';
	operateLog.operateState = '成功';
	operateLog.deviceType = '';
	operateLog.deviceId = '';
	operateLog.deviceName = '';
	PlanExec.insert_trafficplanexeclog(operateLog);
}
PlanRun.planExecClose = function(){
	
}
//执行最后一个
PlanRun.execPlanStopStep = function(){
	var currentExecStep = PlanExec.planExecRecord.length;
	$('#currentExecStep').val(currentExecStep);
	$(".block").removeClass("active");
	$("#block"+ currentExecStep +"").addClass("active");
	$("#planState").val("0");
	console.log("=====进入结束预案=======");
}
//选择下一个节点
PlanRun.selectPlanNextStep = function(planDeviceModel,deviceNum,runType){
	if(deviceNum==PlanExec.planDeviceRecord.length-1){
		//最后一个设备
		PlanRun.execPlanNextStep(runType);
	}else{
		var planDeviceModel_next = PlanExec.planDeviceRecord[deviceNum+1];
		if(planDeviceModel.linkageID==planDeviceModel_next.linkageID){
			var tmpKey = "_"+planDeviceModel_next.linkageID+"_"+planDeviceModel_next.planType+"_"+planDeviceModel_next.execOrder;
			$('#currentExecLinkage').val(planDeviceModel_next.linkageID);
			$('#currentDeviceStep').val(tmpKey);
			PlanRun.planDeviceStep(planDeviceModel_next,runType);
		}else{
			PlanRun.execPlanNextStep(runType);
		}
	}
}
