
var adm2 = {
    foldUnfold(obj,css1,css2,contentId,height){
        if($(obj).attr("class") == css1){
            $(obj).attr("class", css2)

        }else{
            $(obj).attr("class", css1)
        }
    },
    leftIframeObj:{
        currentId : "",
        state : false,
        /**
         * 弹出打开iframe
         * @param id
         * @param url
         */
        open(id, url) {
            if(!this.state) {
                $('#menu2').animate({
                    width: "400px"
                }, 400);
                $('#menu2').css("padding","5px")
                this.state = !this.state
            }

            if(this.currentId != id) {
                this.currentId = id
                var mhtml = "<iframe id='iframe' src='" + url+"'></iframe><a href='javascript:adm2.leftIframeObj.showAndHide()'><div class='showAndHide'></div>"
                $("#menu2").html(mhtml)
                adm2.landObj.removeGeoJson()
                adm2.kcObj.removeGeoJson()
            }
        },
        /**
         * 显示或隐藏iframe
         */
        showAndHide(){
            if(!this.state) {
                $('#menu2').animate({
                    width: "400px"
                }, 400);
                $('#menu2').css("padding","5px")
            }else{
                $('#menu2').animate({
                    width: "0px"
                }, 400);
                $('#menu2').css("padding","0px")
            }
            this.state = !this.state
        }

    },
    landObj:{
        map: null,
        lands : [],
        points : [],
        view:null,
        overLay:null,
        init(map,view,overLay){
          this.map = map;
          this.view = view;
          this.overLay = overLay;
        },
        createGeoJson(url, sf, zIndex, callback){
            var that = this;
            $.get(url,
                function(result){
                    var features = (new ol.format.GeoJSON()).readFeatures(result)

                    var pointVector = that.createPointVector(features, zIndex)

                    var vector = new ol.layer.Vector({
                        minZoom: 16,
                        source: new ol.source.Vector({
                            features: features
                        })
                        ,style: sf,
                        zIndex: zIndex
                    })
                    callback(vector, pointVector)
                }
            )
        },
        removeGeoJson(){
            if(this.lands.length == 0){
                return;
            }
            for(var layer of this.lands){
                this.map.removeLayer(layer)
            }
            for(var temp of this.points){
                this.map.removeLayer(temp)
            }
            this.lands = []
            this.points = []
        },
        createPointVector(features, zIndex){
            //创建一个点的features
            var pointSource = new ol.source.Vector()
            var iconLayer = new ol.layer.Vector({
                source: pointSource,
                zIndex: zIndex,
                maxZoom: 18
            });

            var fill = new ol.style.Fill({
                color:'#fff'
            })
            for(var feature of features){
                var land_id = feature.get("land_id");
                var mu_area = feature.get("mu_area")
                var iconStyle = new ol.style.Style({
                    text: new ol.style.Text({
                        text: ""+land_id,
                        font: 'bold 14px Calibri,sans-serif',    //字体与大小
                        fill: fill
                    }),
                    image: new ol.style.Icon(({
                        opacity:1,
                        src: '../static/images/mark_b.png'
                    }))
                });
                var center = ol.extent.getCenter(feature.getGeometry().getExtent())
                var iconFeature = new ol.Feature({
                    geometry: new ol.geom.Point(center)
                });
                iconFeature.setStyle(iconStyle)
                iconFeature.set("oid",feature.W)
                iconFeature.set("land_id",land_id)
                iconFeature.set("mu_area",mu_area)
                pointSource.addFeature(iconFeature)
            }
            return iconLayer;
        },
        _addPointLayer(pointLayer){
            this.map.addLayer(pointLayer)
            this.points.push(pointLayer)
        },
        _addLayer(layer){
            this.lands.push(layer)
            this.map.addLayer(layer)
            this.view.fit(layer.getSource().getExtent(),{maxZoom: 16})
        },
        _addInteraction(layers, infoUrl){
            var that = this
            var select = new ol.interaction.Select({
                condition: ol.events.condition.singleClick,
                layers:layers,
                // style: function(feature){
                //     return styleFunction(feature, true)
                // }
            })

            select.on("select",function(e){
                var selected = e.selected;
                if(selected.length > 0){
                    var feature = selected[0]
                    that.showTips(feature, that.getTipsHtml(feature, infoUrl))
                }
            })
            this.map.addInteraction(select)
        },
        addLandGeoJson(url,infoUrl){
            var that = this;
            this.removeGeoJson()
            this.createGeoJson(url, styleFunction2, 100, function(layer, pointLayer){
                that._addLayer(layer)
                that._addPointLayer(pointLayer)
                that._addInteraction([layer,pointLayer], infoUrl)
            });
        },
        showTips(feature,html){
            var center = ol.extent.getCenter(feature.getGeometry().getExtent())
            popup.show(center, html)
        },
        getTipsHtml(feature, infoUrl){

            var oid = feature.get("oid")

            if(!oid){
                oid = feature.W
            }

            var names = ["地块编号","面积(亩)"];
            var values = ['land_id','mu_area'];
            var html='<table class="layui-table"><tbody>';
            for(var i=0;i<names.length;i++){
                if(feature.get(values[i])){
                    var item = '<tr>'
                    item +='<td>'+names[i]+'</td>';
                    item +='<td>'+feature.get(values[i])+'</td>';
                    item += '</tr>'
                    html+=item
                }
            }
            var url = infoUrl + '?objectId=' + oid.substr(oid.indexOf(".") + 1);
            var click = 'tool.open("'+ url +'","80%");'
            console.log(click)
            var item = '<tr>'
            item +='<td></td>';
            item +='<td><button type="button" onclick='+click+' class="layui-btn layui-btn-primary">详情</button></td>';
            item += '</tr>'
            html+=item
            html+=' </tbody></table>'
            return html;
        },
        fitByObjectId(objectId,infoUrl){
            if(this.lands.length >0){
                var feature = this.lands[0].getSource().getFeatureById("all_land."+objectId);
                this.view.fit(feature.getGeometry().getExtent(),{maxZoom: 17})
                this.showTips(feature, this.getTipsHtml(feature, infoUrl))
            }
        }

    },
    kcObj:{
        map: null,
        kcs : [],
        points : [],
        view:null,
        overLay:null,
        init(map,view,overLay){
            this.map = map;
            this.view = view;
            this.overLay = overLay;
        },
        createGeoJson(url, sf, zIndex, callback){
            var that = this;
            $.get(url,
                function(result){
                    var features = (new ol.format.GeoJSON()).readFeatures(result)

                    var pointVector = that.createPointVector(features, zIndex)

                    var vector = new ol.layer.Vector({
                        minZoom: 16,
                        source: new ol.source.Vector({
                            features: features
                        })
                        ,style: sf,
                        zIndex: zIndex
                    })
                    callback(vector, pointVector)
                }
            )
        },
        removeGeoJson(){
            if(this.kcs.length == 0){
                return;
            }
            for(var layer of this.kcs){
                this.map.removeLayer(layer)
            }
            for(var temp of this.points){
                this.map.removeLayer(temp)
            }
            this.kcs = []
            this.points = []
        },
        createPointVector(features, zIndex){
            //创建一个点的features
            var pointSource = new ol.source.Vector()
            var iconLayer = new ol.layer.Vector({
                source: pointSource,
                zIndex: zIndex,
                maxZoom: 18
            });

            var fill = new ol.style.Fill({
                color:'#fff'
            })
            for(var feature of features){
                var objectid = feature.get("objectid")
                var sqr = feature.get("申请人")
                var ksmc = feature.get("矿山名称")
                var kczkz = feature.get("开采主矿种")
                var kqmj = feature.get("矿区面积")

                var iconStyle = new ol.style.Style({
                    text: new ol.style.Text({
                        text: ""+objectid,
                        font: 'bold 14px Calibri,sans-serif',    //字体与大小
                        fill: fill
                    }),
                    image: new ol.style.Icon(({
                        opacity:1,
                        src: '../static/images/mark_b.png'
                    }))
                });
                var center = ol.extent.getCenter(feature.getGeometry().getExtent())
                var iconFeature = new ol.Feature({
                    geometry: new ol.geom.Point(center)
                });
                iconFeature.setStyle(iconStyle)
                iconFeature.set("oid",feature.W)
                iconFeature.set("objectid",objectid)
                iconFeature.set("申请人",sqr)
                iconFeature.set("矿山名称",ksmc)
                iconFeature.set("开采主矿种",kczkz)
                iconFeature.set("矿区面积",kqmj)
                pointSource.addFeature(iconFeature)
            }
            return iconLayer;
        },
        _addPointLayer(pointLayer){
            this.map.addLayer(pointLayer)
            this.points.push(pointLayer)
        },
        _addLayer(layer){
            this.kcs.push(layer)
            this.map.addLayer(layer)
            this.view.fit(layer.getSource().getExtent(),{maxZoom: 16})
        },
        _addInteraction(layers, infoUrl){
            var that = this
            var select = new ol.interaction.Select({
                condition: ol.events.condition.singleClick,
                layers:layers,
                // style: function(feature){
                //     return styleFunction(feature, true)
                // }
            })

            select.on("select",function(e){
                var selected = e.selected;
                if(selected.length > 0){
                    var feature = selected[0]
                    that.showTips(feature, that.getTipsHtml(feature, infoUrl))
                }
            })
            this.map.addInteraction(select)
        },
        addKcGeoJson(url,infoUrl){
            var that = this;
            this.removeGeoJson()
            this.createGeoJson(url, styleFunction2, 100, function(layer, pointLayer){
                that._addLayer(layer)
                that._addPointLayer(pointLayer)
                that._addInteraction([layer,pointLayer], infoUrl)
            });
        },
        showTips(feature,html){
            var center = ol.extent.getCenter(feature.getGeometry().getExtent())
            popup.show(center, html)
        },
        getTipsHtml(feature, infoUrl){

            var oid = feature.get("oid")
            if(!oid){
                oid = feature.W
            }


            var names = ["申请人","矿山名称","开采主矿种","矿区面积"];
            var values = ['申请人','矿山名称','开采主矿种','矿区面积'];
            var html='<table class="layui-table"><tbody>';
            for(var i=0;i<names.length;i++){
                if(feature.get(values[i])){
                    var item = '<tr>'
                    item +='<td>'+names[i]+'</td>';
                    item +='<td>'+feature.get(values[i])+'</td>';
                    item += '</tr>'
                    html+=item
                }
            }
            var url = infoUrl + '?objectId=' + oid.split(".")[1];
            var click = 'tool.open("'+ url +'","80%");'
            console.log(click)
            var item = '<tr>'
            item +='<td></td>';
            item +='<td><button type="button" onclick='+click+' class="layui-btn layui-btn-primary">详情</button></td>';
            item += '</tr>'
            html+=item
            html+=' </tbody></table>'
            return html;
        },
        fitByObjectId(objectId, infoUrl){
            if(this.kcs.length >0){
                var feature = this.kcs[0].getSource().getFeatureById(objectId);
                this.view.fit(feature.getGeometry().getExtent(),{maxZoom: 17})
                this.showTips(feature, this.getTipsHtml(feature, infoUrl))
            }
        }

    }
}
