<template>
<div class="map-wrapper">
  <div id="map" ref="map"/>
  <div v-if="data.length > 0" class="visualmap">
    <p>{{visualLabel[0]}}</p>
    <div class="bar" />
    <p>{{visualLabel[1]}}</p>
  </div>
</div>
</template>

<script>
import china from 'echarts/map/json/china.json'
const mapData = [
  { name: '北京', value: 177 },
  { name: '天津', value: 42 },
  { name: '河北', value: 102 },
  { name: '山西', value: 81 },
  { name: '内蒙古', value: 17 },
  { name: '辽宁', value: 67 },
  { name: '吉林', value: 182 },
  { name: '黑龙江', value: 100 },
  { name: '上海', value: 24 },
  { name: '江苏', value: 299 },
  { name: '浙江', value: 114 },
  { name: '安徽', value: 29 },
  { name: '福建', value: 316 },
  { name: '江西', value: 91 },
  { name: '山东', value: 19 },
  { name: '河南', value: 137 },
  { name: '湖北', value: 26 },
  { name: '湖南', value: 114 },
  { name: '重庆', value: 91 },
  { name: '四川', value: 25 },
  { name: '贵州', value: 62 },
  { name: '云南', value: 83 },
  { name: '西藏', value: 9 },
  { name: '陕西', value: 80 },
  { name: '甘肃', value: 256 },
  { name: '青海', value: 10 },
  { name: '宁夏', value: 18 },
  { name: '新疆', value: 67 },
  { name: '广东', value: 123 },
  { name: '广西', value: 59 },
  { name: '海南', value: 14 }
]
export default {
  name: 'ChinaMap',
  props: {
    data: {
      type: Array,
      default() {
        return []
      }
    },
    visualLabel: {
      type: Array,
      default() {
        return ['高', '低']
      }
    },
    visualFormatter: {
      type: [String, Function],
      // default: '{b}<br/>综合种养产量：{c}吨'
      default() {
        return (e) => {
          const {data} = e
          return `
            <h3>${data.name}</h3>
            <p>养殖面积：${data.area}（公顷）</p>
            <p>淡水养殖产量：${data.water}（吨）</p>
            <p>稻田产量：${data.farm}（吨）</p>
            `
        }
      }
    },
    visualConfig: {
      type: Object,
      default() {
        return {}
      }
    },
  },
  data() {
    return {
      map: null,
      mapName: 'china',
      config: {
        geo: {
          map: 'china',
          left: '5%',
          top: '5%',
          label: {
            normal: {
              show: false,
            },
            emphasis: {
              color: '#fff',
            }
          },
          itemStyle: {
            normal: {
              areaColor: 'rgba(0, 191, 255, 0.1)',
              borderColor: 'rgba(0, 191, 255, 0.6)',
              borderWidth: 0.4,
            },
            emphasis: {
              areaColor: '#5ad4ff',
              color: '#fff'
            }
          },
          regions: [],
        },
        series: []
      }
    }
  },
  mounted() {
    this.$nextTick(this.initMap)
  },
  methods: {
    initMap() {
      // 初始化echarts
      this.$echarts.registerMap(this.mapName, china)
      this.map = this.$echarts.init(this.$refs.map)
      this.map.setOption(this.config)
      this.addEvent()
    },
    addEvent() {
      // 监听地图点击事件
      this.map.on('click', (ev) => {
        const {name} = ev.data
        this.setRegions(name)
        this.$emit('select', name)
      })
    },
    setRegions(name) {
      if (!name) {
        this.config.geo.regions = []
        this.map.setOption(this.config)
        return
      }
      let regions = [{
        name,
        selected: true,
        itemStyle: {
          areaColor: '#5ad4ff',
          color: 'red',
          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: {
    data(cur, past) {
      if (cur && cur !== past && cur.length > 0) {
        this.config.series.push({
          type: 'map',
          map: this.mapName,
          geoIndex: 0,
          data: this.data,
        })
        this.config.tooltip = {
          trigger: 'item',
          formatter: this.visualFormatter,
        }
        this.config.visualMap = Object.assign({
          show: false,
          inRange: {
            opacity: 0.5,
            color: ['rgba(91, 213, 255, 0.1)', 'rgba(91, 213, 255, 0.6)']
          }
        }, this.visualConfig)
        this.map.setOption(this.config)
      }
    }
  }
}
</script>

<style lang="stylus" scoped>
.map-wrapper
  width 100%
  height 100%
  position absolute
  #map
    width 100%
    height 100%
  .visualmap
    color #fff
    position absolute
    top 50%
    left 5%
    text-align center
    .bar
      width 2rem
      height 6rem
      background linear-gradient(to bottom, rgba(91, 213, 255, 0.6), rgba(91, 213, 255, 0.1))
</style>
