//工具条框选操作
function FrameSelect(){
	
}
FrameSelect.init=function (){
	FrameSelect();
};
//画折线
FrameSelect.drawPolyline=function(){
	var Graphic=MapAPI.getGraphic();
	var Draw=MapAPI.getDraw();
	var Color=MapAPI.getColor();
	if(!FrameSelect.draw ){
		FrameSelect.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();
	    		 FrameSelect.draw.deactivate(); 
	    		 FrameSelect.draw=null; 
	    	}else{
	    		MapAPI.getMap().disableMapNavigation();
	    		FrameSelect.draw.activate('polyline');
	    		FrameSelect.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='FrameSelect.confirmDrawPolyline();' type='button' value='确认'>"+
	    		     " <input class='btn btn-default' onclick='FrameSelect.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);
	    			FrameSelect.graphic=new Graphic(evt.geometry, symbol,attr,infoTemplate);
	    			MapAPI.getFrameSelectGraphicsLayer().add(FrameSelect.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([FrameSelect.graphic]);
						var hide=MapAPI.getMap().infoWindow.on("hide",function(){
							FrameSelect.cancelDrawPolyline();
							hide.remove();
						});
	    			});
	    			MapAPI.getMap().enableMapNavigation();
	    			FrameSelect.draw.deactivate(); 
	    			 FrameSelect.draw=null; 
	    		});
	    	}
};

//框选确认功能
FrameSelect.confirmDrawPolyline=function(){
	var areaName=$("#areaName").val();
	var roadName = $("#roadName").val();
	var sxt = $("#sxt").val();
	var position = $("#position").val();
    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]]);
							}
						}
						data.geometry=result;
						var html="<div class='bcontent'><div>" +
								 "<span style='color:#75767A'>"+ssld+"</span>" +
								 "</div><ul  class='bcontentItem'>" +
								 	"<li onclick=' FrameSelect.detailInfoFrameSelect(\""+uuid+"\")'>详细信息</li>" +
								 	"<li onclick='FrameSelect.deleteFrameSelect(\""+uuid+"\")'>删除区域</li>" +
								 "</ul></div>";
						MapUtil.addPolygon(data,html);
						MapAPI.getMap().infoWindow.hide();
						$.showMsg("区域添加成功！");
				 }else{
					 $.showMsg("区域添加失败!");
				 }
			 }
		});
		
	}else{
		$.showMsg("监控名称和路段名称不能为空!");
	}
};

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

//框选删除功能
FrameSelect.deleteFrameSelect=function(xh){
	$.ajax({										// 删除数据
		 type:'post',
		 dataType : 'json',
		 url:'trafficroadgridinfoAction!delete_Trafficroadgridinfo.do',
		 data:{"roadGridId":xh},
		 success:function(flag){
			 if(flag == "success"){
				 var graphicArr=MapAPI.getFrameSelectGraphicsLayer().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.getFrameSelectGraphicsLayer().remove(graphic);
						MapAPI.getMap().infoWindow.hide();
						$.showMsg("标注区域删除成功!");
					}
			 }
		 }	 
	});		 
	
};

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