<template>
  <div id="layout" class="layout-container">
    <el-aside class="nav-sidebar">
      <div class="logo">
        <el-image src="/turing.png" fit="contain" />
      </div>
      <!-- <el-radio-group v-model="isCollapse" style="margin-bottom: 20px">
        <el-radio-button :value="false">expand</el-radio-button>
        <el-radio-button :value="true">collapse</el-radio-button>
      </el-radio-group> -->
      <el-menu :default-active="activeIndex" class="el-menu-part" router :collapse="isCollapse">
        <el-menu-item index="/">
          <el-icon>
            <House />
          </el-icon>
          <template #title><span>首页</span></template>
        </el-menu-item>
        <el-menu-item index="/alertManagement">
          <el-icon>
            <Management />
          </el-icon>
          <template #title><span>告警列表</span></template>
        </el-menu-item>
        <el-menu-item index="/dataStatistics">
          <el-icon><TrendCharts /></el-icon>
          <template #title><span>数据统计</span></template>
        </el-menu-item>
        <el-menu-item index="/userList">
          <el-icon><Avatar /></el-icon>
          <template #title><span>用户管理</span></template>
        </el-menu-item>
        <el-sub-menu index="1">
          <template #title>
            <el-icon>
              <location />
            </el-icon>
            <span>面板测试</span>
          </template>
          <el-menu-item index="/alertChart">
            <el-icon>
              <WarningFilled />
            </el-icon>
            <template #title><span>功能点1测试</span></template>
          </el-menu-item>
          <el-menu-item index="/statistics">
            <el-icon>
              <document />
            </el-icon>
            <template #title><span>功能点2测试</span></template>
          </el-menu-item>
        </el-sub-menu>
      </el-menu>
    </el-aside>
    <div class="content-layout">
      <!-- 头部区域 -->
      <el-header class="nav-header">

        <div class="header-right">
          <!-- 用户头像下拉菜单 -->
          <el-dropdown trigger="click">
            <span class="el-dropdown-link">
              <el-avatar shape="square"> 用户 </el-avatar>
              <span class="username">{{ username }}</span>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item>
                  <el-icon>
                    <User />
                  </el-icon> 用户中心
                </el-dropdown-item>
                <el-dropdown-item @click="onLogout">
                  <el-icon>
                    <SwitchButton />
                  </el-icon> 退出登录
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>

      <!-- 主内容区域 -->
      <el-main class="main-content">
        <router-view />
      </el-main>

      <!-- 页脚区域 -->
      <el-footer class="nav-footer">
        Powered by AI
      </el-footer>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, watch, onMounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import {
  Document,
  WarningFilled,
  Location,
  User,
  SwitchButton,
  WarnTriangleFilled,
  Setting,
  House,
  Grid,
  Management,
  TrendCharts,
  Avatar
} from '@element-plus/icons-vue';
import { BoxApi } from '@/utils/boxApi';
import { ElMessage } from 'element-plus';

const route = useRoute();
const router = useRouter();
const activeIndex = ref(route.path);
const isCollapse = ref(false);

// 动态获取 username
const username = ref(''); // 默认值为空

// 页面加载时，检查 localStorage 是否存储了用户名
onMounted(() => {
  const storedUsername = localStorage.getItem('username');
  if (storedUsername) {
    username.value = storedUsername;
  } else {
    username.value = '用户'; // 如果 localStorage 没有值，默认显示 "用户"
  }
});

const apiInstance = new BoxApi();

// 监听路由变化，更新菜单的 active 项
watch(
  () => route.path,
  (newPath) => {
    activeIndex.value = newPath;
  }
);

// const onLogout = () => {
//   localStorage.removeItem('alertToken');
//   router.push('/login');
// };
const onLogout = async () => {
  try {
    localStorage.removeItem('alertToken');
    ElMessage.success('退出登录成功');
    router.push('/login');
    await apiInstance.logout();
  } catch (error: any) {
    ElMessage.error(`后台接口调用失败: ${error.message}`);
  }
};

</script>

<style scoped>
.layout-container {
  display: flex;
  height: 100%;
  width: 100%;
  margin: 1px;
  padding: 1px;

}

.nav-sidebar {
  width: 200px;
  background-color: #001529;
  /* width: 200px; */
  color: #fff;
  min-height: 100vh;
  border-top-left-radius: 8px;
  border-bottom-left-radius: 8px;
}

.logo {
  display: flex;
  justify-content: center;
  margin-bottom: 30px;
}

.el-menu-part {
  background-color: transparent;
  color: #fff;
  border: 1px;
  /* width: 200px; */
}

.el-menu-part .el-menu-item {
  /* padding:30px 0; */
  padding: 0px;
  margin: 0;
  text-align: center;
  font-size: 16px;
  font-weight: bold;
  color: #fff;
  background-color: #001529;
  transition: background-color 0.3s ease, color 0.3s ease;
}


/* 悬停样式 */
.el-menu-part .el-menu-item:hover {
  background-color: #001529;
  color: #00aaff;
}

/* 选中样式 */
.el-menu-part .el-menu-item.is-active {
  background-color: #001529;
  color: #eeea07;
}

::v-deep .el-sub-menu__title {
  font-size: 16px;
  font-weight: bold;
  color: #fff;
}

::v-deep .el-sub-menu__title:hover {
  background-color: #001529;
  color: #00aaff;
}


/* 内容布局区域 */
.content-layout {
  display: flex;
  flex-direction: column;
  flex-grow: 1;
  min-height: 100%;
}

/* 头部样式 */
.nav-header {
  background-color: #001529;
  padding: 10px;
  font-size: 18px;
  font-weight: bold;
  color: white;
  height: 58px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  justify-content: right;
  /* flex-shrink: 1; */
}

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

.username {
  margin-left: 10px;
  font-weight: bold;
  font-size: 16px;
  color: #fff;
}

/* 主内容样式 */
.main-content {
  background-color: #f5f8fc;
  flex-grow: 1;
  min-height: 80%;
}

/* 页脚样式 */
.nav-footer {
  background-color: #fff;
  /* padding: 10px; */
  font-size: 16px;
  color: #333;
  height: 2vh;
  display: flex;
  justify-content: center;
  text-align: center;
  /* flex-shrink: 0; */
}</style>
