<!-- 画板 -->
<template>
  <div ref="baseMap" class="base-map">
    <canvas
      v-loading="loading"
      id="floorMap"
      :width="canvasWidth"
      :height="canvasHeight"
      tabindex="0"
    ></canvas>
  </div>
</template>
<script>
import { SGraphView, SGraphScene } from "@persagy-web/graph";
import { SFloorParser, getJsonz, SZoneParser } from "@persagy-web/big/lib";
export default {
  data() {
    return {
      canvasWidth: 0, // 画布的宽
      canvasHeight: 0, // 画布的高
      view: null, // 视图 view
      scene: null, // 场景类
      dataKey: "base/f2a4b3d10d3511eb9a1da1ce4aece47c.jsonz", // 基 路径
      mapUrl: "/image-service/common/file_get?systemId=revit&key=", // 图 key
      loading: false,
    };
  },
  methods: {
    // 初始化
    init() {
      this.clearImg();
      this.view ? (this.view.scene = this.scene) : null;
      // 获取压缩包数据并解压
      this.getMapBlob();
    },
    // 请求获取地图的压缩包
    getMapBlob() {
      const url = this.mapUrl + this.dataKey;
      this.loading = true;

      getJsonz(url)
        .then((data) => {
          // 解析数据并放入压缩包中
          this.parserData(data);
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    // 解析数据并注入 scene 类中
    parserData(data) {
      let parser = new SFloorParser();
      parser.parseData(data);
      parser.spaceList.forEach((t) => {
        // 添加图例
        this.scene.addItem(t);
      });
      parser.wallList.forEach((t) => {
        this.scene.addItem(t);
      });
      parser.virtualWallList.forEach((t) => {
        this.scene.addItem(t);
      });
      parser.doorList.forEach((t) => {
        this.scene.addItem(t);
      });
      parser.columnList.forEach((t) => {
        this.scene.addItem(t);
      });
      parser.casementList.forEach((t) => {
        this.scene.addItem(t);
      });
      // 画板是否可拖动
      if (this.view) {
        this.view.DragMove = true;
        this.view.fitSceneToView();
      }
    },

    // 清空画布
    clearImg() {
      this.scene = new SGraphScene();
      if (this.view) {
        this.view.update();
      }
    },
  },
  watch: {
    // 监听key
    dataKey: {
      handler(val) {
        if (val) {
          this.init();
        }
      },
      immediate: true,
    },
  },
  created() {
    this.clearImg();
  },
  mounted() {
    // 获取 canvas 的宽高
    this.canvasWidth = 800;
    this.canvasHeight = 600;
    // 初始化场景类
    this.view = new SGraphView("floorMap");
    if (this.scene) {
      this.view.scene = this.scene;
    }
  },
};
</script>