<template>
<div class="map-wrapper">
  <BorderBox class="sum">
    <p>主要品种</p>
    <h2>草鱼</h2>
  </BorderBox>
  <div class="map" ref="map"/>
</div>
</template>

<script>
const mock = [
  {name: '江苏省渔业技术推广中心扬中基地', location: [119.809835, 32.307434], to: '陕西', toLocation: [108.944866, 34.349753], value: 13.4},
  {name: '江苏省渔业技术推广中心扬中基地', location: [119.809835, 32.307435], to: '广西', toLocation: [118.065326, 25.905837], value: 11.3},
  {name: '江苏省渔业技术推广中心扬中基地', location: [119.809835, 32.307436], to: '山西', toLocation: [115.894445, 28.76435], value: 2.4},
  {name: '江苏省渔业技术推广中心扬中基地', location: [119.809835, 32.307437], to: '湖北', toLocation: [114.174298, 30.571852], value: 15.6},
  {name: '江苏省渔业技术推广中心扬中基地', location: [119.809835, 32.307438], to: '云南', toLocation: [102.942748, 24.027546], value: 5.3},
  {name: '江苏省渔业技术推广中心扬中基地', location: [119.809835, 32.307439], to: '福建', toLocation: [118.065326, 25.905837], value: 5.5},
  {name: '江苏省渔业技术推广中心扬中基地', location: [119.809835, 32.307440], to: '广东', toLocation: [113.484399, 23.230846], value: 6},
  {name: '江苏省渔业技术推广中心扬中基地', location: [119.809835, 32.307441], to: '辽宁', toLocation: [120.530564, 40.789036], value: 7.2},
  {name: '江苏省渔业技术推广中心扬中基地', location: [119.809835, 32.307442], to: '湖南', toLocation: [112.031013, 27.29511], value: 5.4},
  {name: '江苏省渔业技术推广中心扬中基地', location: [119.809835, 32.307443], to: '新疆', toLocation: [86.716332, 42.486489], value: 3.2},
  {name: '江苏省渔业技术推广中心扬中基地', location: [119.809835, 32.307444], to: '浙江', toLocation: [120.273002, 29.120389], value: 7},
]
import china from 'echarts/map/json/china.json'
import BorderBox from '../border-box'
export default {
  name: 'MigrateMap',
  components: {
    BorderBox,
  },
  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',
          layoutCenter: ['50%', '38%'],
          layoutSize: '100%',
          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',
      //   formatter: this.visualFormatter,
      //   // extraCssText: `background-image:url(${require('@/assets/images/tooltip.png')});background-size:cover;`,
      // }
      this.config.tooltip = {
        trigger: 'item',
      }
      this.config.series = [
        {
          'type': 'lines',
          'zlevel': 2,
          'effect': {
            'show': true,
            'period': 4,
            'trailLength': 0.02,
            'symbol': 'arrow',
            'symbolSize': 5
          },
          'lineStyle': {
            'normal': {
              'width': 2,
              'opacity': 0.5,
              'curveness': 0.3,
              color: ({value}) => {
                if (value > 12) {
                  return '#dd393a'
                } else if (value > 6) {
                  return '#fbc006'
                } else {
                  return '#58e61c'
                }
              }
            }
          },
          'tooltip': {
            'trigger': 'item',
            textStyle: {
              fontSize: 12 * this.sizeRate,
            },
            confine: true,
            formatter: ({data}) => {
              return `
                <div class="map-tooltip-info">
                  <h3>${data.fromName}</h3>
                  <p><span>销往省份：</span>${data.toName}</p>
                  <p><span>销量：</span>${data.value}万尾</p>
                </div>
              `
            }
          },
          data: mock.map(item => {
            return {
              fromName: item.name,
              toName: item.to,
              value: item.value,
              coords: [item.location, item.toLocation],
            }
          }),
        },
        {
          'name': '收货地址',
          'type': 'effectScatter',
          'coordinateSystem': 'geo',
          'zlevel': 2,
          'rippleEffect': {
            'period': 4,
            'brushType': 'stroke',
            'scale': 4
          },
          'tooltip': {
            'trigger': 'item'
          },
          'label': {
            'normal': {
              'show': false,
              'position': 'left',
              'offset': [
                -5,
                5
              ],
              'formatter': '{b}'
            },
            'emphasis': {
              'show': true
            }
          },
          'hoverAnimation': true,
          'symbol': 'circle',
          'symbolSize': 5,
          'itemStyle': {
            'normal': {
              'show': true,
              opacity: .5,
              // 'color': 'rgba(245,216,47,.5)'
              color: ({data}) => {
                const {value} = data
                if (value[2] > 12) {
                  return '#dd393a'
                } else if (value[2] > 6) {
                  return '#fbc006'
                } else {
                  return '#58e61c'
                }
              }
            }
          },
          data: mock.map(item => {
            return {
              name: item.to,
              value: [item.toLocation[0] + '', item.toLocation[1] + '', item.value],
            }
          }),
        },
        {
          'name': '发送地址',
          'type': 'scatter',
          'coordinateSystem': 'geo',
          'zlevel': 2,
          'tooltip': {
            'trigger': 'item'
          },
          'label': {
            'normal': {
              'show': true,
              'position': 'right',
              'color': '#00ffff',
              'formatter': '{b}',
              'textStyle': {
                'color': '#00ffff'
              }
            },
            'emphasis': {
              'show': true
            }
          },
          'symbol': 'circle',
          'symbolSize': 5,
          'itemStyle': {
            'normal': {
              'show': false,
              'color': 'rgba(16,228,253,.5)'
            }
          },
          'data': [
            {
              'name': '江苏省渔业技术推广中心扬中基地',
              'value': [
                '119.809835',
                '32.307434',
                100
              ]
            },
          ]
        }
      ]
      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: 20 * this.sizeRate,
  //           symbol: 'pin',
  //         }]
  //         this.map && this.map.setOption(this.config)
  //       }
  //     },
  //     immediate: true,
  //   }
  // }
}
</script>

<style lang="stylus" scoped>
.map-wrapper
  width 50%
  height 70%
  left 25%
  position absolute
  >.map
    width 100%
    height 100%
    z-index 99
  >.sum
    width 30%
    // margin 0 auto
    margin 1rem 0 0 1rem
    text-align center
    font-weight bold
    h2
      color $edgeColor
      font-family $font-pang
      font-size 1.5rem
      font-weight 400
</style>
<style lang="stylus">
.map-tooltip-info
  color $edgeColor
  padding 1rem
  border .1rem solid $edgeColor
  span
    color #fff
</style>
