//
function PlanExec(){}
$(function(){
	$("#requestTime").val(0);
	PlanPushlet.init();
	PlanExec.init();
	PlanRun.init();
	PlanHtml.init();
});
PlanExec.init = function (){
	var winId	= $.getUrlParam("winId");
	PlanExec.planRecord = top.$("#"+winId).panel("options").data;
	PlanExec.planExecRecord;
	PlanExec.planDeviceRecord;
	PlanExec.initPlan();
}
//关联显示预案
PlanExec.initPlan = function(){
	var params = {};
	if( PlanExec.planRecord.eventtype=="1"){
		 PlanExec.planRecord.eventtype = "18";
	}
	params.alarmType = PlanExec.planRecord.eventtype;
	params.planGrade = PlanExec.planRecord.alarmlevel;
	params.channelId = PlanExec.planRecord.vdid+"-"+PlanExec.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){
			PlanExec.planRecord.planid =  res[0].planid;
			PlanExec.planRecord.planname =  res[0].planname;
			if(PlanExec.planRecord.accidentname=="未处理"){	
				//关联预案
				var operateLog = {};
				operateLog.operateCommand = '关联预案';
				operateLog.operateModule = '';
				operateLog.operateContent = PlanExec.planRecord.alarmlevelname +PlanExec.planRecord.eventname+'关联'+PlanExec.planRecord.planname+'成功';
				operateLog.operateState = '成功';
				operateLog.deviceType = '';
				operateLog.deviceId = PlanExec.planRecord.vdid+"-"+PlanExec.planRecord.channelid;
				operateLog.deviceName = '';
				PlanExec.insert_trafficplanexeclog(operateLog);
				//启动预案
				operateLog = {};
				operateLog.operateCommand = '启动预案';
				operateLog.operateModule = '';
				operateLog.operateContent = PlanExec.planRecord.alarmlevelname +PlanExec.planRecord.eventname+'启动'+PlanExec.planRecord.planname+'成功';
				operateLog.operateState = '成功';
				operateLog.deviceType = '';
				operateLog.deviceId = PlanExec.planRecord.vdid+"-"+PlanExec.planRecord.channelid;
				operateLog.deviceName = '';
				PlanExec.insert_trafficplanexeclog(operateLog);
			}
			//加载预案
			PlanExec.loadPlanInfo(PlanExec.planRecord.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;'>");
		PlanExec.planExecRecord = res;
		$('#allStep').val(res.length);
		for(var x=0;x<res.length;x++){
			var tmpPlan = res[x];
			tmpPlan = PlanExec.changeExecModel(tmpPlan);
			tmplinkage.push(PlanHtml.loadPlanStepDiv(tmpPlan,(1+x)));
			if(x==(res.length-1)){
				tmplinkage.push("</ul>");
				$('#planlinageDiv').append(tmplinkage.join(""));
				PlanExec.loadPlanDeviceInfo(res[0].planid);
			}
		}
	});
}
//加载预案设备
PlanExec.loadPlanDeviceInfo = function(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 = "";
		PlanExec.planDeviceRecord = res;
		for(var x=0;x<res.length;x++){
			var tmpplanDevice = res[x];
			deviceHtml = PlanHtml.loadPlanDeviceDiv(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[0]);
			}
		}
	});
}
//修改预案执行模式名称
PlanExec.changeExecModel = function(data){
	if(data.execmodel=="0"){
		data.execmodelname = "自";
	}
	if(data.execmodel=="1"){
		data.execmodelname = "手";
	}
	if(data.execModel=="2"){
		data.execmodelname = "延";
	}
	return data;
}
//1运行--2暂停--0停止
//启动
PlanExec.planStart = function(){
	$("#planState").val("1");
}
//暂停
PlanExec.planPause = function(){
	$("#planState").val("2");
}
//继续执行
PlanExec.planNext = function(){
	$("#planState").val("1");
	var currentExecStep = $('#currentExecStep').val();
	PlanRun.planRunStep(currentExecStep,"1");
}
//手动执行
PlanExec.planManual = function(){
	$("#planState").val("1");
	var currentExecStep = $('#currentExecStep').val();
	PlanRun.planRunStep(currentExecStep,"4");
}
//终止
PlanExec.planStop = function(){
	$("#planState").val("0");
}
//插入日志
PlanExec.insert_trafficplanexeclog = function(operateLog){
	operateLog.planId = PlanExec.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){			 
		}
	});
	//显示日志
	$('#planLogDiv').append(PlanHtml.unionPlanLogDiv(operateLog));
}
//加载预案设备状态
PlanExec.loadPlanDeviceState = function(){
	var params = {};
	params.planID = PlanExec.planRecord.planid;
	params.page = "1";
	params.rows = "100";	
	//params.order = " order by linkageId,execOrder asc ";
	$.ajax({
		type: "post",
		url: "trafficaccidentstateAction!query_Trafficaccidentstate_plan.do",
		dataType: "json",
		data: params,
	}).done(function(res) {	
		console.log(res);
		//定位到设备，要定位节点
		var tmp_planDeviceModel;
		for(var x=0;x<res.length;x++){
			var planDeviceModel = res[x];
			var tmpKey = "_"+planDeviceModel.execid+"_"+planDeviceModel.plantype+"_"+planDeviceModel.execorder;
			if($("#plan_linkage_statu_"+tmpKey)){
				if(planDeviceModel.execstate=="1"){
					//定位节点
					$('#currentDeviceState').val("未执行");
					tmp_planDeviceModel = plandevicemodel;
				}else if(planDeviceModel.execstate=="2"){
					
				}else if(planDeviceModel.execstate=="3"){
					$("#plan_linkage_statu_"+tmpKey).removeClass("state-wait");	
					$("#plan_linkage_statu_"+tmpKey).addClass("state-success");
				}else if(planDeviceModel.execstate=="4"){
					$("#plan_linkage_statu_"+tmpKey).removeClass("state-wait");	
					$("#plan_linkage_statu_"+tmpKey).addClass("state-fail");
					$('#currentDeviceState').val("执行失败");
				}
			}
			if(x==res.length-1){
				//最后一个设备
				return
			}
			var planDeviceModel_next = res[x+1];
			if(planDeviceModel.execid == planDeviceModel_next.execid){
				
			}else{
				//当前节点状态Step
				PlanExec.stepStateByDevice(planDeviceModel);
				$('#currentDeviceState').val("");
			}
		}
	});
}
//根据设备找节点
PlanExec.stepStateByDevice = function(planDeviceModel){
	for(var x = 0;x<PlanExec.planExecRecord.length;x++){
		var tmp_planExecModel = PlanExec.planExecRecord[x];
		if(tmp_planExecModel.linkageid== planDeviceModel.execid){
			var tmpStep = parseInt(x)+parseInt(1);
			if($('#currentDeviceState').val()==("执行失败")){
				$('#plan_exec_zt_'+tmpStep).html("<span class='icon-state icon-fail' ></span>执行失败");	
			}else if($('#currentDeviceState').val()==("未执行")){
				$('#plan_exec_zt_'+tmpStep).html("<span class='icon-state icon-fail' ></span>执行失败");	
				PlanRun.planRunStep(tmpStep,"1");
			}else{
				$('#plan_exec_zt_'+tmpStep).html("<span class='icon-state icon-success' ></span>执行成功");	
			}
			//所有操作按钮灰掉
			$("#btn_again_step_"+tmpStep).removeClass('hidden');
			$("#btn_confirm_step_"+tmpStep).removeClass('hidden');
			$("#btn_skip_step_"+tmpStep).removeClass('hidden');
			$("#btn_manual_step_"+tmpStep).removeClass('hidden');
			$("#btn_advance_step_"+tmpStep).removeClass('hidden');
			//$("#btn_again_step_"+tmpStep).addClass('hidden');		
			$("#btn_confirm_step_"+tmpStep).addClass('hidden');		
			$("#btn_skip_step_"+tmpStep).addClass('hidden');		
			$("#btn_manual_step_"+tmpStep).addClass('hidden');		
			$("#btn_advance_step_"+tmpStep).addClass('hidden');
			break;			
		}
	}
}
