<template>
  <div class="layout">
    <aside class="layout-aside">
      <div class="brand">
        <img class="brand-logo" :src="logo" alt="logo">
        <span class="brand-title">飞鸟后台数据平台</span>
      </div>

      <nav class="side-menu">
        <div v-for="item in sideMenus" :key="item.path" class="side-menu__item"
          :class="{ 'is-active': isActive(item.path) }" @click="go(item.path)">
          <span class="side-menu__icon">
            <MenuIcon :name="item.icon" />
          </span>
          <span class="side-menu__text">{{ item.title }}</span>
        </div>
      </nav>
    </aside>

    <div class="layout-main">
      <div class="layout-header">
        <div class="header-right">
          <el-dropdown trigger="click">
            <div class="user-entry">
              <el-avatar :size="32" class="user-avatar">
                <el-icon :size="18">
                  <UserFilled />
                </el-icon>
              </el-avatar>
              <span class="user-name">用户001</span>
              <el-icon class="user-arrow">
                <ArrowDown />
              </el-icon>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item disabled>个人中心</el-dropdown-item>
                <el-dropdown-item divided>退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>

      <main class="layout-content">
        <router-view v-slot="{ Component }">
          <keep-alive v-if="route.meta && route.meta.keepAlive !== false">
            <component :is="Component" />
          </keep-alive>
          <component v-else :is="Component" />
        </router-view>
      </main>
    </div>
  </div>
</template>

<script setup>
import { useRoute, useRouter } from "vue-router";
import { UserFilled, ArrowDown } from "@element-plus/icons-vue";
import { sideMenus } from "./menu";
import MenuIcon from "./MenuIcon.vue";

import logo from "@/assets/layout/logo.png";

const route = useRoute();
const router = useRouter();

const isActive = (path) => route.path === path || route.path.startsWith(path + "/");

const go = (path) => {
  if (route.path !== path) {
    router.push(path);
  }
};
</script>

<style lang="scss" scoped>
.layout {
  display: flex;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #f0f2f5;
}

.layout-aside {
  width: 280px;
  flex-shrink: 0;
  background: #000;
  display: flex;
  flex-direction: column;
  color: rgba(255, 255, 255, 0.6);
}

.brand {
  height: 76px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 16px;
  background: url('@/assets/layout/title-bg.png') no-repeat center center;
  background-size: 100% 100%;
}

.brand-logo {
  width: 29px;
  height: 32px;
  object-fit: contain;
}

.brand-title {
  font-size: 24px;
  letter-spacing: 2px;
  font-family: "PangMenZhengDao";
  background: linear-gradient(180deg, #FFD489 0%, #FFFFFF 100%);
  // background: linear-gradient(to bottom, #F3C11D, #ffffff);
  background-clip: text;
  color: transparent;
  text-decoration: none;
}

.side-menu {
  padding: 13px 0 12px 20px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.side-menu__item {
  height: 60px;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  border-radius: 4px 0 0 4px;
  cursor: pointer;
  color: #9a9a9a;
  transition: background 0.2s, color 0.2s;

  &:hover {
    color: #FFD489;
  }

  &.is-active {
    font-weight: 500;
    color: #FFD489;
    background: linear-gradient(90deg, rgba(255, 212, 137, 0.18) 0%, rgba(25, 25, 25, 0) 100%);
  }
}

.side-menu__icon {
  display: inline-flex;

  :deep(.menu-icon) {
    filter: grayscale(1);
    transition: filter 0.2s;
  }
}

.side-menu__item.is-active .side-menu__icon,
.side-menu__item:hover .side-menu__icon {
  :deep(.menu-icon) {
    filter: none;
  }
}

.side-menu__text {
  font-size: 18px;
  line-height: 30px;
  letter-spacing: 2px;
}

.layout-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.layout-header {
  height: 76px;
  flex-shrink: 0;
  background: #fff;
  // box-shadow: 0px 2px 2px 0px rgba(0, 0, 0, 0.1);
  box-shadow: 0px -2px 2px 0px rgba(0, 0, 0, 0.1) inset;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding: 0 24px;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-entry {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  outline: none;
}

.user-avatar {
  background: #e8e8e8;
  color: #8c8c8c;
}

.user-name {
  font-size: 14px;
  color: #333;
}

.user-arrow {
  color: #999;
  font-size: 12px;
}

.layout-content {
  flex: 1;
  min-height: 0;
  overflow: auto;
  background: #F5F5F5;
}
</style>
