var winId	= $.getUrlParam("winId");
var planRecords = top.$("#"+winId).panel("options").data;
var planExecRecord;
var planDeviceRecord;
var planRunStatue = "0";//1：启用，0：暂停
$(function(){
	initDefault();	
	initPushLet();

	//可以根据ID加载planRecord
	window.WebRtcServer && WebRtcServer.connectServer();
	(window !== top.WebRtcServer.reqPage) && top.WebRtcServer.setReqPage(window);
	initBodyEvent();
	//初始化视频播放控件
	initBlock();
	initRecordBlock();
	parent.$(".panel-tool-close").on("click",function(){
		returnTrafficAccident();
	})
	parent.$(".l-btn-text").on("click",function(){	
		returnTrafficAccident();
	})
});
function eventTabChange(type){
	if(type=="eventDiv"){   //事件图片
		$("#eventDiv").css("display","block");
		$(".pc-recordvideo").css("display","none");
		$(".pc-img").css("display","block");
		$(".pc-video").css("display","none");
		$("#planDiv").css("display","none");
		$("#pictureNav").addClass("active");
		$("#videoNav").removeClass("active");
		$("#recordNav").removeClass("active");
		$("#planNav").removeClass("active");
		videoblock.stopMonitor(); 
		recordblock.stopPlayback();
	}else if(type=="recordDiv"){   //录像播放
		$("#eventDiv").css("display","block");
		$(".pc-recordvideo").css("display","block");
		$(".pc-img").css("display","none");
		$(".pc-video").css("display","none");
		$("#planDiv").css("display","none");
		$("#pictureNav").removeClass("active");
		$("#videoNav").removeClass("active");
		$("#recordNav").addClass("active");
		$("#planNav").removeClass("active");
		ProgressBar.init({
			startTime:new Date(planRecords.occurTime),
			endTime:new Date(),
			totalSecond:299.825
		});
		webrtc_startRecordByEventId(planRecords.vdId,planRecords.channelId,planRecords.tdmc,planRecords.accidentId,planRecords.occurTime);//设备编号$通道编号$通道名称信息
		//关闭实时视频
		videoblock.stopMonitor(); 
	}else if(type=="videoDiv"){   //视频播放
		$("#eventDiv").css("display","block");
		$(".pc-recordvideo").css("display","none");
		$(".pc-img").css("display","none");
		$(".pc-video").css("display","block");
		$("#planDiv").css("display","none");
		$("#pictureNav").removeClass("active");
		$("#videoNav").addClass("active");
		$("#recordNav").removeClass("active");
		$("#planNav").removeClass("active");
		
		webrtc_startVideo(planRecords.vdId,planRecords.channelId,planRecords.tdmc);
		//关闭录像
		recordblock.stopPlayback();
	}else if(type=="planDiv"){   //预案
		$("#eventDiv").css("display","none");
		$(".pc-recordvideo").css("display","none");
		$(".pc-img").css("display","none");
		$(".pc-video").css("display","none");
		$("#planDiv").css("display","block");
		$("#pictureNav").removeClass("active");
		$("#videoNav").removeClass("active");
		$("#recordNav").removeClass("active");
		$("#planNav").addClass("active");
	}
}

