//监控设备
var graph1;
//服务器设备
var graph2;

//设备数据
var deviceDataSource;

$(function(){
	//设备统计
	loadDeviceInfo();
	//设备列表
	initTreePanel();
	//查询设备
	searchVideosPage();
	searchVideos();
	// 运维看板中间 数据
	//loadAbnormalDevice();
	initDeviceData();
	loadDeviceTree(2);
});
/*var data={
		name:"中心交换机",
		symbol:"image://itemPage/mainMiddle/images/routeNormal.png",
		symbolSize:30,
		children:[
		          	{name:"管理服务器",symbol:"image://itemPage/mainMiddle/images/server.png",symbolSize:30,value:12},
		          	{name:"200中级",symbol:"image://itemPage/mainMiddle/images/routeNormal.png",symbolSize:30,children:[{name:"完好率 90%",symbol:"image://itemPage/mainMiddle/images/videoNormal.png",symbolSize:30,children:[{name:"节点1",value:1,symbol:"image://itemPage/mainMiddle/images/videoNormal.png",symbolSize:30},{name:"节点2",value:2,symbol:"image://itemPage/mainMiddle/images/videoNormal.png",symbolSize:30}]}]},
		          	{name:"a",	symbol:"image://itemPage/mainMiddle/images/routeNormal.png",symbolSize:30,children:[{name:"完好率 70%",symbol:"image://itemPage/mainMiddle/images/videoNormal.png",symbolSize:30,children:[{name:"节点3",value:2,symbol:"image://itemPage/mainMiddle/images/videoNormal.png",symbolSize:30},{name:"节点4",value:4,symbol:"image://itemPage/mainMiddle/images/videoNormal.png",symbolSize:30}]}]},
		          	{name:"203share",symbol:"image://itemPage/mainMiddle/images/routeNormal.png",symbolSize:30,children:[{name:"完好率 90%",symbol:"image://itemPage/mainMiddle/images/video.png",symbolSize:30,children:[{name:"节点5",value:5,symbol:"image://itemPage/mainMiddle/images/video.png",symbolSize:30},{name:"节点6",value:6,symbol:"image://itemPage/mainMiddle/images/video.png",symbolSize:30}]}]}
		          ]
}*/
function initDeviceData(){
	$.ajax({
		type : 'post',
		dateType : 'json',
		async: false,
		data:{},
		url:"ywvideocurrentstateAction!getVideoTree.do",
		success : function(res){
			deviceDataSource=res;
		}
		});
}
function selectVideo(type,video){
	var returnVideo= {};
	$.extend(true,returnVideo,video);
	 if(type==1){
		for(var i=0,len=video.length;i<len;i++){
			var children=video[i].children
			returnVideo[i].children=[];
			for(var j=0;j<children.length;j++){
				if((children[j].isonline+children[j].recordstate+children[j].vqcstate+children[j].vqdstate)==4){
					returnVideo[i].children.push(children[j]);
				}
			}
			}
	}else if(type==0){
		for(var i=0,len=video.length;i<len;i++){
			var children=video[i].children
			returnVideo[i].children=[];
			for(var j=0;j<children.length;j++){
				if((children[j].isonline+children[j].recordstate+children[j].vqcstate+children[j].vqdstate)<4){
					returnVideo[i].children.push(children[j]);
				}
			}
		}
	}
	var returnValue = [];
	for(i in returnVideo){
		returnValue.push(returnVideo[i]);
	}
	return returnValue;
}
function loadDeviceTree(type){
	var data={
			name:"中心交换机",
			symbol:"image://itemPage/mainMiddle/images/routeNormal.png",
			symbolSize:30,
			children:[
			          ]
				}
			var video=selectVideo(type,deviceDataSource.video);
			var machineRate=deviceDataSource.machine.intactRate;
			var machine=deviceDataSource.machine.children;
			for(var i=0,len=video.length;i<len;i++){
				var item={name:video[i].xzmc,symbol:"image://itemPage/mainMiddle/images/routeNormal.png",symbolSize:30,	children:[ ]};
				if(parseInt(video[i].intactRate.split(".")[0])<90){
					var whl={name:"完好率"+video[i].intactRate,symbol:"image://itemPage/mainMiddle/images/videoAbnormal.png",symbolSize:30,	children:[ ]};
				}else{
					var whl={name:"完好率"+video[i].intactRate,symbol:"image://itemPage/mainMiddle/images/videoNormal.png",symbolSize:30,	children:[ ]};
				}
				data.children.push(item);
				data.children[i].children.push(whl);
				var groupLength=parseInt(video[i].children.length/50);//分组的长度
				var groupYS=video[i].children.length%50;//最后一组的节点数
				if(groupLength==0){//分组为0，显示设备节点
						for(var j=0,length=video[i].children.length;j<length;j++){
							if(video[i].children[j].isonline+video[i].children[j].recordstate+video[i].children[j].vqcstate+video[i].children[j].vqdstate<4){
								var item1={name:video[i].children[j].channelname,symbol:"image://itemPage/mainMiddle/images/videoAbnormal.png",symbolSize:30};
							}else{
								var item1={name:video[i].children[j].channelname,symbol:"image://itemPage/mainMiddle/images/videoNormal.png",symbolSize:30,value:2};
							}
							data.children[i].children[0].children.push(item1);
						}
				}else{//分组不为0，则分组
						if(groupLength>50){//判断第一层分组的数目是否大于50，如果大于50，则继续分组，否则显示设备节点
								  for(var j=0;j<50;j++){
										var item1={name:"组"+(j+1),symbol:"image://itemPage/mainMiddle/images/videoNormal.png",symbolSize:30,children:[]};
										data.children[i].children[0].children.push(item1);
									}
									var groupLength1=parseInt(groupLength/50);//计算第二层分组的数目
									var groupYS=(groupLength%50)+(Math.ceil(groupLength/50));//第二层最后一组的分层数目
										var total=0
									for(var j=groupLength1;j<50;j++){//将设备节点挂在第一层的下面
											for(var k=0;k<50;k++){
												total++;
														if(video[i].children[((j-groupLength1)*50)+k].isonline+video[i].children[((j-groupLength1)*50)+k].recordstate+video[i].children[((j-groupLength1)*50)+k].vqcstate+video[i].children[((j-groupLength1)*50)+k].vqdstate<4){
															var item2={name:video[i].children[((j-groupLength1)*50)+k].channelname,symbol:"image://itemPage/mainMiddle/images/videoAbnormal.png",symbolSize:30};
														}else{
															var item2={name:video[i].children[((j-groupLength1)*50)+k].channelname,symbol:"image://itemPage/mainMiddle/images/videoNormal.png",symbolSize:30,value:2};
														}
														data.children[i].children[0].children[j].children.push(item2);
												}
											}
						for(var j=0;j<groupLength1;j++){//第二层进行分组
							
							if(j!=groupLength1-1){
								for(var k=0;k<50;k++){
									var item1={name:"组"+(k+1),symbol:"image://itemPage/mainMiddle/images/videoNormal.png",symbolSize:30,children:[]};
									data.children[i].children[0].children[j].children.push(item1);
									}
							}else{
								for(var k=0;k<groupYS;k++){
									var item1={name:"组"+(k+1),symbol:"image://itemPage/mainMiddle/images/videoNormal.png",symbolSize:30,children:[]};
									data.children[i].children[0].children[j].children.push(item1);
								}
							}
							
						}
							for(var j=0;j<groupLength1;j++){//将设备节点挂在第二层的下面
								if(j!=groupLength1-1){
										for(var k=0;k<50;k++){
											for(var h=0;h<50;h++){
												if(video[i].children[total+h+(k*50)+(j*50*50)].isonline+video[i].children[total+h+(k*50)+(j*50*50)].recordstate+video[i].children[total+h+(k*50)+(j*50*50)].vqcstate+video[i].children[total+h+(k*50)+(j*50*50)].vqdstate<4){
													var item2={name:video[i].children[total+h+(k*50)+(j*50*50)].channelname,symbol:"image://itemPage/mainMiddle/images/videoAbnormal.png",symbolSize:30};
												}else{
													var item2={name:video[i].children[total+h+(k*50)+(j*50*50)].channelname,symbol:"image://itemPage/mainMiddle/images/videoNormal.png",symbolSize:30,value:2};
												}
												data.children[i].children[0].children[j].children[k].children.push(item2);
											}
										}
								}else{
									for(var k=0;k<groupYS;k++){
										if(k!=groupYS-1){
											for(var h=0;h<50;h++){
												if(video[i].children[total+h+(k*50)+(j*50*50)].isonline+video[i].children[total+h+(k*50)+(j*50*50)].recordstate+video[i].children[total+h+(k*50)+(j*50*50)].vqcstate+video[i].children[total+h+(k*50)+(j*50*50)].vqdstate<4){
													var item2={name:video[i].children[total+h+(k*50)+(j*50*50)].channelname,symbol:"image://itemPage/mainMiddle/images/videoAbnormal.png",symbolSize:30};
												}else{
													var item2={name:video[i].children[total+h+(k*50)+(j*50*50)].channelname,symbol:"image://itemPage/mainMiddle/images/videoNormal.png",symbolSize:30,value:2};
												}
												data.children[i].children[0].children[j].children[k].children.push(item2);
											}
										}else{
											for(var h=0;h<(video[i].children.length%50);h++){
												if(video[i].children[total+h+(k*50)+(j*50*50)].isonline+video[i].children[total+h+(k*50)+(j*50*50)].recordstate+video[i].children[total+h+(k*50)+(j*50*50)].vqcstate+video[i].children[total+h+(k*50)+(j*50*50)].vqdstate<4){
													var item2={name:video[i].children[total+h+(k*50)+(j*50*50)].channelname,symbol:"image://itemPage/mainMiddle/images/videoAbnormal.png",symbolSize:30};
												}else{
													var item2={name:video[i].children[total+h+(k*50)+(j*50*50)].channelname,symbol:"image://itemPage/mainMiddle/images/videoNormal.png",symbolSize:30,value:2};
												}
												data.children[i].children[0].children[j].children[k].children.push(item2);
											}
										}
									}
								}
							}
						
					}else{//在第一层下面显示设备节点
								for(var j=0;j<=groupLength;j++){
									var item1={name:"组"+(j+1),symbol:"image://itemPage/mainMiddle/images/videoNormal.png",symbolSize:30,children:[]};
									data.children[i].children[0].children.push(item1);
									
									if(j!=groupLength){
										for(var k=0;k<50;k++){
												if(video[i].children[(j*50)+k].isonline+video[i].children[(j*50)+k].recordstate+video[i].children[(j*50)+k].vqcstate+video[i].children[(j*50)+k].vqdstate<4){
													var item2={name:video[i].children[(j*50)+k].channelname,symbol:"image://itemPage/mainMiddle/images/videoAbnormal.png",symbolSize:30};
												}else{
													var item2={name:video[i].children[(j*50)+k].channelname,symbol:"image://itemPage/mainMiddle/images/videoNormal.png",symbolSize:30,value:2};
												}
												data.children[i].children[0].children[j].children.push(item2);
											}
									}else{
										for(var k=0;k<groupYS;k++){
											if(video[i].children[(j*50)+k].isonline+video[i].children[(j*50)+k].recordstate+video[i].children[(j*50)+k].vqcstate+video[i].children[(j*50)+k].vqdstate<4){
												var item2={name:video[i].children[(j*50)+k].channelname,symbol:"image://itemPage/mainMiddle/images/videoAbnormal.png",symbolSize:30};
											}else{
												var item2={name:video[i].children[(j*50)+k].channelname,symbol:"image://itemPage/mainMiddle/images/videoNormal.png",symbolSize:30,value:2};
											}
											data.children[i].children[0].children[j].children.push(item2);
										}
									}
						}
					}
				}
			}
			var machinestateFlag=1;
			for(var i=0,len=machine.length;i<len;i++){
				if(machine[i].machinestate==0){
					machinestateFlag=0;
				}
			}
			if(machinestateFlag==0){
				var item={name:"管理服务器",symbol:"image://itemPage/mainMiddle/images/serverAbnormal.png",symbolSize:30,	children:[ ]};
			}else{
				var item={name:"管理服务器",symbol:"image://itemPage/mainMiddle/images/serverNormal.png",symbolSize:30,	children:[ ]};
			}
			data.children.push(item);
			for(var i=0,len=machine.length;i<len;i++){
				if(machine[i].machinestate==1){
					item={name:machine[i].machinename,symbol:"image://itemPage/mainMiddle/images/serverNormal.png",symbolSize:30,	children:[ ]};
				}else{
					item={name:machine[i].machinename,symbol:"image://itemPage/mainMiddle/images/serverAbnormal.png",symbolSize:30,	children:[ ]};
				}
				data.children[data.children.length-1].children.push(item);
			}
			console.log("data",data)
			parent.EchartsBase.treeGraph({
				idObj:document.getElementById("deviceGraph"),
				data:data
			});
}
function loadDeviceInfo(){
	$.ajax({
		type : 'post',
		dateType : 'json',
		url:"ywmachineinfoAction!count_HomepageDeviceCount.do",
		async:false,
		success : function(res){
			if(res){
				$('#jkzl').html(res.jkzl);
				$('#jkzxs').html(res.jkzxs);
				$('#fwqzl').html(res.fwqzl);
				$('#fwqzxs').html(res.fwqzxs);
			}
		}
	});
}		 	

