//关联显示预案
function unionPlan(){
	var params = {};
	params.alarmType = planRecord.eventType;
	params.planGrade = planRecord.alarmLevel;
	params.channelId = planRecord.vdId+"-"+planRecord.channelId;
	params.page = "1";
	params.rows = "1";
	$.ajax({
		type: "post",
		url: "trafficplanmanageAction!query_Trafficplanmanage_sjgl.do",
		dataType: "json",
		data: params,
	}).done(function(res) {
		if(res.length>0){
			$('#planName').append(res[0].planName);
			//加载预案
			loadPlanInfo(res[0].planID);
		}
	});
}
//加载预案执行任务
function loadPlanInfo(planID){
	var params = {};
	params.planID = planID;
	params.page = "1";
	params.rows = "100";
	params.order = " order by linkageOrder asc ";
	$.ajax({
		type: "post",
		url: "trafficplanlinkagedeviceAction!query_Trafficplanlinkagedevice.do",
		dataType: "json",
		data: params,
	}).done(function(res) {
		var tmplinkage = new Array();
		tmplinkage.push("<ul style='width:1800px;'>");
		planExecRecord = res;
		$('#allStep').val(res.length);
		for(var x=0;x<res.length;x++){
			var tmpPlan = res[x];
			tmpPlan = changeExecModel(tmpPlan);
			tmplinkage.push(loadPlanStepDiv(tmpPlan,(1+x)));
			if(x==(res.length-1)){
				tmplinkage.push("</ul>");
				$('#planlinageDiv').append(tmplinkage.join(""));
				$('#planName').append(res[0].planName);
				loadPlanDeviceInfo(res[0].planID);
			}
		}
	});
}
//通用预案节点，以1为开始的流程
function loadPlanStepDiv(data,num){
	var tmpmsg = new Array();
	if(num==planExecRecord.length){
		tmpmsg.push('<li class="block active" id="block'+ num +'">');
		tmpmsg.push('	<div class="block-head">');
		tmpmsg.push('		<div class="block-title">'+data.linkageDeviceName+'</div>');
		tmpmsg.push('		<span class="block-mode">'+data.execModelName+'</span>');
		tmpmsg.push('		<div class="block-state" id="plan_exec_zt_'+num+'"><span class="icon-state icon-wait" ></span>等待结束</div>');
		tmpmsg.push('		<div class="block-buttonGroup">');
		tmpmsg.push('			<a class="primary-btn" onclick="planExecClose('+num+')" >结束预案</a>');
		tmpmsg.push('		</div>');
		tmpmsg.push('	</div>');
		tmpmsg.push('</li>');
	}else{
		tmpmsg.push('<li class="block active" id="block'+ num +'">');
		tmpmsg.push('	<div class="block-head">');
		tmpmsg.push('		<div class="block-title">'+data.linkageDeviceName+'</div>');
		tmpmsg.push('		<span class="block-mode">'+data.execModelName+'</span>');
		//tmpmsg.push('		<div id="execType_'+num+'" class="hidden" ></span>');
		tmpmsg.push('		<div class="block-state" id="plan_exec_zt_'+num+'"><span class="icon-state icon-wait" ></span>等待中</div>');
		tmpmsg.push('		<div class="block-buttonGroup">');
		tmpmsg.push('			<a id="btn_again_step_'+num+'" class="primary-btn hidden" onclick="planExecRunType('+num+','+data.linkageDeviceType+',0)" >重新执行</a>');
		tmpmsg.push('			<a id="btn_confirm_step_'+num+'" class="primary-btn hidden" onclick="planExecRunType('+num+','+data.linkageDeviceType+',1)" >确认执行</a>');
		tmpmsg.push('			<a id="btn_manual_step_'+num+'" class="primary-btn hidden" onclick="planExecRunType('+num+','+data.linkageDeviceType+',4)" >手动执行</a>');
		tmpmsg.push('			<a id="btn_skip_step_'+num+'" class="primary-btn " onclick="planExecRunType('+num+','+data.linkageDeviceType+',2)" >跳过执行</a>');		
		tmpmsg.push('			<a id="btn_advance_step_'+num+'" class="primary-btn " onclick="planExecRunType('+num+','+data.linkageDeviceType+',3)" >提前执行</a>');
		tmpmsg.push('		</div>');
		tmpmsg.push('	</div>');
		tmpmsg.push('	<div class="block-con">');
		tmpmsg.push('		<ul id="plan_device_div_'+data.linkageID+'" >');
		tmpmsg.push('		</ul>');
		tmpmsg.push('	</div>');
		tmpmsg.push('</li>');
	}
	return tmpmsg.join("");
}
////加载预案设备
function loadPlanDeviceInfo(planID){
	var params = {};
	params.planID = planID;
	params.page = "1";
	params.rows = "100";
	params.order = " order by linkageId,execOrder asc ";
	$.ajax({
		type: "post",
		url: "trafficplanexecmodeAction!query_Trafficplanexecmode.do",
		dataType: "json",
		data: params,
	}).done(function(res) {
		var deviceHtml = "";
		planDeviceRecord = res;
		for(var x=0;x<res.length;x++){
			var tmpplanDevice = res[x];
			deviceHtml = loadPlanDeviceDiv(tmpplanDevice);
			if($('#plan_device_div_'+tmpplanDevice.linkageID)){
				$('#plan_device_div_'+tmpplanDevice.linkageID).append(deviceHtml);
			}else{
				console.log("===无挂载节点问题======");
			}
			if(x==(res.length-1)){
				startPlanFirst(res[0]);
			}
		}
	});
}
//
function loadPlanDeviceDiv(data){
	data = changeExecModel(data);
	var tmpkey = "_"+data.linkageID+"_"+data.planType+"_"+data.execOrder;
	var tmpmsg = new Array();
	tmpmsg.push('			<li class="plan_linkage_li" id="plan_linkage_li_'+tmpkey+'">');
	tmpmsg.push('				<span class="list-num">'+data.execOrder+'</span>');
	tmpmsg.push('				<span class="list-con">'+data.linkageDeviceName+data.messageContent+'</span>');
	tmpmsg.push('				<span class="list-state state-wait" id="plan_linkage_statu_'+tmpkey+'"></span>');
	tmpmsg.push('			</li>');	
	return tmpmsg.join("");
}
function startPlanFirst(startPlanInfo){	
	$('#currentExecStep').val("1");
	$('#currentExecLinkage').val(startPlanInfo.linkageID);
	//自动，手动执行
	if(startPlanInfo.execModel=="0"){
		//第一个节点--0:重新执行，1:确认执行, 2跳过执行，3提早执行，4手动执行
		planExecStep("1","1");
	}else{
		//$('#plan_exec_zt_'+currentStepStat).html(data.linkageDeviceName+":正在执行");
	}
}
//
function planExecStep(num,runType){
	console.log("====planExecStep==num==="+num+"=runType="+runType);
	$(".block").removeClass("active");
	$("#block"+ num +"").addClass("active");
	$("#currentExecType").val(runType);
	//$("#execType_"+num).val(runType);	
	if(planExecRecord){
		var planExecModel = planExecRecord[(num-1)];
		//自动+手动
		if(planExecModel.execModel=="1"&&runType=="1"){
			$.showMsg("请手动执行！");
			return;
		}
		//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>正在执行");	
		
		var currentDeviceStep = $('#currentDeviceStep').val();
		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;
				if(currentDeviceStep==tmpKey){
					planDeviceStep(planDeviceModel,runType);
					break;
				}
				if(!currentDeviceStep){
					$('#currentDeviceStep').val(tmpKey);
					planDeviceStep(planDeviceModel,runType);
					break;
				}
			}
		}
	}
}
function planDeviceStep(planDeviceModel,runType){
	console.log("=planDeviceStep==planDeviceModel="+planDeviceModel+"=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);
				}
			}
		});
	}
}
//执行下一个流程
function execPlanNextStep(runType){
	console.log("======执行方式==="+runType);
	var currentExecStep = $('#currentExecStep').val();
	//进入下一个STEP
	var currentDeviceState = $('#currentDeviceState').val();
	if(currentDeviceState=="执行失败"){
		$("#plan_exec_zt_"+currentExecStep).html("<span class='icon-state icon-fail' ></span>执行失败");
		$('#currentDeviceState').val("");
	}else{
		$("#plan_exec_zt_"+currentExecStep).html("<span class='icon-state icon-success' ></span>执行成功");
	}
	//只有重新执行按钮
	$("#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 = "";
	}
	//进入重置，提前的节点任务
	console.log("==execStepList=1111="+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);
					for(var i=0;i<planDeviceRecord.length;i++){
						var planDeviceModel = planDeviceRecord[i];
						//同一节点下
						if(planDeviceModel.linkageID==planExecRecord[tmp_step_num-1].linkageID){
							var tmpKey = "_"+planDeviceModel.linkageID+"_"+planDeviceModel.planType+"_"+planDeviceModel.execOrder;
							$('#currentExecLinkage').val(planDeviceModel.linkageID);
							$('#currentDeviceStep').val(tmpKey);
							break;
						}
					}
					planExecStep(tmp_step_num,tmp_step_runType);
				}
			}
		}
	}else{
		if(runType=="0"||runType=="3"){
			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){
					//
					$('#currentExecStep').val(tmp_step_num);
					for(var i=0;i<planDeviceRecord.length;i++){
						var planDeviceModel = planDeviceRecord[i];
						//同一节点下
						if(planDeviceModel.linkageID==planExecRecord[tmp_step_num-1].linkageID){
							var tmpKey = "_"+planDeviceModel.linkageID+"_"+planDeviceModel.planType+"_"+planDeviceModel.execOrder;
							$('#currentExecLinkage').val(planDeviceModel.linkageID);
							$('#currentDeviceStep').val(tmpKey);
							break;
						}
					}
					planExecStep(tmp_step_num,runType);
					break;
				}
			}
		}else{
			//顺序执行
			currentExecStep = parseInt(1)+parseInt(currentExecStep);
			$('#currentExecStep').val(currentExecStep);
			//新开任务几点
			for(var i=0;i<planDeviceRecord.length;i++){
				var planDeviceModel = planDeviceRecord[i];
				//同一节点下
				if(planDeviceModel.linkageID==planExecRecord[currentExecStep-1].linkageID){
					var tmpKey = "_"+planDeviceModel.linkageID+"_"+planDeviceModel.planType+"_"+planDeviceModel.execOrder;
					$('#currentExecLinkage').val(planDeviceModel.linkageID);
					$('#currentDeviceStep').val(tmpKey);
					break;
				}
			}
			planExecStep(currentExecStep,"1");
		}
	}
}
function planExecClose(){
	
}
function planExecRunType(num,stepType,runType){
	var execStepList = $("#execStepList").val();
	if(!execStepList){
		execStepList = "";
	}
	//runType 0:重新执行，1:确认执行, 2跳过执行，3提早执行，4手动执行
	if(runType=="0"){
		$("#btn_again_step_"+num).removeClass('disable');
		$("#btn_again_step_"+num).addClass('disable');
		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"){
		$("#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);
}