//开启第一个节点任务
function startPlanFirst(startPlanInfo){	
	$('#currentExecStep').val("1");
	$('#currentExecLinkage').val(startPlanInfo.linkageID);
	//自动，手动执行
	if(startPlanInfo.execModel=="0"){
		//第一个节点--0:重新执行，1:确认执行, 2跳过执行，3提早执行，4手动执行，5延时执行
		planExecStep("1","1");
	}else{
		//$('#plan_exec_zt_'+currentStepStat).html(data.linkageDeviceName+":正在执行");
	}
}
//
function planExecStep(num,runType){
	console.log("=========planExecStep========="+num);
	if(num == planExecRecord.length){
		execPlanStopStep();
		return;
	}
	$('#currentExecStep').val(num);
	$(".block").removeClass("active");
	$("#block"+ num +"").addClass("active");
	$("#currentExecType").val(runType);
	if(planExecRecord){
		var planExecModel = planExecRecord[(num-1)];
		//自动+手动
		if(planExecModel.execModel=="1"&&runType=="1"){
			$("#planState").val("2");
			$.showMsg("请手动执行！");
			return;
		}
		if(planExecModel.execModel=="2"){	
			if(runType=="5"){
				//延时过来
			}else{
				$('#delayTime').val(planExecModel.delayTime);
				setTimeout('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 = '';
		insert_trafficplanexeclog(operateLog);
		//
		//type 0:重新执行，1:确认执行, 2跳过执行，3提早执行，4手动执行
		$("#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);
		//if(nextNum<planExecRecord.length);
		for(var zz=nextNum;zz<planExecRecord.length;zz++){
			var planExecModel_next = planExecRecord[zz-1];
			var tmp_html = $('#plan_exec_zt_'+zz).html();
			if($("#btn_confirm_step_"+zz)&&tmp_html.indexOf("等待中")!=-1){
				//console.log("=tmp_html="+tmp_html+"===execModel=="+planExecModel_next.execModel);
				//console.log(planExecModel_next);
				//手动---有手动，跳动
				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>正在执行");	
		planExecStepDevice(num,runType);
	}
}
//任务节点下的设备
function planExecStepDevice(num,runType){
	//console.log("==planExecStepDevice=planExecStepDevice="+num);
	var planExecModel = planExecRecord[(num-1)];
	for(var i=0;i<planDeviceRecord.length;i++){
		var planDeviceModel = planDeviceRecord[i];
		//同一节点下
		if(planDeviceModel.linkageID==planExecModel.linkageID){
			var tmpKey = "_"+planDeviceModel.linkageID+"_"+planDeviceModel.planType+"_"+planDeviceModel.execOrder;
			$('#currentExecLinkage').val(planExecModel.linkageID);
			$('#currentDeviceStep').val(tmpKey);			
			planDeviceStep(planDeviceModel,runType);
			break;			
		}
	}
}
function planDeviceStep(planDeviceModel,runType){
	//console.log("=planDeviceStep==planDeviceModel="+planDeviceModel.linkageID+"=runType="+runType);
	if($("#planState").val()=="2"){
		$.showMsg("已暂停");
		return;
	}
	if($("#planState").val()=="0"){
		$.showMsg("已停止");
		return;
	}
	var tmpKey = "_"+planDeviceModel.linkageID+"_"+planDeviceModel.planType+"_"+planDeviceModel.execOrder;
	//console.log("======planDeviceStep======tmpKey="+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");		
	var showTitle = "";
	if(planDeviceModel.planType=="1"||planDeviceModel.planType=="2"){
		if(planDeviceModel.planType=="1"){
			showTitle = "诱导屏发布中！";
		}
		if(planDeviceModel.planType=="2"){
			showTitle = "指示灯发布中";
		}
		var mm = {};
		mm.senderModuleName = "trafficLinkage";
		mm.eventId = planRecord.eventId;
		mm.alarmCode = planRecord.eventType;
		mm.deviceType = planDeviceModel.planType;
		mm.deviceId = planDeviceModel.linkageDeviceId;
		mm.execState = "4";
		mm.type = runType;
		mm.deviceInstruction = "";//设备指令；		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=="3"){
		showTitle = "视频上墙联动中！";
		var mm = {};
		mm.senderModuleName = "trafficLinkage";
		mm.alarmId = planRecord.eventId;
		mm.alarmCode = 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=="5"){
		showTitle = "语音播报中！";
		var mm = {};
		mm.senderModuleName = "trafficLinkage";
		mm.eventId = planRecord.eventId;
		mm.alarmCode = 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;
	insert_trafficplanexeclog(operateLog);
}

//执行下一个流程
function execPlanNextStep(runType){
	var currentExecStep = $('#currentExecStep').val();
	//进入下一个STEP
	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 = planExecRecord[(currentExecStep-1)];
	var operateLog = {};
	operateLog.operateCommand = "执行返回"		
	operateLog.operateModule = planExecModel.linkageDeviceName;
	operateLog.operateContent = planExecModel.linkageDeviceName+''+tmpexecState+'。';
	operateLog.operateState = '成功';
	operateLog.deviceType = '';
	operateLog.deviceId = '';
	operateLog.deviceName = '';
	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+";","");
					//console.log("==execStepList=2222="+execStepList+"=tmp_step_num="+tmp_step_num+"=tmp_step_runType="+tmp_step_runType);
					$("#execStepList").val(execStepList);
					$('#currentExecStep').val(tmp_step_num);
					planExecStep(tmp_step_num,tmp_step_runType);					
				}
			}
		}
	}else{
		var hasPlan = false;
		for(var zz=0;zz<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);					
				planExecStep(tmp_step_num,"1");
				break;
			}
		}
		if(!hasPlan){
			execPlanStopStep();
		}
	}
}
function planExecClose(){
	
}

function planExecRunType(num,stepType,runType){
	var execStepList = $("#execStepList").val();
	if(!execStepList){
		execStepList = "";
	}
	//runType 0:重新执行，1:确认执行, 2跳过执行，3提早执行，4手动执行
	var tmpexecState = "";
	if(runType=="0"){
		$("#btn_again_step_"+num).removeClass('disable');
		$("#btn_again_step_"+num).addClass('disable');
		execStepList = execStepList+num+"_"+runType+";";
		//暂停状态下，再启动
		if($("#planState").val()==("2")||$("#planState").val()==("0")){
			$("#planState").val("1");
			planExecStep(num,"0");
		}
	}
	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");
			planExecStep(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 = planExecRecord[(num-1)];
	var operateLog = {};
	operateLog.operateCommand = "调整流程"		
	operateLog.operateModule = planExecModel.linkageDeviceName;
	operateLog.operateContent = planExecModel.linkageDeviceName+'调整'+tmpexecState+'。';
	operateLog.operateState = '成功';
	operateLog.deviceType = '';
	operateLog.deviceId = '';
	operateLog.deviceName = '';
	insert_trafficplanexeclog(operateLog);
}