function loadDeviceGraph1(){
	parent.EchartsBase.relationalGraph1({
	    idObj:document.getElementById("deviceGraph1"),
		data:graph1
	});
}

function searchVideosPage() {
	$('#pageBox').pagination({
		pageSize:20,
		onSelectPage: function(pageNumber, pageSize){
			searchVideos(pageNumber,pageSize);
		}
	});
}

function loadDeviceGraph2(){
	parent.EchartsBase.relationalGraph2({
		idObj:document.getElementById("deviceGraph2"),
		data:graph2
	});
}
  
function searchVideos(page, rows){
	
	var tree = xzTree.tree("getSelected");
	var params = {};
	
	var xzqh = "";
	if(tree != null){
		xzqh = tree.xzbh;
	}
	if(xzqh == -1){
		xzqh = "";
	}
	var fw = $('input:radio[name="c"]:checked').val();
	var tdmc = $('#tdmc').val();
	if(fw==0){  // 异常
		params.abnormal = 1;
	}else if(fw==1){		//正常
		params.normal = 1;
	}
	params.sbmc = tdmc;
	params.xzqh = xzqh;
	if (page == undefined || page == "" || page == null) {
		params.page = 1;
	} else {
		params.page = page;
	}
	if (rows == undefined || rows == "" || rows == null) {
		params.rows = 20;
	} else {
		params.rows = rows
	}
	
	$.ajax({
		type : 'post',
		dateType : 'json',
		url:"ywvideocurrentstateAction!query_videoState.do",
		data : params,
		async:false,
		success : function(res){
			if(res.rows){
				var htmlTr = "";
				res.rows.forEach(function(item){
					var isonline = item.isonline;
					var networkerror = item.networkerror;
					var vqcstate = item.vqcstate;
					var vqdstate = item.vqdstate;
					var recordstate = item.recordstate;
					var state = "",stateColor="#3bb256";						//是否异常
					if( isonline==1 && networkerror==1 && vqcstate==1 && vqdstate==1 && recordstate==1){
						state = "正常";
						stateColor="#3bb256";
					}else{
						state = "异常";
						stateColor="#cc0001";
					}
					
					var ip = item.sbip;
					ip = ip == null ? "" : ip;			// ip信息
					
					var vqcfailurecause = item.vqcfailurecause;
					var vqdfailurecause = item.vqdfailurecause;
					var recordfailurecause = item.recordfailurecause;
					var ycxx="";							// 异常详情
					if(vqcfailurecause!=null && vqcfailurecause!=""){
						ycxx = vqcfailurecause+","
					}
					if(vqdfailurecause!=null && vqdfailurecause!=""){
						ycxx += vqdfailurecause+","
					}
					if(recordfailurecause!=null && recordfailurecause!=""){
						ycxx += recordfailurecause
					}
					
					var xzmc = item.xzmc==null?"":item.xzmc;   //行政名称
					htmlTr += "<tr>" +
									"<td class='easyui-tooltip' title="+item.channelname+"><div>"+item.channelname+"</div></td>" +
									"<td class='easyui-tooltip' title="+xzmc+"><div>"+xzmc+"</div></td>" +
									"<td class='easyui-tooltip' title="+ip+"><div>"+ip+"</div></td>" +
									"<td class='easyui-tooltip' title="+state+"><div  style='color:"+stateColor+";display:inline-block;vertical-align:middle;'><span style='margin-right: 4px;width:12px;height:12px;background-color:"+stateColor+";display:inline-block;vertical-align:middle;'></span><span style='display:inline-block;vertical-align:middle;'>"+state+"</span></div></td>" +
									"<td class='easyui-tooltip' title="+ycxx+"><div style='color:"+stateColor+"'>"+ycxx+"</div></td>" +
							  "</tr>"
				})
				$('#sbxx').html(htmlTr);
				$('#pageBox').pagination({
					total : res.total
				});
			}
		}
	})
}
	

