function PlanPushlet(){}
PlanPushlet.init = function (){
	PlanPushlet();
	PlanPushlet.initPushLet();
	setTimeout('PlanPushlet.planDelayTime()',1000);
	setTimeout('PlanPushlet.planDelayRun()',1000);
	setTimeout('PlanPushlet.planHandRun()',1000);
	setTimeout('PlanPushlet.planCheckResult()',1000);
}
//初始PUSHLET
PlanPushlet.initPushLet = function(){
	try {
		PL._init();
		PL.join();
		PL.listen();
		PL.subscribe('/trafficPlanData');
	} catch(e) {		
		setTimeout("PlanPushlet.initPushLet()",1000);
	}
}
//接收PUSHLET数据
function onData(data) {
	var event = data.get("p_subject");
	var sid = data.get("p_sid");
	var text = data.get("json");
	if(text==""||text==null||text==undefined){
		return;
	}
	if(event == "/trafficPlanData") {
		PlanPushlet.pushStatMsg(eval(text));
		return;
	}
}
PlanPushlet.pushStatMsg = function(data){
	var currentDeviceStep = $('#currentDeviceStep').val();
	if(data&&data.length>0){
		for(var x=0;x<data.length;x++){
			if(data[x].deviceId&&data[x].deviceType){
				var pushletData =data[x];
				if(PlanExec.deviceMap.get(pushletData.deviceId)){
					var tmpDevice = PlanExec.deviceMap.get(pushletData.deviceId);
					var tmpKey = "_"+tmpDevice.linkageID+"_"+tmpDevice.planType+"_"+tmpDevice.execOrder;
					var tmpexecState = "";
					if(pushletData.execState=="3"){
						tmpexecState = "执行成功";
						$("#plan_linkage_statu_"+tmpKey).removeClass("state-wait");
						$("#plan_linkage_statu_"+tmpKey).addClass("state-success");	
					}else if(pushletData.execState=="4"){
						tmpexecState = "执行失败";
						$("#plan_linkage_statu_"+tmpKey).removeClass("state-wait");
						$("#plan_linkage_statu_"+tmpKey).addClass("state-fail");	
					}	
					var linkagekey = "plan_linkage_device_"+tmpDevice.linkageID;
					var tmp_linkagekey = $("."+linkagekey);					
					var hasResult = "";
					for(var zz=0;zz<tmp_linkagekey.length;zz++){
						var tmpdata = tmp_linkagekey[zz];
						tmpdata = $(tmpdata)[0].outerHTML;
						if(tmpdata){
							if(tmpdata.indexOf("state-wait")!=-1){
								hasResult = "wait";
							}
							if(tmpdata.indexOf("state-fail")!=-1){
								hasResult = "failt";
								break;
							}
							if(tmpdata.indexOf("state-success")!=-1){
								if(hasResult!="failt"){
									hasResult = "success";
								}
							}
						}
					}
					if(hasResult=="wait"){
						
					}
					if(hasResult=="failt"){
						$('#plan_exec_jd_'+tmpDevice.linkageID).removeClass("planYuan-first4");
						$('#plan_exec_jd_'+tmpDevice.linkageID).addClass("planYuan-first2");
						$("#plan_exec_zt_"+tmpDevice.linkageID).html("<span class='icon-state icon-fail' ></span>执行失败");
					}
					if(hasResult=="success"){
						$('#plan_exec_jd_'+tmpDevice.linkageID).removeClass("planYuan-first4");
						$('#plan_exec_jd_'+tmpDevice.linkageID).addClass("planYuan-first3");
						$("#plan_exec_zt_"+tmpDevice.linkageID).html("<span class='icon-state icon-success' ></span>执行成功");
					}			
					//plan_linkage_device_5 state-wait
					//plan_linkage_device_5 state-wait
					var operateLog = {};
					operateLog.operateCommand = "执行返回";		
					operateLog.operateModule = tmpDevice.typeName;
					operateLog.operateContent = '['+tmpDevice.linkageDeviceName+']['+tmpDevice.typeName+']'+tmpexecState+'.';
					operateLog.operateState = '成功';
					operateLog.deviceType = '';
					operateLog.deviceId = tmpDevice.linkageDeviceId;
					operateLog.deviceName = tmpDevice.linkageDeviceName;
					PlanExec.insert_trafficplanexeclog(operateLog);
				}
			}
		}
	}
}
setTimeout('PlanPushlet.refreshCurrentPlan()',1000);  
//刷新查询   
PlanPushlet.refreshCurrentPlan = function(){
	if(!$("#requestState").val()){
		$("#requestState").val("0");
		$("#requestTime").val("0");
	}
	//判断请求返回，超过5秒结束任务，判断为失败
	if($("#requestState").val()=="0"){
		$("#requestTime").val("0");
	}
	if($("#requestState").val()=="1"){
		var resuestTime = $("#requestTime").val();
		if(isNaN(resuestTime)){
			resuestTime = 0;
		}				
		resuestTime = parseInt(resuestTime)+parseInt(1);
		if(parseInt(resuestTime)>5){
			$("#requestState").val("0");
			$("#requestTime").val("0");
			//执行异常任务
			PlanPushlet.execPlanException();
			setTimeout('PlanPushlet.refreshCurrentPlan()',1000);
		}else{
			$("#requestTime").val(resuestTime);
			setTimeout('PlanPushlet.refreshCurrentPlan()',1000);
		}
	}else{
		setTimeout('PlanPushlet.refreshCurrentPlan()',1000);
	}
}
//根据
PlanPushlet.execPlanException = function(){
	var useRunResutlAuto = true;
	for(var i=0;i<PlanExec.deviceMap.size();i++){
		var tmpDevice = PlanExec.deviceMap.next().value();
		var tmpKey = "_"+tmpDevice.linkageID+"_"+tmpDevice.planType+"_"+tmpDevice.execOrder;
		var tmpexecState = "";
		tmpexecState = "执行失败";
		if($("#plan_exec_zt_"+tmpDevice.linkageID).html().indexOf("正在执行")!=-1){				
			var tmpPlanStep = PlanExec.stepMap.get(tmpDevice.linkageID);
			if(tmpPlanStep.execModel=="0"){
				if(useRunResutlAuto){
					$("#plan_linkage_statu_"+tmpKey).removeClass("state-wait");
					$("#plan_linkage_statu_"+tmpKey).addClass("state-success");
					$('#plan_exec_jd_'+tmpDevice.linkageID).removeClass("planYuan-first1");
					$('#plan_exec_jd_'+tmpDevice.linkageID).removeClass("planYuan-first2");
					$('#plan_exec_jd_'+tmpDevice.linkageID).removeClass("planYuan-first3");
					$('#plan_exec_jd_'+tmpDevice.linkageID).removeClass("planYuan-first4");
					$('#plan_exec_jd_'+tmpDevice.linkageID).addClass("planYuan-first3");
					$("#plan_exec_zt_"+tmpDevice.linkageID).html("<span class='icon-state icon-fail' ></span>执行成功");
				}else{
					$("#plan_linkage_statu_"+tmpKey).removeClass("state-wait");
					$("#plan_linkage_statu_"+tmpKey).addClass("state-fail");
					$('#plan_exec_jd_'+tmpDevice.linkageID).removeClass("planYuan-first1");
					$('#plan_exec_jd_'+tmpDevice.linkageID).removeClass("planYuan-first2");
					$('#plan_exec_jd_'+tmpDevice.linkageID).removeClass("planYuan-first3");
					$('#plan_exec_jd_'+tmpDevice.linkageID).removeClass("planYuan-first4");
					$('#plan_exec_jd_'+tmpDevice.linkageID).addClass("planYuan-first2");
					$("#plan_exec_zt_"+tmpDevice.linkageID).html("<span class='icon-state icon-fail' ></span>执行失败");
				}
				var operateLog = {};
				operateLog.operateCommand = "执行返回";		
				operateLog.operateModule = tmpDevice.typeName;
				operateLog.operateContent = '['+tmpDevice.linkageDeviceName+']['+tmpDevice.typeName+']'+tmpexecState+'.';
				operateLog.operateState = '失败';
				operateLog.deviceType = '';
				operateLog.deviceId = tmpDevice.linkageDeviceId;
				operateLog.deviceName = tmpDevice.linkageDeviceName;
				PlanExec.insert_trafficplanexeclog(operateLog);
			}
		}
	}
}
//延时执行
PlanPushlet.planDelayTime = function(){
	var delayLinkageID = $('#delayLinkageID').val();
	if(delayLinkageID){
		if(delayLinkageID.indexOf(";")!=-1){
			var delayLinkageID_s =  delayLinkageID.split(";");
			for(var i=0;i<delayLinkageID_s.length;i++){
				var tmpLinkageID = delayLinkageID_s[i];
				if(tmpLinkageID){
					var tmpLinkageID_delay = $('#plan_step_delay_'+tmpLinkageID).html();
					console.log("=delayLinkageID="+delayLinkageID+"==tmpLinkageID=="+tmpLinkageID+"=tmpLinkageID_delay="+tmpLinkageID_delay);
					if(parseInt(tmpLinkageID_delay)==0){
						//定时执行任务  tmpLinkageID_delay
						if($("#plan_exec_zt_"+tmpLinkageID).html().indexOf("等待执行")!=-1){
							$('#plan_step_delayrun_'+tmpLinkageID).html(5);
							$("#plan_exec_zt_"+tmpLinkageID).html("<span class='icon-state icon-fail' ></span>正在执行");
							PlanRun.runDelayLinkage(tmpLinkageID);
						}
					}else{
						if($("#planState").val()==("1")){
							tmpLinkageID_delay = parseInt(tmpLinkageID_delay)-1;
							$('#plan_step_delay_'+tmpLinkageID).html(tmpLinkageID_delay);
						}
					}
				}
			}
		}
	}	
	setTimeout('PlanPushlet.planDelayTime()',1000);
}
PlanPushlet.planDelayRun = function(){
	var delayLinkageID = $('#delayLinkageID').val();
	if(delayLinkageID){
		if(delayLinkageID.indexOf(";")!=-1){
			var delayLinkageID_s =  delayLinkageID.split(";");
			for(var i=0;i<delayLinkageID_s.length;i++){
				var tmpLinkageID = delayLinkageID_s[i];
				if(tmpLinkageID){
					var tmpLinkageID_delay = $('#plan_step_delayrun_'+tmpLinkageID).html();
					if(parseInt(tmpLinkageID_delay)==0){
						tmpexecState = "执行失败";
						if($("#plan_exec_zt_"+tmpLinkageID).html().indexOf("正在执行")!=-1){				
							var tmpPlanStep = PlanExec.stepMap.get(tmpLinkageID);
							$('#plan_exec_jd_'+tmpLinkageID).removeClass("planYuan-first1");
							$('#plan_exec_jd_'+tmpLinkageID).removeClass("planYuan-first2");
							$('#plan_exec_jd_'+tmpLinkageID).removeClass("planYuan-first3");
							$('#plan_exec_jd_'+tmpLinkageID).addClass("planYuan-first2");
							$("#plan_exec_zt_"+tmpLinkageID).html("<span class='icon-state icon-fail' ></span>执行失败");
							/*
							var operateLog = {};
							operateLog.operateCommand = "执行返回";		
							operateLog.operateModule = tmpDevice.typeName;
							operateLog.operateContent = '['+tmpDevice.linkageDeviceName+']['+tmpDevice.typeName+']'+tmpexecState+'.';
							operateLog.operateState = '失败';
							operateLog.deviceType = '';
							operateLog.deviceId = tmpDevice.linkageDeviceId;
							operateLog.deviceName = tmpDevice.linkageDeviceName;
							PlanExec.insert_trafficplanexeclog(operateLog);
							*/
							$('#delayLinkageID').val(delayLinkageID.replace(tmpLinkageID,""));
						}
					}else{
						tmpLinkageID_delay = parseInt(tmpLinkageID_delay)-1;
						$('#plan_step_delayrun_'+tmpLinkageID).html(tmpLinkageID_delay);
					}
				}
			}
		}
	}	
	setTimeout('PlanPushlet.planDelayRun()',1000);
}

