
import GisUtils from '@/utils/gis.js';
import {
  fromLonLat
} from 'ol/proj'
import { getUserGeo, getUserGeoList } from "@/api/dataScreen/stockCooperative/index.js";
import { treeselect, getDept, listDeptExcludeChild } from "@/api/system/dept";
import { getInfo } from "@/api/login";
import { getConfigKey } from "@/api/system/config";
import { getPermanentListByDeptId, listPermanent } from "@/api/asset/permanent.js";
import { getResourceListByDeptId, listResource } from "@/api/asset/resource.js";
import { attachmentList } from "@/api/common/uploadAttachment.js";
let gis = null;
export default {
  components: {
  },
  data () {
    return {
    };
  },
  computed: {
  },
  created () {
  },
  mounted () {
    this.initMap();
  },
  methods: {
    // 加载地图
    initMap () {
      gis = new GisUtils('map2')
      gis.addTianDiTuLayer()
      gis.addAnnotationLayer()
      const initZoom = 11;
      //初始加载当前用户级别的geojson数据
      getUserGeo().then((res) => {
        if (res.code == 200) {
          // 存在geojson数据，加载边界图层，定位到当前位置
          if (res.data.geoJson != null && res.data.geoJson != "") {
            // 解析geojson数据,添加轮廓图层
            require(["@/assets/maps/" + res.data.geoJson], (data) => {

              this.map.addLayer(boundaryLayer);

              //定位查询位置
              const maxXMap = boundaryLayer.values_.source.featuresRtree_.rbush_.data.maxX;
              const maxYMap = boundaryLayer.values_.source.featuresRtree_.rbush_.data.maxY;
              const minXMap = boundaryLayer.values_.source.featuresRtree_.rbush_.data.minX;
              const minYMap = boundaryLayer.values_.source.featuresRtree_.rbush_.data.minY;
              //获取边界区域的中心位置
              const center = ol.extent.getCenter([maxXMap, maxYMap, minXMap, minYMap]);
              this.map.getView().animate({
                // 只设置需要的属性即可
                center: center, // 中心点
                zoom: initZoom, // 缩放级别
                rotation: undefined, // 缩放完成view视图旋转弧度
                duration: 1000, // 缩放持续时间，默认不需要设置
              });
            });
          } else { // 不存在geojson数据，直接定位到当前位置
            gis.getMapContainer().getView().animate({
              // 只设置需要的属性即可
              center: fromLonLat([res.data.lng, res.data.lat]), // 中心点
              zoom: initZoom, // 缩放级别
              rotation: undefined, // 缩放完成view视图旋转弧度
              duration: 1000, // 缩放持续时间，默认不需要设置
            });
          }
          //村级组级只定位坐标点   deptLevel：  1 组级、2 村级、3 乡镇级、4 区县级
          if (res.data.deptLevel === '3' || res.data.deptLevel === '4') {
            let locationIcon;
            if (res.data.deptLevel === '3') {
              locationIcon = "red.png";
            } else if (res.data.deptLevel === '4') {
              locationIcon = "yellow.png";
            }
            //获取用户级别的下级区域
            setTimeout(() => {
              getUserGeoList(res.data.deptId).then((res) => {
                if (res.code == 200) {
                  this.addDeptLayer(res.data, require(`./icon/${locationIcon}`))
                }
              });
            }, 500);
          } else {
            let lowestOrder = [];
            lowestOrder.push(res.data);
            this.addDeptLayer(lowestOrder, require('./icon/green.png'));
          }
        }
      });

      gis.getMapContainer().on("click", (evt) => {
        let feature = gis.getMapContainer().forEachFeatureAtPixel(
          evt.pixel,
          (feature) => feature
        );
        if (feature) {
          // 镇级：加载村级坐标点
          if (feature.get('deptLevel') === '3') {
            gis.getMapContainer().getView().animate({
              center: fromLonLat([feature.get('lng'), feature.get('lat')]), // 中心点
              zoom: 12, // 缩放级别
              rotation: undefined, // 缩放完成view视图旋转弧度
              duration: 1000, // 缩放持续时间，默认不需要设置
            });
            //根据镇级deptID获取下级部门集合，并创建坐标点
            getUserGeoList(feature.get('deptId')).then((res) => {
              if (res.code == 200) {
                let content = res.data;
                if (this.villagePointLayer) {
                  gis.getMapContainer().removeLayer(this.villagePointLayer);
                  this.villagePointLayer = "";
                }
                this.villagePointLayer = this.addDeptLayer(content, require('./icon/green.png'));
              }
            });
          }
        }
      });

      // 缩小地图，重新加载页面
      gis.getMapContainer().on("moveend", (evt) => {
        const zoom = gis.getMapContainer().getView().getZoom();
        if (this.villagePointLayer && zoom < 10.5) {
          gis.getMapContainer().removeLayer(this.villagePointLayer);
          this.villagePointLayer = "";
        }
      });
    },
    // 创建矢量数据源
    addDeptLayer (nextDeptSet, locationIcon) {
      let features = [];
      nextDeptSet.forEach(item => {
        let fs = gis.getFeature(item, locationIcon)
        features.push(fs);
      });
      gis.getVectorLayerByFs(features)
      gis.mapSetFit(features)
    },
    // 获取资源列表
    getResourceList (deptId) {
      getResourceListByDeptId(deptId).then(response => {
        if (response.data && response.data.length > 0) {
          this.addResourceLayer(response.data);
        }
      });
    },
    // 添加资源图层
    addResourceLayer (resourceList) {
      let features = [];
      resourceList.forEach(item => {
        if (item.theGeom != null && item.theGeom !== '') {
          let fs = gis.getFeature3(item)
          features.push(fs);
        }

      });


      gis.getVectorLayerByFs(features)
      gis.mapSetFit(features)
    },
    getAllList () {
      //三务公开
      // this.getList(null);
      // //零工公开
      // this.getTempWorkerOpenList();
      // //重大事项公开
      // this.getMajorEventOpenList();
      // //补贴资金管理
      // this.getSubsidyFundsList();
      // //固定资产
      // this.getPermanentListList();
      // //资源资产
      // this.getResourceListList();
      // //合同信息
      // this.getContractionByPermanentList();
      // //资金情况
      // this.getFundAndBankAmountByDeptIdArray();
    },
  }
};
