<template>
  <div class="main-layout">
    <div class="header" :class="{ [name]: true }">
      <span class="title-span">
        <el-button
          :icon="Back"
          circle
          type="primary"
          @click="router.back()"
          v-if="showBack"
        />
        <span v-if="!name">不可移动文物管理平台</span>
      </span>
      <el-dropdown class="avatar" v-if="user">
        <span>
          <el-avatar :src="user.head" />
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item @click="logoutHandler">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
    <div class="content">
      <ly-slide class="slide" v-if="user && !['pano', 'map'].includes(name)" />
      <div class="view">
        <RouterView v-slot="{ Component }">
          <KeepAlive :include="['pano', 'map', 'query-map', 'query-pano']">
            <component :is="Component" />
          </KeepAlive>
        </RouterView>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { Back } from "@element-plus/icons-vue";
import { router } from "@/router";
import { computed } from "vue";
import { user, logout } from "@/store/user";
import { errorHook } from "@/request/state";
import lySlide from "./slide/index.vue";

const name = computed(() => router.currentRoute.value.meta?.navClass as string);
const routeName = computed(() => router.currentRoute.value.name as string);
const logoutHandler = () => {
  logout();
  router.replace({ name: "login" });
};
errorHook.push((code) => {
  if (code === 4008) {
    router.replace({ name: "login" });
  }
});

const showBack = computed(() => {
  if (name.value === "pano") {
    return true;
  } else {
    return user && routeName.value === "map";
  }
});
</script>

<style lang="scss" scoped>
.main-layout {
  --border-color: #dcdfe6;
  display: flex;
  flex-direction: column;
  height: 100%;
}
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 10px;

  &:not(.pano, .map) {
    border-bottom: 1px solid var(--border-color);
    flex: 0 0 auto;
  }
  &.pano,
  &.map {
    pointer-events: none;
    z-index: 9;
    position: absolute;
    width: 100%;

    .avatar {
      display: none;
    }
    * {
      pointer-events: all;
    }
  }
}

.content {
  flex: 1;
  overflow: hidden;
  display: flex;

  .slide {
    width: 200px;
    flex: 0 0 auto;
    border-right: 1px solid var(--el-border-color);
  }

  .view {
    flex: 1;
    overflow: hidden;
    background-color: var(--bgColor);
    flex-direction: column;
    display: flex;

    &.full {
      width: 100%;
    }

    .player {
      flex: 0 0 auto;
      height: 32px;
    }

    .main {
      margin: 0 16px 16px;
      display: flex;
      flex-direction: column;
      overflow: hidden;
      flex: 1;
    }
  }
}

.title-span {
  display: flex;
  align-items: center;
  > span {
    margin-left: 10px;
  }
}
</style>
