<script setup lang="ts">
import { ElNotification } from "element-plus";
import { Manager, Socket } from "socket.io-client";
import { computed, onMounted, onUnmounted, ref } from "vue";
import { useRoute } from "vue-router";

const route = useRoute();
const key = computed(() => route.path);

const socket = ref<Socket | null>(null);

//错误消息通知
const handleErrorMsgNotify = () => {
  //websocket连接
  const manager = new Manager(import.meta.env.VITE_WEBSOCKET_API);
  socket.value = manager.socket("/");
  //收到错误通知
  socket.value.on("error_message", onErrorMsg);
};

//错误消息处理
const onErrorMsg = ({ message }: { message: any }) => {
  ElNotification({
    title: "爬虫错误提示",
    message,
    type: "error",
  });
};

onMounted(() => {
  handleErrorMsgNotify();
});

onUnmounted(() => {
  if (socket.value) {
    socket.value.disconnect();
  }
});
</script>

<template>
  <section class="app-main">
    <router-view v-slot="{ Component }" :key="key">
      <transition name="fade-transform" mode="out-in">
        <component :is="Component" />
      </transition>
    </router-view>
  </section>
</template>

<style scoped>
.app-main {
  /*50 = navbar  */
  background-image: url("@/assets/picture/background1.png");
  background-size: 100% 100%;
  background-repeat: no-repeat;
  min-height: calc(100vh - 50px);
  width: 100%;
  position: relative;
  overflow-y: auto; /* 修改为auto允许垂直滚动 */
  overflow-x: hidden; /* 防止水平滚动 */
}
.fixed-header + .app-main {
  padding-top: 50px;
}
</style>
