<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">
      <header 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>
      </header>

      <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: #1a1a1a;
  display: flex;
  flex-direction: column;
  color: #cfcfcf;
}

.brand {
  height: 64px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 16px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

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

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

.side-menu {
  padding: 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

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

  &:hover {
    color: #e8d48b;
    background: rgba(212, 175, 55, 0.08);
  }

  &.is-active {
    color: #d4af37;
    background: linear-gradient(90deg, rgba(212, 175, 55, 0.28), rgba(212, 175, 55, 0.08));
  }
}

.side-menu__icon {
  display: inline-flex;
  width: 22px;
  height: 22px;
}

.side-menu__text {
  font-size: 15px;
}

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

.layout-header {
  height: 64px;
  flex-shrink: 0;
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding: 0 24px;
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.04);
}

.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>
