<template>
  <el-container class="smart-city-container">
    <!-- 顶部Header -->
    <el-header class="city-header">
      <div class="header-left">
        <img src="@/assets/images/logo.png" alt="" />
        <!-- <span class="city-name">123123</span> -->
      </div>
      <div class="header-right">
        <div>
          <el-dropdown>
            <span class="username">管理员</span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="handleLogout"
                  >退出登录</el-dropdown-item
                >
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
    </el-header>

    <el-container>
      <!-- 侧边Aside -->
      <el-aside width="220px" class="city-aside">
        <el-menu
          :default-active="$route.path"
          active-text-color="#409EFF"
          background-color="#1f313b"
          text-color="#fff"
          router
        >
          <template v-for="route in menuRoutes" :key="route.path">
            <!-- 无子菜单的项目 -->
            <el-menu-item
              v-if="!route.children || route.children.length === 0"
              :index="route.path"
            >
              <el-icon><component :is="route.meta?.icon || 'menu'" /></el-icon>
              <span>{{ route.meta?.menuName || route.name }}</span>
            </el-menu-item>

            <!-- 有子菜单的项目 -->
            <el-sub-menu v-else :index="route.path">
              <template #title>
                <el-icon
                  ><component :is="route.meta?.icon || 'menu'"
                /></el-icon>
                <span>{{ route.meta?.menuName || route.name }}</span>
              </template>
              <template v-for="child in route.children" :key="child.path">
                <el-menu-item v-if="child.meta?.showInMenu !== false"
                  :index="child.path"
                  style="padding-left: 20px !important"
                >
                  <el-icon><component v-if="child.meta?.icon" :is="child.meta?.icon" /></el-icon>
                  <span>{{ child.meta?.menuName || child.name }}</span>
                </el-menu-item>
              </template>
            </el-sub-menu>
          </template>
        </el-menu>
      </el-aside>

      <!-- 主内容Main -->
      <el-main class="city-main">
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { computed, h, resolveComponent } from "vue";
import { useRouter, useRoute } from "vue-router";

const router = useRouter();

// 计算菜单路由
const menuRoutes = computed(() => {
  const mainRoute = router.options.routes.find((route) => route.path === "/");
  if (!mainRoute?.children) return [];

  return mainRoute.children.filter(
    (route) => !route.meta || route.meta.showInMenu !== false
  );
});

// 处理退出登录
const handleLogout = () => {
  // 清除登录状态
  localStorage.removeItem("token");

  // 跳转到登录页
  router.push("/login");
};
</script>

<style scoped lang="less">
.smart-city-container {
  height: 100vh;
  overflow: hidden;
}

/* 选中菜单项样式 */
.el-menu-item {
  color: #666;
  background-color: #fff;
}
.el-menu-item.is-active {
  background-color: #ecf3fd;
  color: #3d84ee;
  border-right: 4px solid #3d84ee;
}

/* 完全去掉系统管理子菜单的背景色 */
:deep(.el-sub-menu__title) {
  color: #666;
  background-color: #fff !important;
}

.city-header {
  width: 100%;
  height: 64px;
  background: #3db8c5 url("@/assets/images/header-bg.png") no-repeat fixed top
    left / 100% 64px;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .header-left {
    padding-left: 25px;
  }
}

.city-name {
  font-size: 20px;
  font-weight: bold;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.username {
  margin-left: 10px;
  color: #fff;
  cursor: pointer;
}

.city-aside {
  background-color: #fff;
  width: 240px;
  height: 100%;
  box-shadow: 0 6px 10px 0 rgba(36, 36, 63, 0.05);
  overflow: auto;
}

.city-main {
  height: calc(100vh - 60px);
  overflow-y: auto;
  --el-main-padding: 0;
}
.header-right {
  display: flex;
}
</style>
