<template>
<div class="map-wrapper">
  <div ref="map"/>
</div>
</template>

<script>
import china from 'echarts/map/json/china.json'
export default {
  name: 'DiseaseMap',
  props: {
    data: {
      type: Array,
      default() {
        return []
      }
    },
    visualLabel: {
      type: Array,
      default() {
        return ['高', '低']
      }
    },
    visualFormatter: {
      type: [String, Function],
      default() {
        return ({data}) => {
          const {data: info} = data
          return `
          <div class="map-tooltip-info">
            <h3>${data.name}</h3>
            <p><span>联系人：</span>${info.name}</p>
            <p><span>联系电话：</span>${info.phone}</p>
            <p><span>地址：</span>${info.address}</p>
            ${info.type ? `<p><span>监测点类型：</span>${info.type}</p>` : ''}
            ${info.variety ? `<p><span>养殖品种：</span>${info.variety}</p>` : ''}
          </div>
          `
        }
      }
    },
    visualConfig: {
      type: Object,
      default() {
        return {}
      }
    },
  },
  data() {
    return {
      map: null,
      mapName: 'china',
      config: {
        geo: {
          map: 'china',
          left: '1%',
          right: '1%',
          bottom: '3%',
          top: 'auto',
          zoom: 1, //当前视角的缩放比例
          // roam: true, //是否开启平游或缩放
          // scaleLimit: { //滚轮缩放的极限控制
          //   min: 0.8,
          //   max: 2,
          // },
          label: {
            show: true,
            color: '#fff',
            fontSize: 12 * this.sizeRate,
            // normal: {
            //   show: false,
            //   color: '#cfcfcf',
            // },
            emphasis: {
              color: '#fff',
              // fontSize: Math.floor(screen.height * 2 / 100),
              // fontFamily: 'Pangmenzhengdao',
            }
          },
          itemStyle: {
            normal: {
              areaColor: 'rgba(91, 213, 255, 0.2)',
              borderColor: 'rgba(91, 213, 255, 0.9)',
              borderWidth: 0.5,
            },
            emphasis: {
              areaColor: 'rgba(91, 213, 255, 0.2)',
              borderColor: 'rgba(91, 213, 255, 0.9)',
            }
          },
          regions: [],
        },
        series: []
      },
      curProvince: null,
    }
  },
  mounted() {
    this.$nextTick(this.initMap)
  },
  beforeDestroy() {
    this.map && window.removeEventListener('resize', () => this.map.resize())
  },
  computed: {
    sizeRate() {
      return Number((screen.height / 800).toFixed(1))
    },
  },
  methods: {
    initMap() {
      // 初始化echarts
      this.$echarts.registerMap(this.mapName, china)
      this.map = this.$echarts.init(this.$refs.map)
      window.addEventListener('resize', () => this.map.resize())
      // this.addEvent()
      // this.config.series.push({
      //   type: 'scatter', // series图表类型
      //   coordinateSystem: 'geo', // series坐标系类型
      //   data: mapData,
      //   itemStyle: {
      //     color: 'gold'
      //   },
      //   symbolSize: 20 * this.sizeRate,
      //   symbol: 'pin',
      // })
      this.config.tooltip = {
        trigger: 'item',
        confine: true,
        formatter: this.visualFormatter,
        // extraCssText: `background-image:url(${require('@/assets/images/tooltip.png')});background-size:cover;`,
      }
      this.map.setOption(this.config)
    },
    // addEvent() {
    //   // 监听地图点击事件
    //   this.map.on('click', (ev) => {
    //     const {name} = ev.data
    //     this.setRegions(name)
    //     this.curProvince = name
    //     this.$emit('select', name)
    //   })
    //   // 点击空白处则取消选中状态
    //   this.map.getZr().on('click', e => {
    //     if (!e.target && this.curProvince) {
    //       const name = this.curProvince
    //       this.setRegions(null)
    //       this.$emit('select', name)
    //     }
    //   })
    // },
    // setRegions(name) {
    //   this.curProvince = name
    //   if (!name) {
    //     this.config.geo.regions = []
    //     this.map.setOption(this.config)
    //     return
    //   }
    //   let regions = [{
    //     name,
    //     selected: true,
    //     itemStyle: {
    //       areaColor: '#5ad4ff',
    //       shadowColor: 'rgba(0, 0, 0, 1)',
    //       shadowBlur: 10,
    //       shadowOffsetY: 10,
    //       shadowOffsetX: -5,
    //     }
    //   }]
    //   if (this.config.geo.regions.length > 0 && this.config.geo.regions[0].name == name) {
    //     regions = []
    //   }
    //   this.config.geo.regions = regions
    //   this.map.setOption(this.config)
    // },
  },
  watch: {
    '$store.state.aquafarmPoints': {
      handler(cur, past) {
        if (cur && cur !== past && cur.length > 0) {
          this.config.series = [{
            type: 'scatter', // series图表类型
            coordinateSystem: 'geo', // series坐标系类型
            data: cur,
            itemStyle: {
              color: 'gold'
            },
            symbolSize: 14 * this.sizeRate,
            symbol: 'pin',
          }]
          this.map && this.map.setOption(this.config)
        }
      },
      immediate: true,
    }
  }
}
</script>

<style lang="stylus" scoped>
.map-wrapper
  width 42%
  height 94%
  position absolute
  >div
    width 100%
    height 100%
    z-index 99
</style>
<style lang="stylus">
.map-tooltip-info
  color $edgeColor
  padding 1rem
  border .1rem solid $edgeColor
  span
    color #fff
</style>
