//工具条划圆形操作
function Circle(){
	
}
Circle.init=function (){
	Circle();
};
//画圆形
Circle.drawPolyline=function(){
	var Graphic=MapAPI.getGraphic();
	var Draw=MapAPI.getDraw();
	var Color=MapAPI.getColor();
	if(!Circle.draw ){
		Circle.draw = new Draw(MapAPI.getMap());
	}
	var PictureFillSymbol=MapAPI.getPictureFillSymbol();
	var SimpleLineSymbol=MapAPI.getSimpleLineSymbol();
	var SimpleFillSymbol=MapAPI.getSimpleFillSymbol();
	
    var symbol = new SimpleFillSymbol(
            SimpleFillSymbol.STYLE_SOLID, 
            new SimpleLineSymbol(
              SimpleLineSymbol.STYLE_SOLID, 
              new Color([255, 0, 0]), 
              2
            ), 
            new Color([255, 0, 0,0.7])
          );
	    	if(!MapAPI.getMap().isPan){
	    		 MapAPI.getMap().enableMapNavigation();
	    		 Circle.draw.deactivate(); 
	    		 Circle.draw=null; 
	    	}else{
	    		MapAPI.getMap().disableMapNavigation();
	    		Circle.draw.activate('circle');
	    		Circle.draw.on("draw-end", function(evt){
	    			console.log("evt",evt)
	    			console.log("evt.geometry12",evt.geometry);
	    			var html= "<div class='bcontent' style='padding-right:20px;'><form class='form-horizontal'>"+
	    			"<div class='form-group' style='display:none;'><label for='xh' class='col-sm-left control-label'>序号</label>"+
	    		    "<div class='col-sm-right'>"+
	    		     " <input type='text' class='form-control' id='xh'  placeholder='序号'>"+
	    		    "</div></div>"+
	    		    "<div class='form-group'><label for='areaName' class='col-sm-left control-label'>区域名称：</label>"+
	    		    "<div class='col-sm-right' style='position:relative;'>"+
	    		    " <input type='text' class='form-control' value='' id='areaName'   placeholder='区域名称'>"+
	    		    "</div></div>"+
	    			"<div class='form-group'><label for='sxt' class='col-sm-left control-label'>监控名称：</label>"+
	    		    "<div class='col-sm-right' style='position:relative;'>"+
	    		     " <input type='text' class='form-control' value='' id='sxt' readonly  placeholder='监控名称'>"+
	    		     "<div onclick='Label.cameraSearch()' class='sxtd glyphicon glyphicon-plus' style='cursor:pointer;width: 30px;height: 30px;position: absolute;top: 2px;right: -8px; font-size: 18px;'>+</div>"+
	    		    "</div></div>"+
	    			"<div class='form-group' style='display:none;'><label for='sbbh' class='col-sm-left control-label'>设备编号：</label>"+
	    		    "<div class='col-sm-right'>"+
	    		     " <input type='text' class='form-control' id='sbbh'  placeholder='设备编号'>"+
	    		    "</div></div>"+
	    			"<div class='form-group' style='display:none;'><label for='tdbh' class='col-sm-left control-label'>通道编号：</label>"+
	    		    "<div class='col-sm-right'>"+
	    		     " <input type='text' class='form-control' id='tdbh'  placeholder='通道编号'>"+
	    		    "</div></div>"+
	    			"<div class='form-group'><label for='roadName' class='col-sm-left control-label'>路段名称：</label>"+
	    		    "<div class='col-sm-right' style='position:relative;'>"+
	    		     " <input type='text' class='form-control' value='' id='roadName' readonly  placeholder='路段名称'>"+
	    		     "<div onclick='Label.roadSearch()' class='sxtd glyphicon glyphicon-plus' style='cursor:pointer;width: 30px;height: 30px;position: absolute;top: 2px;right: -8px; font-size: 18px;'>+</div>"+
	    		    "</div></div>"+
	    		    "<div class='form-group'><label for='position' class='col-sm-left control-label'>坐标：</label>"+
	    		    "<div class='col-sm-right'>"+
	    		     " <input type='text' class='form-control' id='position' value='${position}' placeholder='坐标'>"+
	    		    "</div></div>"+
	    		    "<div class='form-group'><div class='col-sm-2'></div>"+
	    		    "<div class='col-sm-10'>"+
	    		    " <input class='btn btn-default' onclick='Circle.confirmDrawPolyline();' type='button' value='确认'>"+
	    		     " <input class='btn btn-default' onclick='Circle.cancelDrawPolyline();'   id='cancelVideoBtn' type='button' value='取消' style='margin-left:34px;'>"+
	    		    "</div></div>"+
	    		    "</form></div>";
	    		   var positionStr='';
	    			for(var i=0,len=evt.geometry.rings[0].length;i<len;i++){
	    				var x=evt.geometry.rings[0][i][0];
	    				var y=evt.geometry.rings[0][i][1];
	    				if(i==len-1){
	    					positionStr+=x+','+y;
	    				}else{
	    					positionStr+=x+','+y+";";
	    				}
	    			}
	    			var attr = {position:positionStr};
	    			var InfoTemplate=MapAPI.getInfoTemplate();
	    			var Point=MapAPI.getPoint();
	    			var point = new Point(evt.geometry.rings[0][0][0], evt.geometry.rings[0][0][1]);
	    			var infoTemplate = new InfoTemplate("绘制区域",html);
	    			Circle.graphic=new Graphic(evt.geometry, symbol,attr,infoTemplate);
	    			MapAPI.getCircleGraphicsLayer().add(Circle.graphic);
	    			MapAPI.getMap().setInfoWindowOnClick(true);
	    			MapAPI.getMap().centerAt(point).then(function(){
						MapAPI.getMap().infoWindow.setTitle(infoTemplate.title);
						MapAPI.getMap().infoWindow.setContent(infoTemplate.content);
						MapAPI.getMap().infoWindow.show(point, MapAPI.getMap().getInfoWindowAnchor(point));
						MapAPI.getMap().infoWindow.setFeatures([Circle.graphic]);
						var hide=MapAPI.getMap().infoWindow.on("hide",function(){
							Circle.cancelDrawPolyline();
							hide.remove();
						});
	    			});
	    			MapAPI.getMap().enableMapNavigation();
	    			Circle.draw.deactivate(); 
	    			 Circle.draw=null; 
	    		});
	    	}
};
/*//画折线
Circle.drawPolyline=function(){
	var Graphic=MapAPI.getGraphic();
	var Draw=MapAPI.getDraw();
	var Color=MapAPI.getColor();
	if(!Circle.draw ){
		Circle.draw = new Draw(MapAPI.getMap());
	}
	var PictureFillSymbol=MapAPI.getPictureFillSymbol();
	var SimpleLineSymbol=MapAPI.getSimpleLineSymbol();
	var SimpleFillSymbol=MapAPI.getSimpleFillSymbol();
	
	var symbol = new SimpleFillSymbol(
			SimpleFillSymbol.STYLE_SOLID, 
			new SimpleLineSymbol(
					SimpleLineSymbol.STYLE_SOLID, 
					new Color([255, 0, 0]), 
					2
			), 
			new Color([255, 0, 0,0.7])
	);
	if(!MapAPI.getMap().isPan){
		MapAPI.getMap().enableMapNavigation();
		Circle.draw.deactivate(); 
		Circle.draw=null; 
	}else{
		MapAPI.getMap().disableMapNavigation();
		Circle.draw.activate('polyline');
		Circle.draw.on("draw-end", function(evt){
			console.log("evt.geometry",evt.geometry);
			var html= "<div class='bcontent' style='padding-right:20px;'><form class='form-horizontal'>"+
			"<div class='form-group' style='display:none;'><label for='xh' class='col-sm-left control-label'>序号</label>"+
			"<div class='col-sm-right'>"+
			" <input type='text' class='form-control' id='xh'  placeholder='序号'>"+
			"</div></div>"+
			"<div class='form-group'><label for='areaName' class='col-sm-left control-label'>区域名称：</label>"+
			"<div class='col-sm-right' style='position:relative;'>"+
			" <input type='text' class='form-control' value='' id='areaName'   placeholder='区域名称'>"+
			"</div></div>"+
			"<div class='form-group'><label for='sxt' class='col-sm-left control-label'>监控名称：</label>"+
			"<div class='col-sm-right' style='position:relative;'>"+
			" <input type='text' class='form-control' value='' id='sxt' readonly  placeholder='监控名称'>"+
			"<div onclick='Label.cameraSearch()' class='sxtd glyphicon glyphicon-plus' style='cursor:pointer;width: 30px;height: 30px;position: absolute;top: 2px;right: -8px; font-size: 18px;'>+</div>"+
			"</div></div>"+
			"<div class='form-group' style='display:none;'><label for='sbbh' class='col-sm-left control-label'>设备编号：</label>"+
			"<div class='col-sm-right'>"+
			" <input type='text' class='form-control' id='sbbh'  placeholder='设备编号'>"+
			"</div></div>"+
			"<div class='form-group' style='display:none;'><label for='tdbh' class='col-sm-left control-label'>通道编号：</label>"+
			"<div class='col-sm-right'>"+
			" <input type='text' class='form-control' id='tdbh'  placeholder='通道编号'>"+
			"</div></div>"+
			"<div class='form-group'><label for='roadName' class='col-sm-left control-label'>路段名称：</label>"+
			"<div class='col-sm-right' style='position:relative;'>"+
			" <input type='text' class='form-control' value='' id='roadName' readonly  placeholder='路段名称'>"+
			"<div onclick='Label.roadSearch()' class='sxtd glyphicon glyphicon-plus' style='cursor:pointer;width: 30px;height: 30px;position: absolute;top: 2px;right: -8px; font-size: 18px;'>+</div>"+
			"</div></div>"+
			"<div class='form-group'><label for='position' class='col-sm-left control-label'>坐标：</label>"+
			"<div class='col-sm-right'>"+
			" <input type='text' class='form-control' id='position' value='${position}' placeholder='坐标'>"+
			"</div></div>"+
			"<div class='form-group'><div class='col-sm-2'></div>"+
			"<div class='col-sm-10'>"+
			" <input class='btn btn-default' onclick='Circle.confirmDrawPolyline();' type='button' value='确认'>"+
			" <input class='btn btn-default' onclick='Circle.cancelDrawPolyline();'   id='cancelVideoBtn' type='button' value='取消' style='margin-left:34px;'>"+
			"</div></div>"+
			"</form></div>";
			var positionStr='';
			for(var i=0,len=evt.geometry.paths[0].length;i<len;i++){
				var x=evt.geometry.paths[0][i][0];
				var y=evt.geometry.paths[0][i][1];
				if(i==len-1){
					positionStr+=x+','+y;
				}else{
					positionStr+=x+','+y+";";
				}
			}
			var attr = {position:positionStr};
			var InfoTemplate=MapAPI.getInfoTemplate();
			var Point=MapAPI.getPoint();
			var point = new Point(evt.geometry.paths[0][0][0], evt.geometry.paths[0][0][1]);
			var infoTemplate = new InfoTemplate("绘制区域",html);
			Circle.graphic=new Graphic(evt.geometry, symbol,attr,infoTemplate);
			MapAPI.getCircleGraphicsLayer().add(Circle.graphic);
			MapAPI.getMap().setInfoWindowOnClick(true);
			MapAPI.getMap().centerAt(point).then(function(){
				MapAPI.getMap().infoWindow.setTitle(infoTemplate.title);
				MapAPI.getMap().infoWindow.setContent(infoTemplate.content);
				MapAPI.getMap().infoWindow.show(point, MapAPI.getMap().getInfoWindowAnchor(point));
				MapAPI.getMap().infoWindow.setFeatures([Circle.graphic]);
				var hide=MapAPI.getMap().infoWindow.on("hide",function(){
					Circle.cancelDrawPolyline();
					hide.remove();
				});
			});
			MapAPI.getMap().enableMapNavigation();
			Circle.draw.deactivate(); 
			Circle.draw=null; 
		});
	}
};
*/
//圆形确认功能
Circle.confirmDrawPolyline=function(){
	var areaName=$("#areaName").val();
	var roadName = $("#roadName").val();
	var sxt = $("#sxt").val();
	var position = $("#position").val();
	/*
	var positionres = $("#position").val();
	var sps = positionres.split(";");
	var positionx = "";
	var positiony = "";
	var position = "";
	for (var i=0;i<sps.length;i++) {
		var sp = sps[i].split(",");
		position += parseFloat(sp[0]).toFixed(5) + ",";
		if (i == sps.length-1) {
			position += parseFloat(sp[1]).toFixed(5) + "";
		} else {
			position += parseFloat(sp[1]).toFixed(5) + ";";
		}
	}*/
	/*
	for (var i=0;i<=45;i++) {
		var sp = sps[i].split(",");
		if (i <= 30) {
			positionx += spx = parseFloat(sp[0]).toFixed(11) + ",";
		}
		if (i <= 15) {
			positiony += parseFloat(sp[1]).toFixed(11) + ",";
		}
		if (i > 30 && i <= 45) {
			if (i == 45) {
				positiony += parseFloat(sp[1]).toFixed(11) + "";
			} else {
				positiony += parseFloat(sp[1]).toFixed(11) + ",";
			}
		}
	}
	position = positionx + positiony +"";
	var positionArr=position.split(',');
	*/
	
    var sbbh = $("#sbbh").val();
    var tdbh = $("#tdbh").val();
 	var xh = $("#xh").val();
 	var uuid = $.uniqueId();
 	var params = {roadName:roadName,channelName:sxt,roadGridPosition:position,
 			      roadNumber:xh,vdid:sbbh,channelId:tdbh,remarks1:areaName,
 			      remarks2:'2',roadGridId:uuid};
// 	console.log("区域绘制：",params);
 	var ssld = areaName + "(" + roadName + ")";
	var data = {"position":position,"sxt":sxt,"xh":uuid,"tdmc":areaName};
	if(areaName.trim()!=''&&sxt.trim()!=''&&roadName.trim()!=''){
		$.ajax({
			 type:'post',
			 dataType : 'json',
			 url:'trafficroadgridinfoAction!insert_Trafficroadgridinfo.do',
			 data:params,
			 success:function(flag){
				 if(flag = "success"){
						var result=[[]];
						if(position!=null&&position.trim()!=''){
							var positionArr=position.split(';');
							for(var j=0;j<positionArr.length;j++){
								result[0].push([positionArr[j].split(',')[0],positionArr[j].split(',')[1]]);
							}
						}
						// 根据position生成圆形经纬度
						// [x:0-30][y:0-15,31-45],生成结果：[x:0-30,31-60(29-0)],[y:0-15,16-30(14-0),31-45,46-60(44-30)]
						/*
						if(position!=null&&position.trim()!=''){
							var resultx = [];
							var resulty = [];
							var resultx1 = [];
							var resulty1 = [];
							var resulty2 = [];
							var positionArr=position.split(',');
							for(var j=0;j<positionArr.length;j++){
								if (j<=30) {
									resultx1.push(positionArr[j]);
									resultx.push(positionArr[j]); // 生成0-30
								}
								if (j > 30 && j <= 46) {
									resulty1.push(positionArr[j]);
									resulty.push(positionArr[j]);// 生成0-15
								}
								if (j > 46) {
									resulty2.push(positionArr[j]);
								}
							}
							// 分别生成相应集合
							for (var i= resultx1.length - 2;i >= 0;i--) {
								resultx.push(resultx1[i]); // 生成x29-0
							}
							for (var i= resulty1.length - 2;i >= 0;i--) {
								resulty.push(resulty1[i]);// 生成y16-30
							}
							for (var i=0;i < resulty2.length;i++) {
								resulty.push(resulty2[i])// 生成31-45
							}
							for (var i= resulty2.length - 2;i >= 0;i--) {
								resulty.push(resulty2[i]);// 生成y46-59
							}
							resulty.push(resulty1[0]);// 生成60
							// 组合为var result=[[]];
							for (i = 0; i<=60; i++) {
								result[0].push([resultx[i],resulty[i]]);
							}
						}*/
						data.geometry=result;
						var html="<div class='bcontent'><div>" +
								 "<span style='color:#75767A'>"+ssld+"</span>" +
								 "</div><ul  class='bcontentItem'>" +
								 	"<li onclick=' Circle.detailInfoCircle(\""+uuid+"\")'>详细信息</li>" +
								 	"<li onclick='Circle.deleteCircle(\""+uuid+"\")'>删除区域</li>" +
								 "</ul></div>";
						MapUtil.addCircle(data,html);
						MapAPI.getMap().infoWindow.hide();
						$.showMsg("区域添加成功！");
				 }else{
					 $.showMsg("区域添加失败!");
				 }
			 }
		});
		
	}else{
		$.showMsg("监控名称和路段名称不能为空!");
	}
};

