var winId	= $.getUrlParam("winId");
var planRecord = top.$("#"+winId).panel("options").data;
var planExecRecord;
var planDeviceRecord;
var chose="";                  // 判断页面选择 的是  图片 录像 或 视频
$(function(){ 
//	console.log("======",planRecord,"-----",planRecord);
	//可以根据ID加载planRecord
	initDefault();	
	//初始化视频播放控件--start
	window.WebRtcServer && WebRtcServer.connectServer();
	(window !== top.WebRtcServer.reqPage) && top.WebRtcServer.setReqPage(window);
	initBodyEvent();
	initBlock();
	initRecordBlock();
	//初始化视频播放控件--end
	//initPushLet();
	//事件详情中图片、录像、视频按钮切换--start
	$(".div-img-title").on('click', 'li', function() {
		$(this).addClass('active').siblings('li').removeClass('active');
		var user_click = $(this).attr('data-c');
		var user_channel = $(this).attr('data-channel'); //存放设备编号$通道编号$通道名称信息
		if(user_click=="1"){  //图片
			chose = "1";
			$('.pc-img').show();
			$('.pc-video').hide();
			$('.pc-recordvideo').hide();			
		}else if(user_click=="2"){  //录像
			chose = "2";
			$('.pc-recordvideo').show();
			$('.pc-img').hide();
			$('.pc-video').hide();
			if(user_channel != null){
				var accidentId = $(this).attr('data-accidentId');
				var occurTime = $(this).attr('data-time');
				//关闭实时视频
				videoblock.stopMonitor();
				var v_channel = user_channel.split('$');
				webrtc_startRecordByEventId(v_channel[0],v_channel[1],v_channel[2],accidentId,occurTime);
			}
		}else{  //视频
			chose = "3";
			$('.pc-img').hide();
			$('.pc-video').show();
			$('.pc-recordvideo').hide();
			if(user_channel != null){
				//关闭录像
				recordblock.stopPlayback();
				var v_channel = user_channel.split('$');
				webrtc_startVideo(v_channel[0],v_channel[1],v_channel[2]);
			}
		}
	});
	//事件详情中图片、录像、视频按钮切换--end
	//关闭窗口
	parent.$(".panel-tool-close").on("click",function(){
		unloadWindow();
	})
	parent.$(".l-btn-text").on("click",function(){
		unloadWindow();
	})
});
/*
function eventTabChange(type){
	//console.log("=type="+type);
	if(type=="eventDiv"){
		$("#eventDiv").css("display","block");
		$("#planDiv").css("display","none");
	}
	if(type=="planDiv"){
		$("#eventDiv").css("display","none");
		$("#planDiv").css("display","block");
	}
}
*/
function initDefault(){
	if(planRecord){
//		console.log("===== plan.js =====",planRecord);
		//获取图片路径
		var imgPath = 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);">';
			}
		}
		$("#eventDiv").find('div.pc-img-topimg img').eq(0).attr('src',getPicProxyUrl()+(firstImg));
		$("#eventDiv").find('div.pc-img-btm').html(allImg);
		//设备参数填充页面
		$("#eventDiv").find('div.div-img li').attr('data-channel',planRecord.vdid+"$"+planRecord.channelid+"$"+planRecord.tdmc);
		$("#eventDiv").find('div.div-img li').attr('data-accidentId',planRecord.accidentid);
		$("#eventDiv").find('div.div-img li').attr('data-time',planRecord.occurtime);
		//事件详细信息
		$('#tdmc').append(planRecord.tdmc);
		$('#occurTime').append(planRecord.occurtime);
		$('#eventName').append(planRecord.eventname);
		$('#alarmLevelName').append(planRecord.alarmlevelname);
		$('#accidentName').append(planRecord.accidentname);
		
		var params={};
		params.accidentId = planRecord.accidentid;
		params.page = 1;
		params.rows = 1;
		$.ajax({
			type: "post",
			url: "trafficaccidentinfoAction!get_Trafficaccidentinfo.do",
			dataType: "json",
			data: params,
		}).done(function(res) {			
//			console.log("测试res======",res);
			$('#roadName').append(res.roadname);
			$('#tunnelName').append(res.tunnelname);
			$('#startPile').append(res.startpile);
		});

		if(planRecord.accidentname=="已忽略"){
			$("#eventDiv").find('div #btn_qrbj').hide();
			$("#eventDiv").find('div #btn_hlbj').hide();
			$("#eventDiv").find('div #btn_ckya').hide();
		}
		if(planRecord.accidentname=="未处理"){
			$("#eventDiv").find('div #btn_qrbj').show();
			$("#eventDiv").find('div #btn_hlbj').show();
			$("#eventDiv").find('div #btn_ckya').hide();
		}
		if(planRecord.accidentname=="处理中"){
			/*$("#eventDiv").find('div #btn_qrbj').hide();
			$("#eventDiv").find('div #btn_hlbj').hide();
			$("#eventDiv").find('div #btn_ckya').show();*/


			$("#eventDiv").find('div #btn_qrbj').show();
			$("#eventDiv").find('div #btn_hlbj').show();
			$("#eventDiv").find('div #btn_ckya').hide();
		}
		if(planRecord.accidentname=="已处理"){
			$("#eventDiv").find('div #btn_qrbj').hide();
			$("#eventDiv").find('div #btn_hlbj').hide();
			$("#eventDiv").find('div #btn_ckya').show();
		}
		
		// 图片目标物
		setTimeout(function () {
		    
			// 圈定告警信息 图片
			var that = $(".pc-img").find('div.pc-img-topimg img').eq(0);
			var w = parseInt(that.width())+2;				// 有  margin 值
			var h = parseInt(that.height())+2; 
			
			var divY = planRecord.locationy;
			var divX = planRecord.locationx;
			var divH = planRecord.locationheight;
			var divW = planRecord.locationwidth;
//			console.log("== w：",w,"  === h：",h,"  === divY：",divY,"  === divX：",divX,"  === divH：",divH,"  === divW：",divW);		
			if(ifNull(divY) || ifNull(divX) || ifNull(divH) || ifNull(divW)){			// 为空不做处理
				
			}else{
				divY = h * divY + 30;		
				divX = w * divX;
				divH = h * divH;
				divW = w * divW;
//				console.log("=== w =：",w,"  === h =：",h,"  === divY =：",divY,"  === divX =：",divX,"  === divH =：",divH,"  === divW =：",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(){
	unloadWindow();
	var params = {};
	params.accidentState = 3;
	params.accidentId = planRecord.accidentid;
	$.ajax({
		type: "post",
		url: "trafficaccidentinfoAction!update_Trafficaccidentinfo_plan.do",
		dataType: "json",
		data: params,
	}).done(function(res) {
		//console.log(res);
		top.$.showMsg("状态已变更为【忽略报警】");
		refshParentQuery('3');
		
		$('#accidentName').text("已忽略");
		$("#btn_qrbj").hide();
		$("#btn_hlbj").hide();
		// 刷新左侧页面状态
		changeState();
		
		
		
		var tdbh = planRecord.vdid;
		var channelId = planRecord.channelid;
		var tdmc = planRecord.tdmc;
		if(chose == "2"){         // 播放录像			
			var accidentId = planRecord.accidentid;
			var occurTime = planRecord.occurtime;
			webrtc_startRecordByEventId(tdbh,channelId,tdmc,accidentId,occurTime);
		}else if(chose == "3"){          // 播放实时视频
			webrtc_startVideo(tdbh,channelId,tdmc);
		}
		
	});
}
//确认报警
function alarmCheckTrue(){
	unloadWindow();
	var params = {};
//	params.accidentState = 2;//处理中
	params.accidentState = 4;//已处理
	params.accidentId = planRecord.accidentid;
	params.alarmType = planRecord.eventtype;
	params.planGrade = planRecord.alarmlevel;
	params.tmpDeviceId = planRecord.vdid+"-"+planRecord.channelid;
	$.ajax({
		type: "post",
		url: "trafficaccidentinfoAction!update_Trafficaccidentinfo_plan.do",
		dataType: "json",
		data: params,
	}).done(function(res) {
		//console.log(res);
		top.$.showMsg("状态已变更为【 确认报警】");
		refshParentQuery('4');
		
//		$('#accidentName').text("处理中");
		$('#accidentName').text("已处理");
		$("#btn_qrbj").hide();
		$("#btn_hlbj").hide();
		// 刷新左侧页面状态
		changeState();
		
//		unionPlan();
		
		
		var tdbh = planRecord.vdid;
		var channelId = planRecord.channelid;
		var tdmc = planRecord.tdmc;
		if(chose == "2"){         // 播放录像			
			var accidentId = planRecord.accidentid;
			var occurTime = planRecord.occurtime;
			webrtc_startRecordByEventId(tdbh,channelId,tdmc,accidentId,occurTime);
		}else if(chose == "3"){          // 播放实时视频
			webrtc_startVideo(tdbh,channelId,tdmc);
		}
	});
}
function unionPlan(){
	unloadWindow();
	var url = top.sysUrl+"/yw/szmap/business/nplan/planExec.jsp";
 	windowid = top.$.createWin({
		 title	: 	'交通事件预案处理',
		 width  :  '1000',
		 height :  '420',
		 data   : 	planRecord,
		 url	:  url
	 });
}
//刷新父窗口的处理信息
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 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 ifNull(str){
	if(str == null){
		return true
	}else if(str == ''){
		return true
	}else if(str == ' '){
		return true;
	}else{
		return false;
	}
}

function changeState(){
	var ff = parent.window.frames[0].document.getElementById("iframe-jklk-tab").contentWindow;
	var page = ff.document.getElementById('page');
	var pangNum = $(page).find('li.pageItemActive').html();	
//	console.log("pangNum：",pangNum);
	ff.searchRequest('',pangNum);
}
