<template>
  <el-container class="smart-city-container">
    <!-- 顶部Header -->
    <el-header class="city-header">
      <div class="header-left">
        <span class="city-name"></span>
      </div>
      <!-- </div> -->
      <div class="header-right">
        <div>
          <el-dropdown>
            <span class="username">管理员</span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item>个人中心</el-dropdown-item>
                <el-dropdown-item>系统设置</el-dropdown-item>
                <el-dropdown-item @click="handleLogout"
                  >退出登录</el-dropdown-item
                >
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </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.meta?.parent" :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.meta.parent">
              <template #title>
                <el-icon
                  ><component :is="route.meta?.icon || 'menu'"
                /></el-icon>
                <span>{{ route.meta.parent }}</span>
              </template>
              <el-menu-item
                :index="route.path"
                style="padding-left: 70px !important"
              >
                {{ route.meta.menuName }}
              </el-menu-item>
            </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 } from "vue";
import { useRouter } from "vue-router";

const router = useRouter();

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

const hanldeChangeLang = (type) => {
  localStorage.setItem("lang", type);
};

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

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

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

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

.city-header {
  width: 100%;
  height: 64px;
  background: #3db8c5
    url("https://apaas-static.oss-cn-beijing.aliyuncs.com/app/header_bg.jpg")
    no-repeat fixed top left / 100% 64px;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

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