//
$(window).ready(function(){
	PlanExec.init();
	PlanRun.init();
	PlanHtml.init();
	PlanPushlet.init();
});
function PlanExec(){};
PlanExec.init = function (){
	//console.log("===accidentId==="+accidentId);
	PlanExec();
	PlanExec.planRecord ={};
	PlanExec.planRecord.eventId = "1";
	PlanExec.planRecord.eventType = "1";
	PlanExec.planExecRecord;
	PlanExec.lastLinkage;
	var planID = PlanExec.getUrlParam("planID");
	PlanExec.deviceMap = new Map();
	PlanExec.stepMap = new Map();
	//console.log("=PlanExec==planID======"+planID);
	PlanExec.loadPlanInfo(planID);
}
//加载预案执行任务
PlanExec.loadPlanInfo = function (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;'>");		
		var tmplinkageStep = new Array();		
		PlanExec.planExecRecord = res;
		$('#allStep').val(res.length);
		for(var x=0;x<res.length;x++){
			var tmpPlan = res[x];
			tmpPlan = PlanExec.changeExecModel(tmpPlan);
			PlanExec.stepMap.put(tmpPlan.linkageID,tmpPlan);
			tmplinkage.push(PlanHtml.loadPlanDivNewTop(tmpPlan,(1+x)));
			tmplinkageStep.push(PlanHtml.loadPlanStepNewStep(tmpPlan,(1+x)));			
			if(x==(res.length-1)){
				//tmplinkage.push("</ul>");
				PlanExec.lastLinkageID = tmpPlan.linkageID;
				$('#plan_jd_div').append(tmplinkage.join(""));
				$('#planLinkageStepDiv').append(tmplinkageStep.join(""));				
				PlanExec.loadPlanDeviceInfo(res[0].planID);
			}
		}
	});
}
//加载预案设备
PlanExec.loadPlanDeviceInfo = function(planID){
	var params = {};
	params.planID = planID;
	params.page = "1";
	params.rows = "200";
	$.ajax({
		type: "post",
		url: "trafficplanexecmodeAction!query_Trafficplanexecmode_plan.do",
		dataType: "json",
		data: params,
	}).done(function(res) {
		var deviceHtml = "";
		PlanExec.planDeviceRecord = res;
		for(var x=0;x<res.length;x++){
			var tmpplanDevice = res[x];
			PlanExec.deviceMap.put(tmpplanDevice.linkageDeviceId,tmpplanDevice);
			//deviceHtml = PlanHtml.loadPlanDeviceDivNew(tmpplanDevice,x);
			deviceHtml = PlanHtml.loadPlanDeviceDivNew(tmpplanDevice);
			if($('#plan_device_div_'+tmpplanDevice.linkageID)){
				$('#plan_device_div_'+tmpplanDevice.linkageID).append(deviceHtml);
			}else{
				//console.log("===预案设备无法挂载节点======");
			}			
			if(x==(res.length-1)){
				PlanRun.startPlanFirst(res);
			}
		}
	});
}
//修改预案执行模式名称
PlanExec.changeExecModel = function(data){
	if(data.execModel=="0"){
		data.execModelName = "自";
		data.execModelNameAll = "自动执行";
	}
	if(data.execModel=="1"){
		data.execModelName = "手";
		data.execModelNameAll = "手动执行";
	}
	if(data.execModel=="2"){
		data.execModelName = "延";
		data.execModelNameAll = "延时执行";
	}
	return data;
}
//插入日志
PlanExec.insert_trafficplanexeclog = function(operateLog){
	//operateLog.createAccount = window.opener.top.MainPage.yhmc;
	operateLog.operateTime = PlanExec.formatDate(new Date(),"yyyy-MM-dd hh:mm:ss");
	$.ajax({
		type:'post',
		dataType : 'json',
		url:'trafficplanexeclogAction!insert_Trafficplanexeclog.do',
		data:operateLog,
		success:function(flag){			 
		}
	});
}
PlanExec.getUrlParam = function(name){
  	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");    
  	var search = window.location.search.substr(1);
  	var href = window.location.href;
  	if(href.indexOf("?")>=0){
  		search = href.substr(href.indexOf("?")+1);
  	}
  	var r = search.match(reg);    
  	if (r != null) return decodeURI(r[2]); return null;    
}
PlanExec.formatDate = function(value,format){
	format 	= format || 'yyyy-MM-dd';
	value 	= value  || new Date(); 
	try{
		return value.format(format);
	}catch(e){
		return null;
	}
}
//1运行--2暂停--0停止
//启动
PlanExec.planStart = function(){
	$("#planState").val("1");
}
//暂停
PlanExec.planPause = function(){
	$("#planState").val("2");
	$.showMsg("已暂停！");
}
//继续执行
PlanExec.planNext = function(){
	$("#planState").val("1");
}
//手动执行
PlanExec.planManual = function(){
	$("#planState").val("1");
}
//终止
PlanExec.planStop = function(){
	
	//关闭隧道实景图
//	parent.tunnelPlanClose();
	$("#planState").val("0");
	console.log("==planStopplanStop==");
	
	
	var params = {};
	params.accidentState = 4;
	params.accidentId = PlanExec.getUrlParam("accidentID");
	console.log('--结束预案--params-----',params);
	$.ajax({
		type: "post",
		url: "trafficaccidentinfoAction!update_Trafficaccidentinfo_plan.do",
		dataType: "json",
		data: params,
	}).done(function(res) {
//		var alarmIndex = PlanExec.getUrlParam("alarmListIndex");
		//从告警事件列表中删除该条告警记录
//		parent.BaseData.alarmList.splice(alarmIndex, 1);
//		reloadIndex();
		top.$.showMsg("预案结束");
	});
	
	
}
function eventTo122(){
	console.log("==eventTo122==");
}
function eventTo120(){
	console.log("==eventTo120==");
}
function eventTo119(){
	console.log("==eventTo119==");
}

function eventNext(){
	console.log("==eventNext==");
}
function eventUp(){
	console.log("==eventUp==");
}
function eventClose(){
	console.log("==eventClose==");
	$('#eventShowDiv').css('display','none');
}

function planOneVoice(){
	console.log("==planOneVoice==");
}

function planOneLight(){
	console.log("==planOneLight==");
}

function planOneCloud(){
	console.log("==planOneCloud==");
}

function changeShowDevice(num){	
	$('#plan_device_div_'+num).slideToggle();
}