function initTreePanel(){
	xzTree = $('#xzTree').tree({
	        lines:false,
			animate: false, 
	    	multiple:false,
	    	editable:false,
	    	border:false,
	    	parentField:"sjxzbh",  
	    	textFiled:"xzmc",
	    	idFiled:"xzbh",
	    	collapse :true,
	    	data: [{
	    		xzmc: '行政目录' ,
				id  	:"-1",
				state	:'open',
				wholePath:'-1',
				xzbh	:'-1'
			}],
//	    	onClick:function(node){
//	    		$(this).tree(node.state === 'closed' ? 'expand' : 'collapse', node.target); 
//	    		doSearch();
//		 	},
		 	onSelect:function(){
		 		searchVideos();
		 	},
		 	onLoadSuccess:function(node,data){
		    	var 	innerHTML='<div class="tooltip-content"><span style="color:#fff">This is the tooltip message.</span></div><div class="tooltip-arrow-outer" style="border-right-color: rgb(102, 102, 102);"></div><div class="tooltip-arrow" style="border-right-color: rgb(102, 102, 102);"></div>';
		 		$('span.tree-title').tooltip({
		 		    position: 'left',
		 		    content: innerHTML,
		 		    onShow: function(){
		 		    	$(this).tooltip('tip').find("span").html($(this).html());
		 				$(this).tooltip('tip').css({
		 					backgroundColor: '#666',
		 					borderColor: '#666'
		 				});
		 		    }
		 		});
		 	},
			onDblClick:function(node){
			 	$(this).tree(node.state === 'closed' ? 'expand' : 'collapse', node.target);  
			},
			onContextMenu: function(e,node){
                e.preventDefault();
                $(this).tree('select',node.target);
                if(node.id == -1){
                	$('#menu1').hide(1).fadeIn(1).menu('show',{
                        left: e.pageX,
                        top: e.pageY
                   });
                }else{
                	$('#menu2').hide(1).fadeIn(1).menu('show',{
                        left: e.pageX,
                        top: e.pageY
                   });
                }
             }
	});
	$.ajax({
		 type:'post',
		 dataType : 'json',
		 url:'menuConfigAction!query_Ptxzs.do',//dac 行政树
//		 url:'ptxzsAction!query_Ptxzs.do',
//		 data:{
//			 order : " order by jdpx asc "
//		 },
		 success:function(data){
			$.each(data,function(i,o){
	    			o.state ='closed';
	    	})
	    	if(data){
	    		xzTree.tree('append', {
	    			parent : xzTree.tree('find', -1).target,
	    			data   : data
	    		});
	    	}
	    	var node = xzTree.tree('find', -1);
	    	xzTree.tree('select', node.target);
	    	
	    	var 	innerHTML='<div class="tooltip-content"><span style="color:#fff">This is the tooltip message.</span></div><div class="tooltip-arrow-outer" style="border-right-color: rgb(102, 102, 102);"></div><div class="tooltip-arrow" style="border-right-color: rgb(102, 102, 102);"></div>';

		 		$('span.tree-title').tooltip({
		 		    position: 'left',
		 		    content:innerHTML,
		 		    onShow: function(){

		 		    	$(this).tooltip('tip').find("span").html($(this).html());
		 				$(this).tooltip('tip').css({
		 					backgroundColor: '#666',
		 					borderColor: '#666'
		 				});
		 		    }
		 		});
		 }
	 });
}
	

