<template>
  <div style="overflow: hidden">
    <div
      id="topology-hrhx"
      style="width: 100%; height: 100vh; overflow: hidden"
    ></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 { getTopologyId } from "@/api/topology.js";
import { urlParse } from "@/utils/ruoyi";
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.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;
      id = this.$route.fullPath.split("id=")[1];
      await getTopologyId(id)
        .then((res) => {
          if (res.code === 200) {
            this.topologyData = res.data.data;
          }
        })
        .catch((res) => {
          this.topologyData = {};
        });
      if (this.topologyData) {
        let data = JSON.parse(this.topologyData);
        console.log(data)
        if(data.websocket){
          data.websocket= data.websocket + "/" + (urlParse()===''?null:urlParse());
          console.log(data)
        }
        this.topologyData=JSON.stringify(data)
        
      }
      this.canvas.open(this.topologyData);
      this.canvas.data.locked = 10;
      // 或 用window.topology对象 === 上面的canvas
      // topology.open(json);
      // 4. 如果json发送变化，重绘
      this.canvas.render();
      this.$nextTick(()=>{
        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>