<template>
  <div id="mapContainer" />
</template>

<script>
const {SMap, Plugins} = window
export default {
  name: 'MonitorMap',
  data() {
    return {
      map: null,
    }
  },
  mounted() {
    this.$nextTick(this.initMap)
  },
  computed: {
    sizeRate() {
      return Math.floor(screen.height * 1.48 / 100 / 11)
    },
  },
  methods: {
    initMap() {
      this.map = new SMap.Map('mapContainer', {
        viewMode: '2D',
        // center: [0, 0],
        // zooms: [6, 7],
        mapStyle: 'smap://styles/dark',
      }).on(SMap.MapEvent.maploaded, () => {
        this.map.hideXQ_Poly()
        this.map.hideXQ_Point()
        this.map.hideMPZ()
        this.addListener()
        // this.map.enableMouseEvent(false) // 禁用鼠标
        this.$emit('complete')
      })
    },
    addListener() { // 触发地图点击事件
      this.map.on(SMap.MapEvent.click, (view, eventParamter) => {
        const {x, y} = eventParamter.mapPoint
        console.log([x, y])
        view.hitTest(eventParamter).then(res => {
          // const {x, y} = res.results[0].graphic.geometry.centroid
          // console.log([x, y])
          if (res.results && res.results.length > 0) {
            // console.log('click', res)
            this.$emit('event', res.results[0] && res.results[0].graphic.attributes)
          }
        })
      })
    },

    // 添加点
    addPoint({key, data, labelKey = null, icon = 'test.png', size = 14, color}) {
      if (!this.map) return
      const points = this.getMapPoints({data, key, labelKey, icon, size, color})
      this.map.add(points)
      return points
      // const overlayGroup = new SMap.OverlayGroup(points)
      // this.map.add(overlayGroup)
      // return overlayGroup
    },

    // 删除点
    removePoint(points) {
      if (!this.map || !points) return
      this.map.remove(points)
    },
    // 将数据转换为符合地图的点数组
    getMapPoints({data = [], key, labelKey, icon, size, color = '#47B3FF'}) {
      return data.map(item => {
        const result = {
          icon: new SMap.Icon({
            size: Array.isArray(size) ? new SMap.Size(size[0], size[1]) : new SMap.Size(size, size),
            image: require(`@/assets/images/${item.icon || icon}`),
          }),
          attributes: {key, ...item},
          position: [item.communityMinx || item.X || item.x || item.gpsx, item.communityMiny || item.Y || item.y || item.gpsy, 0],
        }
        if (labelKey) {
          result.label = new SMap.Label({
            text: (item[labelKey] || item[key]) + '',
            size,
            color,
          })
        }
        return new SMap.Marker(result)
      })
    },

    /**
     * ------  聚焦点位 -------
     * @param {String} x [必填 - x坐标]
     * @param {String} y [必填 - y坐标]
     * @param {Number} zoom [可选 - 放大比例（默认为6）]
     */
    focus(x, y, zoom = 6) {
      if (!this.map || !x || !y ) {
        console.error('传入的坐标点不能为空')
        return
      }
      this.map.setZoomAndCenter(zoom, [x, y])
    },

    /**
     * ------  放大 -------
     */
    zoomIn() {
      this.map.zoomIn()
    },

    /**
     * ------  缩小 -------
     */
    zoomOut() {
      this.map.zoomOut()
    },
    // 添加面
    addPolygon({paths, fillColor = 'rgba(255,255,255,.3)', strokeColor = 'transparent', attributes = {}, style = 'solid', strokeWeight = 1, label = null}) {
      const polygon = new SMap.Polygon({
        paths: paths.map(item => new SMap.LngLat(item[0], item[1])),
        attributes,
        fillColor,
        style,
        strokeColor,
        strokeStyle: style,
        strokeWeight,
        label,
      })
      this.map.add(polygon)
    },
    // 添加线状覆盖物
    addPolyLine({paths, attributes = {}, strokeColor = 'rgba(51,145,255,.6)', width = 2}) {
      const polyline = new SMap.Polyline({
        path: paths.map(item => new SMap.LngLat(item[0], item[1])),
        attributes,
        cap: 'square',
        strokeColor,
        style: 'solid',
        lineJoin: 'round',
        width,
      })
      this.map.add(polyline)
    },
    // 添加边界
    addBoundary({name, weight = 10, count = 10, color = 'rgba(51,145,255,.6)', maskColor = [0, 0, 0, 0]}) {
      const boundary = {
        boundaryType: 'jd_boundary',
        boundaryDefinition: `name like '%${name}%'`,
        boundarydistance: weight,
        bounarycount: count,
        boundaryColor: color,
        maskColor: maskColor,
      }
      const Boundary = new Plugins.MaskBoundary(this.map.view)
      Boundary.add(boundary)
      return Boundary
    },
  },
}
</script>

<style lang="stylus">
  .esri-component.esri-attribution.esri-widget
    display none !important
</style>
<style lang="stylus" scoped>
#mapContainer
  width 100%
  height 100vh
  background-color #001021
  position fixed
  top 0
  left 0
  right 0
  bottom 0
</style>