function loadAbnormalDevice(){
	var fixed=false;
	graph1={
		categories:[{name:"服务器设备",symbol:"circle"},{name:"监控设备",symbol:"circle"}],
		nodes:[
		       ],
		       links:[
		              {source:"监控设备1",target:"监控设备2",value:10},
		              {source:"监控设备2",target:"监控设备3",value:10},
		              {source:"监控设备2",target:"监控设备4",value:10},
		              {source:"监控设备2",target:"监控设备5",value:10},
		              {source:"监控设备2",target:"监控设备6",value:10},
		              {source:"监控设备6",target:"监控设备7",value:10},
		              {source:"监控设备6",target:"监控设备8",value:10}
		              ]
		};
	graph2={
			categories:[{name:"服务器设备",symbol:"circle"},{name:"监控设备",symbol:"circle"}],
			nodes:[
			       ]
	};
	$.ajax({
		type : 'post',
		dateType : 'json',
		url:"ywvideocurrentstateAction!query_videoState.do",		// 异常的  监控设备
		data : {"abnormal":1,'page':1,'rows':8},			// start 和 limit 控制 数据条数
		async:false,
		success : function(res){
			var nodes=
				[
				 {name:"监控设备1",fixed:fixed,symbol:"image://images/videoAbnormal.png",label:{color:"red"}},
				 {name:"监控设备2",fixed:fixed,symbol:"image://images/videoAbnormal.png",label:{color:"red"}},
				 {name:"监控设备3",fixed:fixed,symbol:"image://images/videoAbnormal.png",label:{color:"red"}},
				 {name:"监控设备4",fixed:fixed,symbol:"image://images/videoAbnormal.png",label:{color:"red"}},
				 {name:"监控设备5",fixed:fixed,symbol:"image://images/videoAbnormal.png",label:{color:"red"}},
				 {name:"监控设备6",fixed:fixed,symbol:"image://images/videoAbnormal.png",label:{color:"red"}},
				 {name:"监控设备7",fixed:fixed,symbol:"image://images/videoAbnormal.png",label:{color:"red"}},
				 {name:"监控设备8",fixed:fixed,symbol:"image://images/videoAbnormal.png",label:{color:"red"}}
				 ];
				if(res.rows){
					for(var i=0,len=res.rows.length;i<len;i++){
						var obj={name:nodes[i].name,videonum:res.rows[i].videonum,channelname:res.rows[i].channelname,fixed:nodes[i].fixed,symbol:nodes[i].symbol,label:nodes[i].label};
						graph1.nodes.push(obj);      
						if(i==7)break;
					}
					loadDeviceGraph1();
				}
		}
	})
	
	// 异常的服务器设备
	$.ajax({
		type : 'post',
		dateType : 'json',
		url:"ywmachinecurrentstateAction!listYwmachinecurrentstateByPage.do",		// 异常的  服务器
		data : {"abnormal":1,"page":1,"rows":6},	// page 和 rows 控制 数据条数
		async:false,
		success : function(res){
/*			res.rows.push({});
*/			var nodes=
				[
				 {		symbolSize :[42,42],symbolOffset:[10,20],fixed:true,x:4,y:4,symbol:"image://images/serverAbnormal.png",label:{color:"red"}},
				 {		symbolSize :[42,42],symbolOffset:[10,-20],fixed:true,x:4,y:13,symbol:"image://images/serverAbnormal.png",label:{color:"red"}},
				 {		symbolSize :[42,42],symbolOffset:[10,20],fixed:true,x:4,y:14,symbol:"image://images/serverAbnormal.png",label:{color:"red"}},
				 {		symbolSize :[42,42],symbolOffset:[10,-20],fixed:true,x:11,y:18,symbol:"image://images/serverAbnormal.png",label:{color:"red"}},
				{		symbolSize :[42,42],symbolOffset:[0,20],fixed:true,x:12,y:4,symbol:"image://images/serverAbnormal.png",label:{color:"red"}},
				 {		symbolSize :[42,42],symbolOffset:[0,-20],fixed:true,x:12,y:13,symbol:"image://images/serverAbnormal.png",label:{color:"red"}}
				 ];
			if(res&&res.rows){
				for(var i=0,len=res.rows.length;i<len;i++){
					var obj={machineid:res.rows[i].machineid,machinename:res.rows[i].machinename,fixed:true,x:nodes[i].x,y:nodes[i].y,symbol:nodes[i].symbol,symbolSize:nodes[i].symbolSize,symbolOffset:nodes[i].symbolOffset,label:{color:"red"}};
					graph2.nodes.push(obj);      
				}
				loadDeviceGraph2();
			}
		}
	})
}