var winId	= $.getUrlParam("winId");
var planRecord = top.$("#"+winId).panel("options").data;
var planExecRecord;
var planDeviceRecord;
$(function(){
	$("#requestTime").val(0);
	//planRecord处理优化
	//可以根据ID加载planRecord
	initDefault();	
	initPushLet();
});
//关联显示预案
function initDefault(){
	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){
			planRecord.planID =  res[0].planID;
			planRecord.planName =  res[0].planName;
			//加载预案
			var operateLog = {};
			operateLog.operateCommand = '关联预案';
			operateLog.operateModule = '';
			operateLog.operateContent = planRecord.alarmLevelName +planRecord.eventName+'关联'+res[0].planName+'成功';
			operateLog.operateState = '成功';
			operateLog.deviceType = '';
			operateLog.deviceId = planRecord.vdId+"-"+planRecord.channelId;
			operateLog.deviceName = '';
			insert_trafficplanexeclog(operateLog);
			
			operateLog = {};
			operateLog.operateCommand = '启动预案';
			operateLog.operateModule = '';
			operateLog.operateContent = planRecord.alarmLevelName +planRecord.eventName+'启动'+res[0].planName+'成功';
			operateLog.operateState = '成功';
			operateLog.deviceType = '';
			operateLog.deviceId = planRecord.vdId+"-"+planRecord.channelId;
			operateLog.deviceName = '';
			insert_trafficplanexeclog(operateLog);
			
			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(""));
				loadPlanDeviceInfo(res[0].planID);
			}
		}
	});
}
//加载预案设备
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 changeExecModel(data){
	if(data.execModel=="0"){
		data.execModelName = "自";
	}
	if(data.execModel=="1"){
		data.execModelName = "手";
	}
	if(data.execModel=="2"){
		data.execModelName = "延";
	}
	return data;
}
//启动
function planStart(){
	//运行
	$("#planState").val("1");
}
//暂停
function planPause(){
	$("#planState").val("2");
}
//继续执行
function planNext(){
	$("#planState").val("1");
	var currentExecStep = $('#currentExecStep').val();
	planExecStep(currentExecStep,"1");
}
//手动执行
function planManual(){
	$("#planState").val("1");
	var currentExecStep = $('#currentExecStep').val();
	planExecStep(currentExecStep,"4");
}
//终止
function planStop(){
	$("#planState").val("0");
}
function insert_trafficplanexeclog(operateLog){
	operateLog.planId = planRecord.planID;
	operateLog.createAccount = top.MainPage.yhmc;
	operateLog.operateTime = $.formatDate(new Date(),"yyyy-MM-dd hh:mm:ss");
	$.ajax({
		type:'post',
		dataType : 'json',
		url:'trafficplanexeclogAction!insert_Trafficplanexeclog.do',
		data:operateLog,
		success:function(flag){			 
		//doSearch();
		}
	});
	$('#planLogDiv').append(unionPlanLogDiv(operateLog));
}



