<template>
  <div class="app-container">
      <div id="map-container" tabindex="0"></div>
      <div v-show="flag" id="panel"></div>
      <div v-show="mapClick && flag" id='bt-wrapper'>
        <div id='bt'>点击去高德地图</div>
      </div>  
  </div>
</template>

<script>
import AMap from 'AMap'
import moment from 'moment'
import { fetchDeviceList } from '@/api/counter'
export default {
  name: 'map2',
  data() {
    return {
      map: null,
      flag: false,
      mapClick: false,
      defaultPositions: [
        {
          title: '海洋公园',
          position: [114.186739, 22.250872],
          count: 66,
          signal: 23,
          energy: 4333
        },
        {
          title: '香港大学',
          position: [114.149176, 22.286245],
          count: 232,
          signal: 23,
          energy: 4333
        },
        {
          title: '南丫北容村',
          position: [114.126261, 22.22863],
          count: 87,
          signal: 33,
          energy: 4333
        },
        {
          title: '大帽山',
          position: [114.124105, 22.401088],
          count: 66,
          signal: 23,
          energy: 4223
        },
        {
          title: '西贡郊野公园',
          position: [114.370966, 22.411628],
          count: 616,
          signal: 23,
          energy: 4556
        },
        {
          title: '盐田',
          position: [114.223565, 22.380771],
          count: 66,
          signal: 23,
          energy: 4433
        },
        {
          title: '清水',
          position: [114.312821, 22.301076],
          count: 2336,
          signal: 23,
          energy: 4333
        }
      ]
    }
  },
  methods: {
    initMap() {
      this.map = new AMap.Map('map-container', {
        center: [114.143472, 22.284105],
        resizeEnable: true,
        zoom: 10
      })
      // 标记点测试数据
      const markers = []
      this.defaultPositions.forEach((item, index) => {
        const marker = new AMap.Marker({
          position: item.position,
          zIndex: 10,
          title: item.title,
          map: this.map
        })
        const infoWindow = this.instantiateInforWindow(item)
        AMap.event.addListener(marker, 'click', () => {
          infoWindow.open(this.map, marker.getPosition())
        })
        markers.push(marker)
      })
      AMap.plugin(['AMap.ToolBar', 'AMap.Scale'], () => {
        this.map.addControl(new AMap.ToolBar())
        this.map.addControl(new AMap.Scale())
      })
    },
    createInfoWindow(title, content) {
      const info = document.createElement('div')
      info.className = 'info'

      // 可以通过下面的方式修改自定义窗体的宽高
      // info.style.width = "400px"
      // 定义顶部标题
      const top = document.createElement('div')
      const titleD = document.createElement('div')
      titleD.className = 'title'
      const closeX = document.createElement('img')
      top.className = 'info-top'
      titleD.innerHTML = title
      closeX.src = 'https://webapi.amap.com/images/close2.gif'
      closeX.onclick = this.closeInfoWindow

      top.appendChild(titleD)
      top.appendChild(closeX)
      info.appendChild(top)

      // 定义中部内容
      const middle = document.createElement('div')
      middle.className = 'info-middle'
      middle.innerHTML = content
      info.appendChild(middle)

      // 定义底部内容
      const bottom = document.createElement('div')
      bottom.className = 'info-bottom'
      bottom.style.position = 'relative'
      bottom.style.top = '0px'
      bottom.style.margin = '0 auto'
      const sharp = document.createElement('img')
      sharp.src = 'https://webapi.amap.com/images/sharp.png'
      bottom.appendChild(sharp)
      info.appendChild(bottom)
      // console.log(info)
      return info
    },
    instantiateInforWindow(item) {
      const content = []
      const curTime = moment().format('YYYY-MM-DD HH:mm:ss')
      content.push('<div class="count">' + item.count + '</div>')
      content.push('<div class="time">时间：' + curTime + '</div>')
      content.push('<div><span>信号：' + item.signal + '</span><span class="energy">电量：' + item.energy + '</span></div>')
      content.push('<div>区域：尖沙咀</div>\n')
      const infoWindow = new AMap.InfoWindow({
        isCustom: true,
        closeWhenClickMap: true,
        content: this.createInfoWindow(item.title, content.join('\n')),
        offset: new AMap.Pixel(16, -45)
      })
      return infoWindow
    },
    closeInfoWindow() {
      this.map.clearInfoWindow()
    },
    getDevicesInitMap(params) {
      fetchDeviceList(params).then(response => {
        this.deviceList = response.data.results
        this.coordinate = this.deviceList[0].coordinate
        if (this.coordinate === null) {
          this.coordinate = [113.203460828994, 22.64902452257]
        }
        const map = new AMap.Map('map-container')
        const drivingOptions = {
          map: map,
          // city: '中山市',
          panel: 'panel'
          // cityd:'乌鲁木齐',
          // policy: AMap.DrivingPolicy.LEAST_TIME
        }
        const button = document.getElementById('bt')
        AMap.plugin(['AMap.ToolBar', 'AMap.Scale', 'AMap.Driving'], () => {
          map.addControl(new AMap.ToolBar())
          map.addControl(new AMap.Scale())
          const driving = new AMap.Driving(drivingOptions)
          driving.search(new AMap.LngLat(113.344095, 22.673091), new AMap.LngLat(113.203460828994, 22.64902452257), (status, result) => {
            button.onclick = () => {
              driving.searchOnAMAP({
                origin: result.origin,
                destination: result.destination
              })
            }
          })
        })
        // const driving = new AMap.Driving(drivingOptions)
        this.flag = true
        // if (AMap.UA.mobile) {
        //   document.getElementById('panel').style.display = 'none'
        //   // document.getElementById('bt-wrapper').style.display = 'normal'
        // }
      })
    }
  },
  created() {
    this.mapClick = this.$store.getters.mapClick
    // if (!this.mapClick) {
    //   this.flag = true
    // }
  },
  mounted() {
    if (this.mapClick) {
      const device_id = this.$store.getters.deviceID
      this.getDevicesInitMap({ device_id })
    } else {
      this.initMap()
    }
  }
}
</script>

