//关联显示预案
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 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" style="display:none;" onclick="planExecStep('+num+',0)" >重新执行</a>');
		tmpmsg.push('			<a class="primary-btn" style="display:none;" onclick="planExecStep('+num+',1)" >确认执行</a>');
		tmpmsg.push('			<a class="primary-btn" onclick="planExecStep('+num+',2)" >跳过执行</a>');
		tmpmsg.push('			<a class="primary-btn" onclick="planExecStep('+num+',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('			<li>');
		tmpmsg.push('				<span class="list-num">1</span>');
		tmpmsg.push('				<span class="list-con">发考生加分加分大师罚款拉风卡看到凤凰歌剧院但是价格很高</span>');
		tmpmsg.push('				<span class="list-state state-success"></span>');
		tmpmsg.push('			</li>');
		tmpmsg.push('			<li>');
		tmpmsg.push('				<span class="list-num">2</span>');
		tmpmsg.push('				<span class="list-con">发考生加分加分大师罚款拉风卡看到凤凰歌剧院但是价格很高</span>');
		tmpmsg.push('				<span class="list-state state-fail"></span>');
		tmpmsg.push('			</li>');
		tmpmsg.push('			<li>');
		tmpmsg.push('				<span class="list-num">3</span>');
		tmpmsg.push('				<span class="list-con">发考生加分加分大师罚款拉风卡看到凤凰歌剧院但是价格很高</span>');
		tmpmsg.push('				<span class="list-state state-success"></span>');
		tmpmsg.push('			</li>');
		*/
		tmpmsg.push('		</ul>');
		tmpmsg.push('	</div>');
		tmpmsg.push('</li>');
	}
	/*
	var tmpmsg = new Array();
	//tmpmsg.push("<div class='container'>");
	tmpmsg.push("	<div class='linkageMenu' style='border:1px solid #ff00ff;'>");
	tmpmsg.push("		<div class='linkageMenu-top1'>");
	tmpmsg.push("			<div class='linkageMenu-top1-title'>");
	tmpmsg.push("				<div class='linkageMenu-top1-tb1'>");
	if(data.execModel=="0"){
		tmpmsg.push("					<img src='image/state-zd.png'/>");
	}else{
		tmpmsg.push("					<img src='image/state-sd.png'/>");
	}
	tmpmsg.push("				</div>");
	tmpmsg.push("			</div>");
	tmpmsg.push("			<div class='linkageMenu-top1-state' >");
	tmpmsg.push(data.linkageDeviceName+":<span id='plan_exec_zt_"+num+"'>等待中</span>");
	//语音播报，已完成
	tmpmsg.push("			</div>");
	tmpmsg.push("			<div class='linkageMenu-top3'>");
	tmpmsg.push("		  		<ul class='linkageMenu-control-button'>");			
	tmpmsg.push("		  			<li id='plan_exec_zx_"+num+"' class='linkageMenu-control-active' style='display:none;' onclick='planExecStep("+num+",0)' >重新执行</li>");
	tmpmsg.push("		  			<li id='plan_exec_qr_"+num+"' class='linkageMenu-control-active' style='display:none;' onclick='planExecStep("+num+",1)' >确认执行</li>");
	tmpmsg.push("		  			<li id='plan_exec_tg_"+num+"' class='linkageMenu-control-active' onclick='planExecStep("+num+",2)' >跳过执行</li>");
	tmpmsg.push("		  			<li id='plan_exec_tq_"+num+"' class='linkageMenu-control-active' onclick='planExecStep("+num+",3)' >提前执行</li>");
	tmpmsg.push("		  		</ul>");
	tmpmsg.push("		  	</div>");
	tmpmsg.push("		</div>");
	tmpmsg.push("		<div class='linkageMenu-top2' id='plan_device_div_"+data.linkageID+"' >");
	//tmpmsg.push("		<div id='plan_device_div_"+data.linkageID+"'></div>");	
	//该列请自由发挥！
	tmpmsg.push("		</div>");
	tmpmsg.push("		<div style='clear:both'></div>");
	tmpmsg.push("	</div>");
	//tmpmsg.push("	<div class='linkageMenu'>");
	//tmpmsg.push("		<div style='clear:both'></div>");
	//tmpmsg.push("	</div>");
	//tmpmsg.push("</div>");
	*/
	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>');
	/*
	tmpmsg.push("<div>"+data.typeName);
	tmpmsg.push("	<div id='plan_linkage_"+tmpkey+"' >");
	tmpmsg.push("		<div id='plan_linkage_deviceId_"+tmpkey+"' >"+data.linkageDeviceId+"</div>");
	tmpmsg.push("		<div id='plan_linkage_deviceName_"+tmpkey+"' >"+data.linkageDeviceName+"</div>");
	tmpmsg.push("		<div id='plan_linkage_message_"+tmpkey+"' >"+data.messageContent+"</div>");
	tmpmsg.push("		<div id='plan_linkage_runmode_"+tmpkey+"' >"+data.execModelName+"</div>");
	tmpmsg.push("		<div id='plan_linkage_statu_"+tmpkey+"' >等待中</div>");	
	tmpmsg.push("	</div>");
	tmpmsg.push("</div>");
	*/
	return tmpmsg.join("");
}
function startPlanFirst(startPlanInfo){	
	$('#currentExecStep').val("1");
	$('#currentExecLinkage').val(startPlanInfo.linkageID);
	//自动，手动执行
	if(startPlanInfo.execModel=="0"){
		//第一个节点--1:确认执行, 2提早执行，3跳过执行，4重新执行,5手动执行
		planExecStep("1","1");
	}else{
		//$('#plan_exec_zt_'+currentStepStat).html(data.linkageDeviceName+":正在执行");
	}
}
function planExecStep(num,type){
	$(".block").removeClass("active");
	$("#block"+ num +"").addClass("active");
	$("#execType").val(type);
	//planExecStep
	if(planExecRecord){
		var planExecModel = planExecRecord[(num-1)];
		//自动+手动
		if(planExecModel.execModel=="0"||type=="5"){
		
		}else{
			$.showMsg("请手动执行！");
			return;
		}
		//改变状态
		$('#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,type);
					break;
				}
				if(!currentDeviceStep){
					$('#currentDeviceStep').val(tmpKey);
					planDeviceStep(planDeviceModel,type);
					break;
				}
			}
		}
	}
}
//type类型，表示0：确认执行，提早，跳过，重新执行-0
function planDeviceStep(planDeviceModel,type){
	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 = type;
		mm.deviceInstruction = "";//设备指令；		Led情报板指令：情报板显示文字。		交通诱导灯指令：1：禁止-红灯；2：通行-绿灯；3：黄灯；4：关灯。
		mm.createAppName = type;		
		$.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 = type;		
		$.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 = "4";
		mm.createAppName = type;
		$.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(planDeviceModel,planDeviceModel_next){
	console.log("===execPlanNextStep==start=tmp_planDeviceInfo.linkageID="+planDeviceModel.linkageID+"==="+planDeviceModel_next.linkageID);
	var currentExecStep = $('#currentExecStep').val();
	var tmpKey = "_"+planDeviceModel_next.linkageID+"_"+planDeviceModel_next.planType+"_"+planDeviceModel_next.execOrder;
	$('#currentExecLinkage').val(planDeviceModel_next.linkageID);
	$('#currentDeviceStep').val(tmpKey);
	if(planDeviceModel_next.linkageID==planDeviceModel.linkageID){
		//本任务还有下一个,设备任务
		//自动
		if(planDeviceModel_next.execModel=="0"){
			planDeviceStep(planDeviceModel_next,"0");
		}else{
			$.showMsg("请手动执行！");
		}
	}else{
		//进入下一个STEP
		var currentDeviceState = $('#currentDeviceState').val();
		if(currentDeviceState=="执行失败"){
			$("#plan_exec_zt_"+currentExecStep).html("<span class='icon-state icon-fail' ></span>执行失败");
			$('#currentDeviceState').val("");
		}
		currentExecStep = parseInt(1)+parseInt(currentExecStep);
		$('#currentExecStep').val(currentExecStep);
		//新开任务几点
		if(planDeviceModel_next.execModel=="0"){
			planExecStep(currentExecStep,"1");
		}else{
			$.showMsg("请手动执行！");
		}
	}
}
//执行最后一个
function execPlanStopStep(planDeviceModel){
	var currentDeviceState = $('#currentDeviceState').val();
	var currentExecStep = $('#currentExecStep').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>执行成功");
	}
	currentExecStep = parseInt(1)+parseInt(currentExecStep);
	$('#currentExecStep').val(currentExecStep);
	console.log("=====execPlanStopStep=======");
}
function planExecClose(){
	
}