//通用设备执行
function loadPlanExecDiv(data,num){
	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='planExec("+num+",0)' >重新执行</li>");
	tmpmsg.push("		  			<li id='plan_exec_qr_"+num+"' class='linkageMenu-control-active' style='display:none;' onclick='planExec("+num+",1)' >确认执行</li>");
	tmpmsg.push("		  			<li id='plan_exec_tg_"+num+"' class='linkageMenu-control-active' onclick='planExec("+num+",2)' >跳过执行</li>");
	tmpmsg.push("		  			<li id='plan_exec_tq_"+num+"' class='linkageMenu-control-active' onclick='planExec("+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 unionPlan(){
	var params = {};
	params.alarmType = planRecords.eventType;
	params.planGrade = planRecords.alarmLevel;
	params.channelId = planRecords.vdId+"-"+planRecords.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);
			loadPlanExecAction(res[0].planID);
		}
	});
}
//加载预案执行任务
function loadPlanExecAction(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();
		planExecRecord = res;
		$('#allExecStep').val(res.length);
		for(var x=0;x<res.length;x++){
			var tmpplan = res[x];
			tmpplan = changeExecModel(tmpplan);
			tmplinkage.push(loadPlanExecDiv(tmpplan,(1+x)));
			if(x==(res.length-1)){
				$('#planlinageDiv').append(tmplinkage.join(""));
				$('#planName').append(res[0].planName);
				loadPlanDeviceAction(res[0].planID);
			}
		}
	});
}
////加载预案设备
function loadPlanDeviceAction(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("===error======");
			}
			if(x==(res.length-1)){
				startPlanExec();
			}
		}
	});
}

function loadPlanDeviceDiv(data){
	data = changeExecModel(data);
	var tmpkey = "_"+data.linkageID+"_"+data.planType+"_"+data.execOrder;
	var tmpmsg = new Array();
	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 startPlanExec(){
	var currentExecStep = $('#currentExecStep').val();
	if(!currentExecStep){
		currentExecStep = 1;
		$('#currentExecStep').val(currentExecStep);
		var tmpplanexec = planExecRecord[(currentExecStep-1)];
		if(tmpplanexec){
			$('#currentExecLinkage').val(tmpplanexec.linkageID);
			//自动，手动执行
			if(tmpplanexec.execModel=="0"){
				//确认执行
				planExec((currentExecStep),'1');
			}else{
				//$('#plan_exec_zt_'+currentStepStat).html(data.linkageDeviceName+":正在执行");
			}
		}
	}
}
function planExec(num,type){
	if(planExecRecord){
		var planExecModel = planExecRecord[(num-1)];
		//改变状态
		$('#plan_exec_zt_'+num).html(planExecModel.linkageDeviceName+":正在执行");
		var currentDeviceStep = $('#currentDeviceStep').val();
		for(var i=0;i<planDeviceRecord.length;i++){
			var planDeviceInfo = planDeviceRecord[i];
			var tmpStep = planDeviceInfo.linkageID+"_"+planDeviceInfo.execOrder;
			if(planDeviceInfo.linkageID==planExecModel.linkageID){
				if(currentDeviceStep==tmpStep){
					planExecDevice(planDeviceInfo,'0');
					break;
				}
				if(!currentDeviceStep){
					currentDeviceStep = (tmpStep);
					planExecDevice(planDeviceInfo,'0');
					break;
				}
			}
		}
		$('#currentDeviceStep').val(currentDeviceStep);
	}
}

function planExecDevice(planDeviceInfo,type){
	var tmpkey = "_"+planDeviceInfo.linkageID+"_"+planDeviceInfo.planType+"_"+planDeviceInfo.execOrder;
	if($("#plan_linkage_statu_"+tmpkey)){
		$("#plan_linkage_statu_"+tmpkey).html("执行中");	
	}
	var showTitle = "";
	if(planDeviceInfo.planType=="1"||planDeviceInfo.planType=="2"){
		if(planDeviceInfo.planType=="1"){
			showTitle = "诱导屏发布中！";
		}
		if(planDeviceInfo.planType=="2"){
			showTitle = "指示灯发布中";
		}
		var mm = {};
		mm.senderModuleName = "trafficLinkage";
		mm.eventId = planRecords.eventId;
		mm.alarmCode = planRecords.eventType;
		mm.deviceType = planDeviceInfo.planType;
		mm.deviceId = planDeviceInfo.linkageDeviceId;
		mm.execState = "4";
		mm.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(planDeviceInfo.planType=="3"){
		showTitle = "视频上墙联动中！";
		var mm = {};
		mm.senderModuleName = "trafficLinkage";
		mm.alarmId = planRecords.eventId;
		mm.alarmCode = planRecords.eventType;
		mm.deviceType = planDeviceInfo.planType;
		mm.deviceId = planDeviceInfo.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(planDeviceInfo.planType=="5"){
		showTitle = "语音播报中！";
		var mm = {};
		mm.senderModuleName = "trafficLinkage";
		mm.eventId = planRecords.eventId;
		mm.alarmCode = planRecords.eventType;
		mm.deviceType = planDeviceInfo.planType;
		mm.deviceId = planDeviceInfo.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);
				}
			}
		});
	}
	
}
