<!--
 * @Description: 液化工厂图层
 * @Author: 王晓颖
 * @Date: 2021-06-04 14:39:31
 -->
<template>
  <div>
    <slot/>
  </div>
</template>

<script>
import commonMixin from '../base/mixins/common.js'
import axios from 'axios'
export default {
  name: 'LiquidFactoryLayer',
  mixins: [commonMixin('layer')],
  props: {
    size: {
      type: String,
      default: ''
    }, // 大小
    scale: {
      type: Number,
      default: 5
    }, // 图片缩放倍数
    height: {
      type: Number,
      default: 0
    }, // 高度
    offset: {
      type: Number,
      default: -15
    }, // 偏移值
    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, layer, height } = this
      axios.get('static/config/liquidFactory.json').then((res) => {
        res = res.data
        if (res.code === 200) {
          this.data = res.data[0].data
          for (const item of this.data) {
            const graphic = new mars3d.graphic.ModelEntity({
              name: item.name,
              position: [parseFloat(item.value[0]), parseFloat(item.value[1]), height],
              url: 'static/gltf/output/yehuagongchang.gltf',
              style: {
                scale: 3,
                heading: 0,
                minimumPixelSize: 30,
                clampToGround: true
              },
              tooltip: item.name
            })
            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>