PlanPushlet.planHandRun = function(){
	var handLinkageID = $('#handLinkageID').val();
	if(handLinkageID){
		if(handLinkageID.indexOf(";")!=-1){
			var handLinkageID_s =  handLinkageID.split(";");
			for(var i=0;i<handLinkageID_s.length;i++){
				var tmpLinkageID = handLinkageID_s[i];
				if(tmpLinkageID){
					var tmpLinkageID_delay = $('#plan_step_delayrun_'+tmpLinkageID).html();
					//console.log("=handLinkageID="+handLinkageID+"=tmpLinkageID_delay="+tmpLinkageID_delay);
					if(parseInt(tmpLinkageID_delay)==0){
						tmpexecState = "执行失败";
						if($("#plan_exec_zt_"+tmpLinkageID).html().indexOf("正在执行")!=-1){				
							var tmpPlanStep = PlanExec.stepMap.get(tmpLinkageID);
							$('#plan_exec_jd_'+tmpPlanStep.linkageID).removeClass("planYuan-first1");
							$('#plan_exec_jd_'+tmpPlanStep.linkageID).removeClass("planYuan-first2");
							$('#plan_exec_jd_'+tmpPlanStep.linkageID).removeClass("planYuan-first3");
							$('#plan_exec_jd_'+tmpPlanStep.linkageID).addClass("planYuan-first2");
							$("#plan_exec_zt_"+tmpPlanStep.linkageID).html("<span class='icon-state icon-fail' ></span>执行失败");
							/*
							var operateLog = {};
							operateLog.operateCommand = "执行返回";		
							operateLog.operateModule = tmpDevice.typeName;
							operateLog.operateContent = '['+tmpDevice.linkageDeviceName+']['+tmpDevice.typeName+']'+tmpexecState+'.';
							operateLog.operateState = '失败';
							operateLog.deviceType = '';
							operateLog.deviceId = tmpDevice.linkageDeviceId;
							operateLog.deviceName = tmpDevice.linkageDeviceName;
							PlanExec.insert_trafficplanexeclog(operateLog);
							*/
							$('#handLinkageID').val(handLinkageID.replace(tmpLinkageID,""));
						}
					}else{
						tmpLinkageID_delay = parseInt(tmpLinkageID_delay)-1;
						$('#plan_step_delayrun_'+tmpLinkageID).html(tmpLinkageID_delay);
					}
				}
			}
		}
	}	
	setTimeout('PlanPushlet.planHandRun()',1000);
}
PlanPushlet.planCheckResult = function(){
	console.log("=PlanPushlet.planCheckResult=");
	var checkResutl = "1";
	for(var i=0;i<PlanExec.stepMap.size();i++){
		var tmpStep = PlanExec.stepMap.next().value();
		if(tmpStep.linkageDeviceName.indexOf("结束")!=-1||$("#plan_exec_zt_"+tmpStep.linkageID).html().indexOf("执行成功")!=-1||$("#plan_exec_zt_"+tmpStep.linkageID).html().indexOf("执行失败")!=-1){
			//checkResutl = "1";
		}else{
			checkResutl = "0";
			break;
		}
	}
	if(checkResutl=="1"){
		if(PlanExec.lastLinkageID){
			$('#plan_exec_jd_'+PlanExec.lastLinkageID).removeClass("planYuan-first1");
			$('#plan_exec_jd_'+PlanExec.lastLinkageID).addClass("planYuan-first4");
		}
	}else{
		setTimeout('PlanPushlet.planCheckResult()',1000);
	}
	
}

