<!--
 * @Description: 管理站图层
 * @Author: 王晓颖
 * @Date: 2021-06-04 10:34:41
 -->
<template>
  <div>
    <slot/>
  </div>
</template>

<script>
import commonMixin from '../base/mixins/common.js'
import { getEarthquakeList } from '@/api/pipe'
import { getToday } from '@/utils/dateutils'
export default {
  name: 'EarthquakeLayer',
  mixins: [commonMixin('layer')],
  props: {
    scale: {
      type: Number,
      default: 1
    }, // 图片缩放倍数
    height: {
      type: Number,
      default: 0
    }, // 高度
    size: {
      type: Number,
      default: 70
    }, // 图片尺寸
    show: {
      type: Boolean,
      default: false
    } // 是否可见
  },
  data() {
    return {
      layer: null, // 图层
      data: [] // 完整数据
    }
  },
  watch: {
    show(val) {
      console.log('watch show: ' + val)
      if (val) {
        this.initLayer()
      } else {
        this.removeLayer()
      }
    }
  },
  methods: {
    load() {
      console.log('load')
    },
    initLayer() {
      const { mars3d, map } = this
      if (map) {
        if (this.show) {
          if (this.layer) {
            this.removeLayer()
          }
        }
        const graphicLayer = new mars3d.layer.GraphicLayer()
        this.layer = graphicLayer
        map.addLayer(graphicLayer)
        this.loadData()
      }
    },
    loadData() {
      const { mars3d, Cesium, layer, height, size } = this
      const date = getToday()
      // const date = '2021-04-19'
      getEarthquakeList(date).then((res) => {
        if (res.data.length === 0) {
          this.$message.info('今日无地震')
        } else {
          const data = res.data.map(item => {
            return {
              name: item['WD53_09'],
              lng: parseFloat(item['WD53_05'].trim()),
              lat: parseFloat(item['WD53_06'].trim()),
              level: item['WD53_07'].trim(),
              deep: item['WD53_08'].trim(),
              time: item['WD53_04'].trim()
            }
          })
          for (const item of data) {
            const img = 'static/images/map/earthquake.png'
            const graphic = new mars3d.graphic.BillboardEntity({
              name: '地震',
              position: [item.lng, item.lat, height],
              style: {
                image: img,
                width: size,
                height: size,
                scale: 0.5,
                clampToGround: true,
                horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
                verticalOrigin: Cesium.VerticalOrigin.BOTTOM
              }
            })
            var html = ` <div class="popup-win"><span class="title">${item.name}</span><br/>
                        <div style="color:#FFB861">震级：${item.level} 级</div>
                        <div style="color:#FFB861">震源深度：${item.deep} km</div>
                        <div style="color:#63AEFF">时间：${item.time}</div>
                        </div>`
            graphic.bindTooltip(html, { anchor: [0, -20] }).openTooltip()
            layer.addGraphic(graphic)
          }
        }
      })
    },
    // 移除图层
    removeLayer() {
      const { map, layer } = this
      if (this.layer) {
        map.removeLayer(layer)
        this.layer = null
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>

</style>
