<template>
  <div style="overflow: hidden">
    <div id="topology-hrhx" style="width: 100%; height: 100vh"></div>
    <div class="tools">
      <a-button type="primary" @click="onBack">
        <a-icon type="left" /> 返回
      </a-button>
      <div></div>
      <a-button type="primary" style="margin-right: 5px" @click="onSizeWindow">
        <a-icon type="border" />
        适合窗口大小
      </a-button>
      <!-- <a-button type="primary" @click="onSizeOri">
        <a-icon type="fullscreen-exit" />
        实际大小
      </a-button> -->
    </div>
  </div>
</template>
<script>
import { Topology } from "@topology/core";
export const Data = "";
import { encryption, decrypt } from "@/utils/rsautils.js";
import {getTopologyId} from "@/api/topology.js"
export default {
  name:"Preview",
  data() {
    return {
      canvas: {},
      locked: 0,
      showTools: true,
      topologyData:null
    };
  },
  created() {},
  mounted() {
    this.init();
    let that = this;
    window.onresize = () => {
      setTimeout(function () {
        that.onSizeWindow();
      }, 100);
    };
  },
  destroyed() {
    this.canvas.destroy();
    window.onresize = null;
  },
  methods: {
    onBack() {
      // this.$router.push({
      //   path: "/workspace",
      //   query: {
      //     id: this.$route.query.id,
      //   },
      // });
      this.$router.go(-1);
    },
    onSizeWindow() {
      this.canvas.fitView(5);
      this.canvas.centerView(15);
    },
    onSizeOri() {
      this.canvas.open(this.topologyData);
    },
    async init() {
      this.canvas = new Topology("topology-hrhx", {});

      // Data.locked=1;
      // this.topologyData=Data
      // 3. 渲染图形
      // 画布图形数据，可以来自于官网下载的json
      let id = this.$route.query.id;
      if (decrypt(id) === null) {
        id = this.$route.fullPath.split("id=")[1];
        console.log(decrypt(id));
      }
      await getTopologyId(decrypt(id))
        .then((res) => {
          if (res.code === 200) {
            this.topologyData = res.data.data;
          }
        })
        .catch((res) => {
          this.topologyData = {};
        });

      this.canvas.open(this.topologyData);
      this.canvas.data.locked = 1;
      // 或 用window.topology对象 === 上面的canvas
      // topology.open(json);
      // 4. 如果json发送变化，重绘
      this.canvas.render();
      this.onSizeWindow();
      topology.data.socketEvent = true;
      topology.on("websocket", (e) => {
        let msg = JSON.parse(e);
        if (msg.code) {
          if (msg.code === 200) {
            this.$message.success(msg.msg);
          } else {
            this.$message.error(msg.msg);
          }
        }
      });
    },
  },
};
</script>
<style lang="scss">
.tools {
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  display: flex;
  & > div {
    flex-grow: 1;
  }

  button {
    margin: 12px 16px;
  }
}
</style>