<template>
  <div class="layout">
    <aside class="layout-aside" :class="{ 'is-collapse': isCollapse }">
      <div class="brand">
        <img class="brand-logo" :src="logo" alt="logo">
        <span v-show="!isCollapse" class="brand-title">飞鸟后台数据平台</span>
      </div>

      <el-menu
        :default-active="activeMenu"
        :collapse="isCollapse"
        :collapse-transition="false"
        class="side-menu"
        @select="go"
      >
        <el-menu-item v-for="item in sideMenus" :key="item.path" :index="item.path">
          <span class="side-menu__icon">
            <MenuIcon :name="item.icon" />
          </span>
          <template #title>
            <span class="side-menu__text">{{ item.title }}</span>
          </template>
        </el-menu-item>
      </el-menu>
    </aside>

    <div class="layout-main">
      <div class="layout-header">
        <div class="layout-header__collapse" @click="toggleCollapse">
          <el-icon :size="20">
            <Expand v-if="isCollapse" />
            <Fold v-else />
          </el-icon>
        </div>
        <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 { computed, ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import { UserFilled, ArrowDown, Fold, Expand } 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 isCollapse = ref(false);

const activeMenu = computed(() => {
  const matched = sideMenus.find(
    (item) => route.path === item.path || route.path.startsWith(item.path + "/")
  );
  return matched?.path || route.path;
});

const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value;
};

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);
  transition: width 0.2s ease;
  overflow: hidden;

  &.is-collapse {
    width: 64px;
  }
}

.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%;
  flex-shrink: 0;
}

.layout-aside.is-collapse .brand {
  justify-content: center;
  padding: 0;
}

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

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

.side-menu {
  flex: 1;
  border-right: none !important;
  background: transparent !important;
  padding: 13px 0 12px;

  &:not(.el-menu--collapse) {
    width: 100%;
    padding-left: 20px;
  }

  :deep(.el-menu-item) {
    height: 60px;
    line-height: 60px;
    margin: 0;
    padding: 0 16px !important;
    border-radius: 4px 0 0 4px;
    color: #9a9a9a;
    background: transparent !important;

    &:hover {
      color: #FFD489 !important;
      background: transparent !important;
    }

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

  &.el-menu--collapse {
    width: 64px;

    :deep(.el-menu-item) {
      justify-content: center;
      padding: 0 !important;
      border-radius: 0;
    }
  }
}

.side-menu__icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  margin-right: 12px;
  flex-shrink: 0;

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

.side-menu.el-menu--collapse .side-menu__icon {
  margin-right: 0;
}

.side-menu :deep(.el-menu-item.is-active .menu-icon),
.side-menu :deep(.el-menu-item:hover .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) inset;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
}

.layout-header__collapse {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  color: #666;
  cursor: pointer;
  border-radius: 4px;
  transition: color 0.2s, background 0.2s;

  &:hover {
    color: #333;
    background: #f5f5f5;
  }
}

.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 {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow: auto;
  background: #F5F5F5;
}
</style>
