//
function Plan(){}
Plan.alarmListIndex = 0;
Plan.init = function (accidentId,number){
	console.log("===accidentId==="+accidentId);
//	console.log("===number==="+number);
	Plan();
	//var accidentId = PlanExec.getUrlParam("accidentId");
	Plan.alarmListIndex = number;
	Plan.planRecord;
	Plan.planExecRecord;
	Plan.planDeviceRecord;
	Plan.loadAccident(accidentId);
	initBlock();
	initRecordBlock();	
}
function changeShowDevice(num){	
	$('#plan_device_div_'+num).slideToggle();
}
Plan.loadAccident = function(accidentId){
	var params = {};
	params.accidentId = accidentId;
	params.page = 1;
	params.rows = 1;
	console.log(params);
	$.ajax({
		type: "post",
		url: "trafficaccidentinfoAction!get_Trafficaccidentinfo.do",
		dataType: "json",
		data: params,
	}).done(function(res) {
		console.log(res);
		if(res){
			Plan.planRecord = res;
			var deviceInfo = BaseData.cameraMap.get(res.deviceId);
			Plan.planRecord.tdmc = deviceInfo.tdmc;
			Plan.planRecord.roadName = deviceInfo.roadName;
			Plan.planRecord.tunnelName = deviceInfo.tunnelName;
			Plan.planRecord.startPile = deviceInfo.startPile;
			//获取图片路径
			var imgPath = Plan.planRecord.eventUrl; 
			var sz_imgPath=new Array(); 
			var firstImg = '';
			var allImg = '';
			if(imgPath != null){
				sz_imgPath = imgPath.split('$');
				firstImg = sz_imgPath[0];
				//循环显示小图
				for(var k=0;k<sz_imgPath.length;k++){
					allImg+='<img src="'+getPicProxyUrl()+(sz_imgPath[k])+'" alt=""  width="100" height="80" onerror="$.loadErrorPic(this);">';
				}
			}
			//console.log("=firstImg="+firstImg);
			$("#showTopImage").find('div.pc-img-topimg img').eq(0).attr('src',getPicProxyUrl()+(firstImg));
			//$("#showTopImage").find('div.pc-img-btm').html(allImg);
			//事件详细信息
			$('#tdmc').html(Plan.planRecord.tdmc);
			$('#occurTime').html(Plan.planRecord.occurTime);
			$('#eventName').html(Plan.planRecord.eventName);
			$('#alarmLevelName').html(Plan.planRecord.alarmLevelName);
			$('#roadName').html(Plan.planRecord.roadName);
			$('#tunnelName').html(Plan.planRecord.tunnelName);
			$('#startPile').html(Plan.planRecord.startPile);
			$('#accidentName').html(Plan.planRecord.accidentName);
			
			if(Plan.planRecord.accidentState=="1"){//未处理
				$("#btn_qrbj").css('display','');
				$("#btn_hlbj").css('display','');
				$("#btn_ckya").css('display','none');
			}
			if(Plan.planRecord.accidentState=="2"){//处理中
				$("#btn_qrbj").css('display','none');
				$("#btn_hlbj").css('display','none');
				$("#btn_ckya").css('display','');
			}
			if(Plan.planRecord.accidentState=="3"){//已忽略
				$("#btn_qrbj").css('display','none');
				$("#btn_hlbj").css('display','none');
				$("#btn_ckya").css('display','none');
			}
			if(Plan.planRecord.accidentState=="4"){//已处理
				$("#btn_qrbj").css('display','none');
				$("#btn_hlbj").css('display','none');
				$("#btn_ckya").css('display','');
			}
			//；0-已展示未查看,1-未展示未执行；2-处理中；3-已忽略；4-已完结;5-已查看；6-联动执行完成，全部成功；7-联动执行完成，失败',
			//  `deviceId` varchar(100) DEFAULT NULL COMMENT '设备ID',
			//Plan.initPlan();
		}		
	});	
}
//图片代理
function getPicProxyUrl(){
	var proName = top.window.location.pathname.split("/")[1];
	var sysUrl = "http://"+window.location.host+"/"+proName;
	var picProxyUrl = sysUrl+"/sourceProxy?fileType=picUrlProxy&picUrl=";
	return  picProxyUrl;
}
Plan.formatDate = function(value,format){
	format 	= format || 'yyyy-MM-dd';
	value 	= value  || new Date(); 
	try{
		return value.format(format);
	}catch(e){
		return null;
	}
}
function eventClose(){
	console.log("==eventClose==");
	$('#eventShowDiv').css('display','none');
}
//忽略报警
function alarmCheckFalse(){
	var params = {};
	params.accidentState = 3;
	params.accidentId = Plan.planRecord.accidentId;
	$.ajax({
		type: "post",
		url: "trafficaccidentinfoAction!update_Trafficaccidentinfo_plan.do",
		dataType: "json",
		data: params,
	}).done(function(res) {
		//console.log(res);
		//top.$.showMsg("状态已变更为【忽略报警】");
		eventClose();
		//refshParentQuery('3');
//		console.log(Plan.alarmListIndex)
		BaseData.alarmList.splice(Plan.alarmListIndex, 1);
		//加载告警事件数据至首页底部页面 
	 	$("#iframe_pageBottom")[0].contentWindow.fillAlarmData(BaseData.alarmList);  //调用子页面中的subFunction方法。    
		
	});
}
//确认报警
function alarmCheckTrue(){
	var params = {};
	params.accidentState = 2;//处理中
	params.accidentId = Plan.planRecord.accidentId;
	params.alarmType = Plan.planRecord.eventType;
	params.planGrade = Plan.planRecord.alarmLevel;
	params.tmpDeviceId = Plan.planRecord.vdId+"-"+Plan.planRecord.channelId;
	$.ajax({
		type: "post",
		url: "trafficaccidentinfoAction!update_Trafficaccidentinfo_plan.do",
		dataType: "json",
		data: params,
	}).done(function(res) {
		//console.log(res);
		//top.$.showMsg("状态已变更为【 确认报警】");
		eventClose();
		Plan.planRecord.accidentName = "处理中";
		//refshParentQuery('4');
		unionPlan();
//		console.log(Plan.planRecord)
//		console.log(Plan.alarmListIndex)
		BaseData.alarmList[Plan.alarmListIndex].accidentName="处理中";
		BaseData.alarmList[Plan.alarmListIndex].accidentState="2";
		//加载告警事件数据至首页底部页面 
	 	$("#iframe_pageBottom")[0].contentWindow.fillAlarmData(BaseData.alarmList);  //调用子页面中的subFunction方法。    
	});
}
function unionPlan(){
	var params = {};
	params.alarmType = Plan.planRecord.eventType;
	params.planGrade = Plan.planRecord.alarmLevel;
	params.channelId = Plan.planRecord.vdId+"-"+Plan.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){
			if(Plan.planRecord.accidentName=="未处理"){	
				//关联预案
				var operateLog = {};
				operateLog.operateCommand = '关联预案';
				operateLog.operateModule = '';
				operateLog.operateContent = Plan.planRecord.alarmLevelName +Plan.planRecord.eventName+'关联'+Plan.planRecord.planName+'成功';
				operateLog.operateState = '成功';
				operateLog.deviceType = '';
				operateLog.deviceId = Plan.planRecord.vdId+"-"+Plan.planRecord.channelId;
				operateLog.deviceName = '';
				Plan.insert_trafficplanexeclog(operateLog);
				//启动预案
				operateLog = {};
				operateLog.operateCommand = '启动预案';
				operateLog.operateModule = '';
				operateLog.operateContent = Plan.planRecord.alarmLevelName +Plan.planRecord.eventName+'启动'+Plan.planRecord.planName+'成功';
				operateLog.operateState = '成功';
				operateLog.deviceType = '';
				operateLog.deviceId = Plan.planRecord.vdId+"-"+Plan.planRecord.channelId;
				operateLog.deviceName = '';
				Plan.insert_trafficplanexeclog(operateLog);
			}
			//加载预案
			loadPlanIframe(res[0].planID, Plan.planRecord.accidentId, Plan.alarmListIndex);
			$("#eventShowDiv").css("display","none");
			//加载预案关联隧道实景图
	   		$("#showPlanTunnel").css("display","block");
	   		$("#showPlanTunnel").addClass("showsuidaoimage");
		    $("#showPlanTunnel .videoPageBox").empty().append($("<img width='100%' height='100%' src='itemPage/eventRight/dytsd.png'/>"));
		}
	});
}
//插入日志
Plan.insert_trafficplanexeclog = function(operateLog){
	//operateLog.createAccount = window.opener.top.MainPage.yhmc;
	operateLog.operateTime = Plan.formatDate(new Date(),"yyyy-MM-dd hh:mm:ss");
	$.ajax({
		type:'post',
		dataType : 'json',
		url:'trafficplanexeclogAction!insert_Trafficplanexeclog.do',
		data:operateLog,
		success:function(flag){			 
		}
	});
}
function loadPlanIframe(planID,accidentID){
	//console.log("==unionPlan==");
	$('#pageBottom iframe').attr("src","plan/PlanExec.jsp?planID="+planID+"&accidentID="+accidentID+"&alarmListIndex="+Plan.alarmListIndex);  
	$("#pageRight iframe").attr("src","plan/PlanRight.jsp");
	$("#pageLeft iframe").attr("src","plan/PlanDetails.jsp");
}
function reloadIndex(){
	console.log("==reloadIndex==");
	$('#pageBottom iframe').attr("src","itemPage/mainBottom/pagebottom.jsp");  
	$("#pageRight iframe").attr("src","itemPage/mainRight/homePageRight.jsp");
	$("#pageLeft iframe").attr("src","itemPage/mainLeft/homePageLeft.jsp");
}
//关闭预案关联隧道实景图
function tunnelPlanClose(){
	$("#showPlanTunnel").css("display","none");
    $("#showPlanTunnel .videoPageBox").empty();
}