<template>
  <div class="app-wrapper">
    <v-sidebar />
    <v-header />
    <div class="main-container">
      <div class="app-main" :class="{ 'content-collapse': sidebar.collapse }">
        <router-view v-slot="{ Component }">
          <transition name="move" mode="out-in">
            <keep-alive :include="[]">
              <component :is="Component"></component>
            </keep-alive>
          </transition>
        </router-view>
      </div>
    </div>
    <Alarm ref="alarmRef" v-if="visible" @close="visible = false" />
  </div>
</template>
<script setup lang="ts">
import { useSidebarStore } from "@/store/sidebar";
import vHeader from "@/components/header.vue";
import vSidebar from "@/components/sidebar.vue";
import Alarm from "@/components/alarm.vue";
import { inject, onBeforeUnmount, onMounted, ref } from "vue";
import { ElMessageBox } from "element-plus";
const ws: any = inject("ws");

// import useWebSocket from "@/utils/webSocket";
// const { onMessage } = useWebSocket();
// onMessage((res) => {
//   console.log(res, "WebSocket  接收服务器消息");
//   if (res.cmd == "warning") {
//     console.log("预警弹窗");
// visible.value = true;
//   }
// });

const sidebar = useSidebarStore();
const alarmRef = ref(null);
const audioPlayer = ref(null);
const visible = ref(false);

const getInclude = (routeList: any) => {
  if (!routeList) return [];
  let list = [];
  routeList.forEach((item) => {
    if (item.meta && item.meta.keepAlive) {
      list.push(item.name);
    }
    if (item.children && item.children.length) {
      list = [...list, ...getInclude(item.children)];
    }
  });
  return list;
};

const handleBeforeUnload = (event: any) => {
  localStorage.removeItem("isReload");
};

onMounted(() => {
  const isReload = localStorage.getItem("isReload");
  // if (isReload !== "true") {
  //   ElMessageBox.alert("由于浏览器安全策略，用户必须点击屏幕才能播放告警声音", "提示", {
  //     confirmButtonText: "OK",
  //     callback: () => {
  //       localStorage.setItem("isReload", "true");
  //     },
  //   });
  // }

  // if (ws.socket == null) {
  //   ws.connect();
  // }
});

onMounted(() => {
  window.addEventListener("beforeunload", handleBeforeUnload);
});

onBeforeUnmount(() => {
  window.removeEventListener("beforeunload", handleBeforeUnload);
});

// const include = getInclude(routes[0].children);
</script>

<style scoped lang="less">
// @media screen and (max-width: 800px) {
//   .main-container {
//     margin-left: 0 !important;
//   }
//   .sidebar-container {
//     width: 0 !important;
//   }
//   .app-wrapper-header {
//     width: 100% !important;
//   }
// }
.app-wrapper {
  position: relative;
  width: 100%;
  height: 100vh;
  background: #f0f2f5;
  overflow: hidden;
  .main-container {
    height: 100vh;
    margin-left: 210px;
    min-height: 100%;
    position: relative;
    transition: 0.3s;
    background-color: #f2f4fa;
  }
  .app-main {
    // height: 100vh;
    height: calc(100vh - 90px);
    position: relative;
    width: 100%;
    display: flex;
    flex-direction: column;
    // padding-top: 70px;
    margin-top: 70px;
    box-sizing: border-box;
    overflow: hidden;
  }
}
</style>
