<template>
  <div class="map">
    <div id="map" ref="rootmap" />
    <div class="community-info" v-show="isShowInfo">
      <div class="community-info__close" title="关闭" @click="closeInfo">
        <i class="iconClean iconfont"></i>
      </div>
      <div class="community-info__header">
        <h2>{{ info.resourceName }}</h2>
        <div>{{ info.areaName }}{{ info.address }}</div>
      </div>
      <div class="community-info__wrapper">
        <div class="community-info__title">
          <h2>基本信息</h2>
        </div>
        <p>{{ info.information }}</p>
      </div>
    </div>
  </div>
</template>

<script>
import AMapLoader from '@amap/amap-jsapi-loader'
import {mapState} from 'vuex'

export default {
  name: 'AppResourceMapDv',
  label: '资源地图',
  provide() {
    return {
      root: this
    }
  },
  props: {
    instance: Function,
    dict: Object
  },
  data() {
    return {
      map: null,
      community: '',
      isShowInfo: false,
      info: {},
      satellite: null,
      zoom: 11,
      choosedId: '',
      center: []
    }
  },
  computed: {
    ...mapState(['user'])
  },

  mounted() {
    this.getCorpLocation()
  },

  methods: {
    getCorpLocation(){
      this.instance.post("/app/appdvcpconfig/getCorpLocation").then(res=>{
        if(res.code==0){
          this.initMap(res.data)
        }
      })
    },

    getList () {
      this.instance.post(`/app/appresourceinfo/listAll`).then(res => {
        if (res.code == 0) {
          const points = res.data.map(item => {
            return {
              ...item,
              lnglat: [item.lng, item.lat],
              id: item.id,
              corpId: item.corpId,
              areaName:item.areaName,
              name: item.resourceName
            }
          })

          this.addMakert(points)
        }
      })
    },

    changeZoom(isAdd) {
      const zoom = isAdd ? this.map.getZoom() + 1 : this.map.getZoom() - 1
      this.map.setZoom(zoom, false, 600)
    },

    getInfo (id) {
      this.instance.post(`/app/appresourceinfo/queryDetailById?id=${id}`).then(res => {
        if (res.code === 0) {
          this.info = res.data
          this.isShowInfo = true
        }
      })
    },

    toCenter() {
      this.map.setZoomAndCenter(this.zoom, this.center, false, 600)
    },

    renderClusterMarker(context) {
      let el = `<div class="polymeric">
        <div class="polymeric-container">
          <p>${context.count}</p>
        </div>
      </div>`

      let offset = new AMap.Pixel(-9, -9)
      context.marker.setContent(el)
      context.marker.setOffset(offset)
      context.marker.lnglat = context.clusterData[0].lnglat

      context.marker.on('click', e => {
        this.map.setZoomAndCenter(this.map.getZoom() + 3, e.target.lnglat, false, 500)
      })
    },

    renderMarker(context) {
      const buildId = context.data[0].id

      let el = `<div class="mark" id="buildId-${buildId}">
         <img src="${context.data[0].icon || 'https://cdn.cunwuyun.cn/dvcp/ply/icon.png'}">
      </div>`

      context.marker.setContent(el);
      context.marker.setAnchor("center")
      context.marker.id = `${buildId}`
      context.marker.data = JSON.stringify(context.data[0])
      context.marker.lnglat = context.data[0].lnglat

      context.marker.on('click', e => {
        this.choosedId = e.target.id
        this.getInfo(e.target.id)
        context.marker.setContent(el);
        document.querySelectorAll('.mark').forEach(el => {
          el.classList.remove('mark-active')
        })
        document.querySelector(`#buildId-${e.target.id}`).add('mark-active')
        this.map.setZoomAndCenter(this.map.getZoom() + 0.000000001, e.target.lnglat, false, 300)
      })
    },

    closeInfo () {
      this.info = {}
      this.isShowInfo = false
      this.choosedId = ''
      this.map.setZoom(this.map.getZoom() + 0.0001)
    },

    addMakert(points) {
      new AMap.MarkerClusterer(this.map, points, {
        gridSize: 60,
        maxZoom: 15,
        clusterByZoomChange: false,
        renderClusterMarker: this.renderClusterMarker,
        renderMarker: this.renderMarker
      })
    },

    initMap({lng,lat}) {
      this.center = [lng,lat];
      AMapLoader.load({
        key: '54a02a43d9828a8f9cd4f26fe281e74e',
        version: '2.0',
        plugins: ['AMap.ToolBar', 'AMap.Scale', 'AMap.MouseTool', 'AMap.MarkerClusterer'],
        AMapUI: {
          version: '1.1',
          plugins: []
        }
      }).then((AMap) => {
        this.map = new AMap.Map('map', {
          resizeEnable: true,
          zooms: [6, 20],
          center: [lng, lat],
          zoom: this.zoom,
          mapStyle: 'amap://styles/40f6fba77127e061a058f670433a67ec'
        })
        this.satellite = new AMap.TileLayer.Satellite()
        this.getList()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  .map {
    position: relative;
    width: 100%;
    height: 100%;
    overflow: hidden;

    .community-info__header {
      margin-bottom: 10px;
      padding: 12px 20px;
      background-image: linear-gradient(270deg, rgba(11, 158, 255, 0.2) 0%, rgba(2, 81, 227, 0.2) 100%);

      h2 {
        max-width: 360px;
        line-height: 28px;
        margin: 0;
        color: #fff;
        font-size: 20px;
        font-weight: 500;
        margin-bottom: 10px;
      }

      div {
        display: flex;
        align-items: center;
        color: #D2E0FF;
        font-size: 12px;
      }

      span {
        display: block;
        margin-top: 4px;
        font-style: normal;
        color: #82C5FF;
        font-size: 14px;
      }
    }

    .community-info {
      position: absolute;
      top: 10px;
      right: 10px;
      width: 400px;
      max-height: calc(100% - 20px);
      overflow-y: overlay;
      overflow-x: hidden;
      z-index: 111;
      background: rgba(7,11,35,0.50);
      border: 1px solid #14345F;

      .community-info__close {
        position: absolute;
        right: 0;
        top: 0;
        padding: 16px 12px 0 12px;
        font-size: 16px;
        color: #8899bb;
        cursor: pointer;

        &:hover {
          opacity: 0.6;
        }
      }

      &::-webkit-scrollbar {
        width: 6px;
        height: 1px;
      }

      &::-webkit-scrollbar-thumb {
        border-radius: 6px;
        background: rgba(144, 147, 153, .5);
      }

      .community-info__wrapper {
        padding: 0 20px 40px;

        .community-info__title {
          display: flex;
          align-items: center;
          justify-content: space-between;
          margin-bottom: 20px;

          h2 {
            position: relative;
            width: 169px;
            padding: 0 20px;
            font-size: 15px;
            color: #fff;
            font-weight: 700;
            background: url(https://cdn.cunwuyun.cn/dvcp/ply/title-bg.png) no-repeat;
            background-size: 169px 30px;
            background-position-y: -5px;
            background-position-x: -10px;
          }

          span {
            width: 52px;
            height: 24px;
            line-height: 24px;
            color: #fff;
            font-size: 14px;
            text-align: center;
            border-radius: 12px;
          }
        }

        p {
          line-height: 1.5;
          font-size: 14px;
          color: #82C5FF;
        }
      }
    }

    ::v-deep .ol-zoom {
      display: none !important;
      top: inherit !important;
      bottom: 0.5em !important;
    }

    div {
      box-sizing: border-box;
    }

    #map {
      width: 100%;
      height: 100%;
    }

    ::v-deep .amap-logo, ::v-deep .amap-copyright {
      display: none !important;
    }

    ::v-deep .amap-icon {
      width: 40px !important;
      height: 40px !important;

      img {
        width: 100%;
        height: 100%;
      }
    }

    .no-more {
      display: block;
      height: 60px;
      line-height: 60px;
      margin-top: 2px;
      text-align: center;
      font-size: 12px;
      color: #999;
      width: 100%;
    }
  }

  .community {
    display: flex;
    align-items: center;
    position: relative;
    height: 28px;
    padding: 0 10px;
    background: #0F8F64;
    border-radius: 26px;
    color: #fff;
    font-size: 12px;
    cursor: pointer;

    &.color1 {
      background: #2266FF;

      em:after {
        border-top-color: #2266FF !important;
      }
    }

    &.color2 {
      background: #F46159;

      em:after {
        border-top-color: #F46159 !important;
      }
    }

    em {
      position: absolute;
      bottom: -6px;
      left: 50%;
      transform: translate(-50%, 0);

      &::after {
        position: absolute;
        bottom: -6px;
        left: 0;
        width: 0;
        height: 0;
        border: 6px solid #0F8F64;
        border-bottom-color: transparent;
        border-left-color: transparent;
        border-right-color: transparent;
        transform: translate(-50%, 0);
        overflow: hidden;
        content: ' ';
      }
    }

    i {
      padding-right: 2px;
      position: relative;
      color: #ffc928;
      font-size: 16px;
      font-style: normal;
    }
  }

  ::v-deep .polymeric {
    display: flex;
    position: relative;
    align-items: center;
    justify-content: center;
    width: 62px;
    height: 62px;
    border-radius: 50%;
    cursor: pointer;
    user-select: none;

    &.polymeric-active {
      .polymeric-container {
        background: #F46159;
      }

      &::after {
        background-color: #F46159;
      }
    }

    &::after {
      position: absolute;
      z-index: -1;
      width: 62px;
      height: 62px;
      border-radius: 50%;
      -webkit-animation: warn 1s ease-out 0s infinite;
      animation: warn 1s ease-out 0s infinite;
      background-color: rgba(15, 143, 100, 1);
      transform: translate(-50%, -50%);
      content: " ";
    }

    .polymeric-container {
      display: flex;
      align-items: center;
      justify-content: center;
      flex-direction: column;
      width: 62px;
      height: 62px;
      border-radius: 50%;
      background: rgba(15, 143, 100, 1);

      p {
        text-align: center;
        width: 58px;
        color: #fff;
        font-size: 18px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }

      p:first-child{
        font-size: 14px;
      }

      h2 {
        color: #fff;
        font-weight: normal;
        font-size: 12px;
      }
    }
  }

  ::v-deep .mark{
    user-select: none;
    width: 50px;
    height: 50px;
    border-radius: 50%;
    cursor: pointer;

    &:hover {
      opacity: 0.7;
    }
  }

  @-webkit-keyframes warn {
    0% {
      transform: scale(.5);
      opacity: 1
    }

    30% {
      opacity: .5
    }

    to {
      transform: scale(1.8);
      opacity: 0
    }
  }

  @keyframes warn {
    0% {
      transform: scale(.5);
      opacity: 1
    }

    30% {
      opacity: .5
    }

    to {
      transform: scale(1.4);
      opacity: 0
    }
  }
</style>