<style lang="css">
  #map-container {
    width: 100%;
    height: 80%;
    overflow: hidden;
    position: absolute;
  }
  .info {
    border: solid 1px silver;
  }
  div.info-top {
    position: relative;
    background: none repeat scroll 0 0 #F9F9F9;
    border-bottom: 1px solid #CCC;
    border-radius: 5px 5px 0 0;
  }
  div.info-top .title {
    display: inline-block;
    color: rgb(236, 110, 37);
    font-size: 14px;
    font-weight: bold;
    line-height: 31px;
    padding: 0 10px;
  }
  div.info-top img {
    position: absolute;
    top: 10px;
    right: 10px;
    transition-duration: 0.25s;
  }
  div.info-top img:hover {
    box-shadow: 0px 0px 5px #000;
  }
  div.info-middle {
    font-size: 12px;
    padding: .4rem .8rem .5rem .5rem;
    line-height: 20px;
    background-color: white;
  }
  .info-middle .count {
    display: flex;
    justify-content: flex-end;
    font-size: 1.5rem;
    font-weight: bold;
    color: #666;
    margin: 0 0 .4rem 0;
  }
  .info-middle span.energy {
    margin-left: 1.8rem;
  }
  div.info-bottom {
    height: 0px;
    width: 100%;
    clear: both;
    text-align: center;
  }
  div.info-bottom img {
    position: relative;
    z-index: 104;
  }
  #panel {
    position: absolute;
    background-color: white;
    max-height: 80%;
    overflow-y: auto;
    top: 10px;
    right: 10px;
    width: 250px;
    border: solid 1px silver;
    margin-top: 100px;
  }
  @media only screen and (max-width: 768px) {
    #panel {
      display: none;
    }
  }
  #bt-wrapper {
    position: absolute;
    width: 2rem;
    bottom: 15%;
    right: 10rem;
  }
  #bt {
    font-size: .8rem;
    text-align: center;
    cursor: pointer;
    border-radius: 5px;
    background-color: #0D9BF2;
    padding: 6px;
    width: 160px;
    color: white;
    margin: 0 auto;
  }
</style>
