<!--
 * @Description: 监控图层
 * @Author: 王晓颖
 * @Date: 2021-06-07 16:23:01
 -->
<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: 'CameraLayer',
  mixins: [commonMixin('layer')],
  props: {
    scale: {
      type: Number,
      default: 0.5
    }, // 图片缩放倍数
    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, scale } = this
      const date = getToday()
      // const date = '2021-04-19'
      getEarthquakeList(date).then((res) => {
        const data = res.data
        for (const item of data) {
          const img = 'static/images/map/camera.png'
          const graphic = new mars3d.graphic.BillboardEntity({
            name: '监控点',
            position: new mars3d.LatLngPoint(item.lng, item.lat, height),
            style: {
              image: img,
              width: size,
              height: size,
              scale: scale,
              clampToGround: true,
              horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
              verticalOrigin: Cesium.VerticalOrigin.BOTTOM
            }
          })
          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>
