<template>
  <div class="dashboard-container">
    <el-button @click="drawPolygon()">绘制围栏</el-button>
    <el-button @click="cleanPolygon()">清除围栏</el-button>
    <el-button @click="savePolygon()">保存围栏</el-button>
    <div id="container"></div>
  </div>
</template>
<script>
import AMapLoader from "@amap/amap-jsapi-loader";
import log from "@/views/monitor/job/log.vue";
import { savePolygon } from "@/api/business/fence";

export default {
  name: "map-view",
  created() {
    this.mapRequest.fenceId = this.$route.query.fenceId;
  },
  data() {
    return {
      map: null,
      _Amap: null,
      mouseTool: null,
      mapRequest: {
        fenceId: 0,
        jwd: {}
      }
    }
  },
  mounted() {
    this.initAMap();
  },
  unmounted() {
    this.map?.destroy();
  },
  methods: {
    initAMap() {
      AMapLoader.load({
        key: "ce38a6b072b603934f6270bd9e97ed49", // 申请好的Web端开发者Key，首次调用 load 时必填
        version: "2.0", // 指定要加载的 JSAPI 的版本，缺省时默认为 1.4.15
        plugins: ["AMap.Scale","AMap.MouseTool"], //需要使用的的插件列表，如比例尺'AMap.Scale'，支持添加多个如：['...','...']
      })
        .then((AMap) => {
          this._Amap = AMap;
          this.map = new AMap.Map("container", {
            // 设置地图容器id
            viewMode: "3D", // 是否为3D地图模式
            zoom: 11, // 初始化地图级别
            center: [116.397428, 39.90923], // 初始化地图中心点位置
          });
          this.mouseTool = new AMap.MouseTool(this.map)
          this.mouseTool.on('draw',this.drawReady)
        })
        .catch((e) => {
          console.log(e);
        });
    },

    drawPolygon () {
      this.mouseTool.polygon({
        strokeColor: "#FF33FF",
        strokeOpacity: 1,
        strokeWeight: 6,
        // strokeOpacity: 0.2,
        fillColor: '#1791fc',
        fillOpacity: 0.4,
        // 线样式还支持 'dashed'
        strokeStyle: "solid",
        // strokeStyle是dashed时有效
        // strokeDasharray: [30,10],
      })
    },

    cleanPolygon () {
      this.mouseTool.close(true)
    },

    drawReady(event) {

      this.mapRequest.jwd=event.obj.getPath();

      console.info('覆盖物对象绘制完成')
    },

    savePolygon() {
      console.log(this.mapRequest.jwd)
      savePolygon(this.mapRequest).then(response => {
        this.$modal.msgSuccess("保存成功");
        this.$router.push({path: '/etcfence/fence'})
      });
    }
  },
};
</script>
<style scoped>
#container{
  padding:0px;
  margin: 0px;
  width: 100%;
  height: 800px;
}
</style>