//圆形取消功能
Circle.cancelDrawPolyline=function(){
	MapAPI.getCircleGraphicsLayer().remove(Circle.graphic);
	MapAPI.getMap().infoWindow.hide();
	$.showMsg("框选取消成功!");
};

//圆形删除功能
Circle.deleteCircle=function(xh){
	$.ajax({										// 删除数据
		 type:'post',
		 dataType : 'json',
		 url:'trafficroadgridinfoAction!delete_Trafficroadgridinfo.do',
		 data:{"roadGridId":xh},
		 success:function(flag){
			 if(flag == "success"){
				 var graphicArr=MapAPI.getCircleGraphicsLayer().graphics;
					var graphic=null;
					for(var i=0,len=graphicArr.length;i<len;i++){
						if(graphicArr[i].attributes.xh==xh){
							graphic=graphicArr[i];
							break;
						}
					}
					if(graphic!=null){
						MapAPI.getCircleGraphicsLayer().remove(graphic);
						MapAPI.getMap().infoWindow.hide();
						$.showMsg("标注区域删除成功!");
					}
			 }
		 }	 
	});		 
	
};

//查看框选详情信息
Circle.detailInfoCircle=function(xh){
	var sysUrl = top.sysUrl;
	windowId = top.$.createWin({
		 title	: 	'详细信息',
		 width	:  	650,
		 height	: 	400,
		 data : {"roadGridId":xh}, 
		 url	:	sysUrl+"/yw/szmap/jx/frameSelectDetail.jsp"
	 });
};