function initDefault(){
	if(planRecords){
		//获取图片路径
		var imgPath = planRecords.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="130" height="100" onerror="$.loadErrorPic(this);">';
			}
		}
		$("#eventDiv").find('div.pc-img-topimg img').eq(0).attr('src',getPicProxyUrl()+(firstImg));
		$("#eventDiv").find('div.pc-img-btm').html(allImg);
		//事件详细信息
		$('#tdmc').append(planRecords.tdmc);
		$('#occurTime').append(planRecords.occurTime);
		$('#eventName').append(planRecords.eventName);
		$('#alarmLevelName').append(planRecords.alarmLevelName);
		$('#roadName').append(planRecords.roadName);
		$('#tunnelName').append(planRecords.tunnelName);
		$('#startPile').append(planRecords.startPile);
		$('#accidentName').append(planRecords.accidentName);
		if(planRecords.accidentName=="已处理"){
			$('#showYaxx').css('display','block');
			$("#planName").text(planRecords.planName);
		}
		if(planRecords.accidentName=="处理中"){
			$('#showYaxx').css('display','block');
			$("#planName").text(planRecords.planName);
		}
		if(planRecords.accidentName == '未处理'){
			$("#btn_qrbj").show();
			$("#btn_hlbj").show();
			$("#btn_checkPlan").hide();
		}else{
			$("#btn_qrbj").hide();
			$("#btn_hlbj").hide();
		}
		setTimeout(function () {
			// 圈定告警信息 图片
			var that = $("#eventDiv").find('div.pc-img-topimg img').eq(0);
			var w = parseInt(that.width())+2;				// 有  margin 值
			var h = parseInt(that.height())+2; 
			var divY = planRecords.locationY;
			var divX = planRecords.locationX;
			var divH = planRecords.locationHeight;
			var divW = planRecords.locationWidth;
			if(ifNull(divY) || ifNull(divX) || ifNull(divH) || ifNull(divW)){			// 为空不做处理
			}else{
				divY = h * divY + 30;				// 需要加上 上面 button 的高度   否则不准确
				divX = w * divX;
				divH = h * divH;
				divW = w * divW;
				var div = "<div style='width:"+ divW +"px;height:"+ divH +"px;border:1px solid red;position:absolute;top:"+ divY +"px;left:"+ divX +"px;'>";		
				$(that).parent().append(div)
			}
		  }, 500);	
	}
}
//图片代理
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;
}
//忽略报警
function alarmCheckFalse(){
	layer.confirm("<p style='font-size: 14px;'> 忽略原因:</p><p style='font-size: 14px;'> <textarea cols=35 rows=5 type='text' id='hlyy'></textarea> </p>", {
        btn : [ '确定', '取消' ]//按钮
    }, function(index) {
    	layer.close(index);
        var params = {};
    	params.accidentState = 3;
    	params.accidentId = planRecords.accidentId;
    	params.disposeReason = $("#hlyy").val();
    	if(params.disposeReason=="" || params.disposeReason==undefined || params.disposeReason==null){
			$.showMsg("请填写忽略报警原因！");
			return false;
		}else{
			$.ajax({
	    		type: "post",
	    		url: "trafficaccidentinfoAction!update_Trafficaccidentinfo_plan.do",
	    		dataType: "json",				
	    		data: params,
	    	}).done(function(res) {
	    		top.$.showMsg("状态已变更为【忽略报警】");
	    		$('#accidentName').text("已忽略");
	    		$("#btn_qrbj").hide();
	    		$("#btn_hlbj").hide();
	    		returnTrafficAccident();
	    	});
		}
	})
}
//确认报警
function alarmCheckTrue(){
	unloadWindow();
//	returnTrafficAccident();
	var params = {};
	params.accidentState = 2;
	params.accidentId = planRecords.accidentId;
	$.ajax({
		type: "post",
		url: "trafficaccidentinfoAction!update_Trafficaccidentinfo_plan.do",
		dataType: "json",
		data: params,
	}).done(function(res) {
		top.$.showMsg("状态已变更为【 确认报警】");
		planRecords.accidentName = "处理中";
		$('#accidentName').text("处理中");
		$("#btn_qrbj").hide();
		$("#btn_hlbj").hide();
		
		unionPlan();
	});
}
//刷新父窗口的处理信息
function refshParentQuery(){
	
}
function changeExecModel(data){
	if(data.execModel=="0"){
		data.execModelName = "自动执行";
	}
	if(data.execModel=="1"){
		data.execModelName = "手动执行";
	}
	if(data.execModel=="2"){
		data.execModelName = "延时执行";
	}
	return data;
}
//查看预案
function checkPlan(){
	var params = {};
	params.accidentId = planRecords.accidentId;
	$.ajax({
		type: "post",
		url: "trafficaccidentstateAction!getPlanInfosByAccidentId.do",
		dataType: "json",
		data: params,
	}).done(function(res) {		
		if(res!=null){
			unloadWindow();
			showPage(res.planName,res.planID, planRecords.accidentId,res.planGrade,res.alarmType,true,res.alarmDeviceId);
			//加载预案
			$("#eventShowDiv").css("display","none");
			
//			returnTrafficAccident();
		}
	});
}
function unionPlan(){
	var params = {};
	params.alarmType = planRecords.eventType;
	params.planGrade = planRecords.alarmLevel;
	params.channelId = planRecords.vdId+"-"+planRecords.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(planRecords.accidentName=="未处理"){	
				//关联预案
				var operateLog = {};
				operateLog.operateCommand = '关联预案';
				operateLog.operateModule = '';
				operateLog.operateContent = planRecords.alarmLevelName +planRecords.eventName+'关联'+planRecords.planName+'成功';
				operateLog.operateState = '成功';
				operateLog.deviceType = '';
				operateLog.deviceId = planRecords.vdId+"-"+planRecords.channelId;
				operateLog.deviceName = '';
				Plan.insert_trafficplanexeclog(operateLog);
				//启动预案
				operateLog = {};
				operateLog.operateCommand = '启动预案';
				operateLog.operateModule = '';
				operateLog.operateContent = planRecords.alarmLevelName +planRecords.eventName+'启动'+planRecords.planName+'成功';
				operateLog.operateState = '成功';
				operateLog.deviceType = '';
				operateLog.deviceId = planRecords.vdId+"-"+planRecords.channelId;
				operateLog.deviceName = '';
				insert_trafficplanexeclog(operateLog);
			}
			//加载预案
			showPage(res[0].planName,res[0].planID, planRecords.accidentId,res[0].planGrade,res[0].alarmType,false,res[0].alarmDeviceId);
		}
	});
}
//插入日志
function insert_trafficplanexeclog(operateLog) {
	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){
	$('#iframe_pageBottom').attr("src","plan/PlanExec.jsp?planID="+planID+"&accidentID="+planRecords.accidentId);  
}
function showPage(planName,planID,accidentID,alarmLevel,eventType,data,roadInfos){
	var url = top.sysUrl+"/im/traffic/alarm/TrafficPlan/sdyaDetail.jsp?roadInfos="+encodeURI(roadInfos)+"&planID="+planID+"&accidentID="+accidentID+"&flag="+data+"&channelId="+(planRecords.vdId+"-"+planRecords.channelId)+"&planName="+encodeURI(planName)+"&alarmLevel="+alarmLevel+"&eventType="+eventType;
	var params = {
			data:data,
			winId:winId
	}
	windowid = top.$.createWin({
		 title	: 	'报警详情-预案处理中',
		 width  :  '780',
		 height :  '550',
		 data   : params,
//		 buttons:  [{
//		     text:top.$.fn.textbox.defaults.close,
//		     handler:fetchData(displayType,1)
//	 	     }],
		 url	:  url
	 });
	top.$(top.GETWIN(windowid)).find("iframe")[0].contentWindow.onload=function(){
		$(top.$(top.GETWIN(windowid)).find("iframe")[0].contentWindow.document).find(".datailsHeader").hide();
	};

}
function returnTrafficAccident(){
	unloadWindow();
	top.$("#"+winId).dialog("close");
	top.$("#"+winId).panel("options").data.flag = true;
}

function ifNull(str){
	if(str == null){
		return true
	}else if(str == ''){
		return true
	}else if(str == ' '){
		return true;
	}else{
		return false;
	}
}