/**
 * Created by Administrator on 2021/4/5.
 */
define(['jquery', "template", "Tools", "paging", 'dateTime', 'swiper'], function ($, template, Tools) {
    //数据存储
    var module = {
        data: {
            propertyList:'',
            map: '',                // 地图图层
        },
    };
    var tools = new Tools();

    module.init = function (page) {
        tools.doGet(assetGet+getQueryVariable('id'), {translate_dict: 1}, module.assetDetail , true);
        tools.doGet(webList, {}, module.webList , true);
        // var map = new BMapGL.Map('container'); // 创建Map实例
        // map.centerAndZoom(new BMapGL.Point(116.320938,39.950026), 18); // 初始化地图,设置中心点坐标和地图级别
        // map.enableScrollWheelZoom(false); // 开启鼠标滚轮缩放
    };

    module.useContent = function (data) {
        if (data.code == 200) {
            module.data.phone = data.data.phone;
            module.data.leader = data.data.leader;
            var propertyDetailData = template('propertyDetailData', module.data);
            $("#propertyDetailContent").html(propertyDetailData);
            setTimeout(() => {
                initMap(data.data);
            }, 500);

        }
    }

    initMap = function (dept) {
        document.getElementById("container").innerHTML = "";
        // 定义地图投影
        let projection = new ol.proj.Projection({
            code: "EPSG:3857",
            units: "degrees",
        });
        // 定义地图图层
        let aerial = new ol.layer.Tile({
            source: new ol.source.XYZ({
                url: "http://t{0-7}.tianditu.com/img_w/wmts?" +
                    "SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles" +
                    "&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=cc4aba6e967096098249efa069733067",
            }),
            isGroup: true,
            name: "卫星影像图",
        });
        let yingxzi = new ol.layer.Tile({
            source: new ol.source.XYZ({
                url: "http://t{0-7}.tianditu.com/DataServer?T=cia_w&x={x}&y={y}&l={z}&tk=cc4aba6e967096098249efa069733067",
            }),
            isGroup: true,
            name: "天地图文字标注--卫星影像图",
        });
        //加载地图
        module.data.map = new ol.Map({
            controls: ol.control.defaults({attribution: false, zoom: false, rotate: false}).extend([]),
            layers: [aerial, yingxzi],
            projection: projection,
            target: "container",
            view: new ol.View({
                center: ol.proj.fromLonLat([dept.lng, dept.lat]), // 地图中心坐标
                zoom: 16,
                minZoom: 1, //地图缩小限制
                maxZoom: 18, //地图放大限制
            }),
            /*interactions: ol.interaction.defaults({
              doubleClickZoom: false,   // 双击放大地图
              // mouseWheelZoom: false,    // 鼠标滚轮放大地图
              // shiftDragZoom: false,     // shift + 鼠标左键拖拽 放大地图
            })*/
        });

        if (module.data.propertyDetail && module.data.propertyDetail.theGeom) {
            const permanentLayer = new ol.layer.Vector({
                source: new ol.source.Vector({
                    features: new ol.format.GeoJSON().readFeatures(module.data.propertyDetail.theGeom),
                }),
                name: 'permanentDetail',
                style: new ol.style.Style({
                    image: new ol.style.Icon({
                        //设置图标偏移
                        anchor: [0.5, 1],
                        //标注样式的起点位置
                        anchorOrigin: "top-right",
                        //X方向单位：分数
                        anchorXUnits: "fraction",
                        //Y方向单位：像素
                        anchorYUnits: "pixels",
                        //偏移起点位置的方向
                        offsetOrigin: "top-right",
                        //透明度
                        opacity: 0.9,
                        //图片路径
                        src: '/infoport/static/images/zc_icon.png',
                    })
                })
            });
            module.data.map.addLayer(permanentLayer);
            module.data.map.getView().setCenter(ol.extent.getCenter(permanentLayer.getSource().getExtent()));
        }
    }

    module.webList = function (data) {
        if (data.code == 200) {
            var content = data.data;
            module.data.webList = content;
            var webData = template('webData', module.data);
            $("#webContent").html(webData);
            var titData = template('titData', module.data);
            $("#titContent").html(titData);
            $('#ewm').attr('src', 'static/images/'+content.top.bz);
        }
    }

    module.assetDetail = function(data){
        if (data.code == 200) {
            var content = data.data;
            module.data.propertyDetail = content;
            tools.doGet(attachmentList, {
                tableId:content.id,
                tableName: 't_asset_permanent',
                bizPath: 'asset'
            }, module.attachmentDetail , true);
            tools.doGet(treeselectSecond+content.deptId, {}, module.useContent , true);
            // var propertyDetailData = template('propertyDetailData', module.data);
            // $("#propertyDetailContent").html(propertyDetailData);
            // $('#dialog').css('display','block');
        }
    }

    module.attachmentDetail = function(data){
        if (data.code == 200) {
            var content = data.rows;
            var attachmentList = [];
            content.map(res=>{
                if (res.fileName.indexOf('png')>-1||res.fileName.indexOf('jpg')>-1){
                    attachmentList.push(res);
                }
            })
            module.data.attachment = attachmentList;
            var propertyDetailData = template('propertyDetailData', module.data);
            $("#propertyDetailContent").html(propertyDetailData);
            $('#dialog').css('display','block');
        }
    }

    //获取地址栏参数
    function getQueryVariable(variable){
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i=0;i<vars.length;i++) {
            var pair = vars[i].split("=");
            if(pair[0] == variable){return pair[1];}
        }
        return(false);
    }

    return module;
});
