<template>
  <div id="layout" class="layout-container">
    <!-- 侧边栏 -->
    <el-aside v-if="!isFullScreen" class="nav-sidebar" :style="{ width: isCollapse ? '64px' : '180px' }">
      <div class="logo" v-if="!isCollapse">
        <el-image src="/turing.png" fit="contain" />
      </div>
      <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-menu-item index="/settings">
          <el-icon>
            <Setting />
          </el-icon>
          <template #title><span>告警设置</span></template>
        </el-menu-item>
        <el-menu-item index="/viewList">
          <el-icon>
            <Location />
          </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="/test">
            <el-icon><WarningFilled /></el-icon>
            <template #title><span>布局备份</span></template>
          </el-menu-item>
          <!-- <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-menu-item index="/cameras">
            <el-icon><Document /></el-icon>
            <template #title><span>功能点3测试</span></template>
          </el-menu-item> -->
        </el-sub-menu>
      </el-menu>
    </el-aside>

    <div class="content-layout">
      <!-- 头部区域 -->
      <el-header v-if="!isFullScreen" class="nav-header">
        <!-- 收缩/展开按钮 -->
        <div>
          <el-icon @click="toggleCollapse" style="cursor: pointer; margin-right: 20px;">
            <component :is="isCollapse ? Expand : Fold" />
          </el-icon>
          <el-icon @click="toggleFullScreen" style="cursor: pointer; margin-right: 20px;">
            <component :is="FullScreen" />
          </el-icon>
        </div>
        <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 @click="goToUserManagement">
                  <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-button v-if="isFullScreen" class="exit-fullscreen-button" type="primary" @click="toggleFullScreen">
        <el-icon>
          <Notification />
        </el-icon>退出全屏
      </el-button>
      <!-- 页脚区域 -->
      <el-footer v-if="!isFullScreen" 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,
  House, Management, TrendCharts, Avatar, Fold, Expand, Setting, FullScreen, Notification
} 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);  // 控制侧边栏收缩状态
const username = ref('');  // 存储用户名

const isFullScreen = ref(false);
const goToUserManagement = () => {
  router.push('/userList');  // 跳转到用户管理页面
};

onMounted(() => {
  const storedUsername = localStorage.getItem('username');
  username.value = storedUsername ? storedUsername : '用户';

});

watch(
  () => route.path,
  (newPath) => {
    activeIndex.value = newPath;
  }
);

const toggleFullScreen = () => {
  const elem = document.documentElement as HTMLElement & {
    webkitRequestFullscreen?: () => Promise<void>;
    msRequestFullscreen?: () => Promise<void>;
  };

  if (!isFullScreen.value) {
    if (elem.requestFullscreen) {
      elem.requestFullscreen();
    } else if (elem.webkitRequestFullscreen) {
      elem.webkitRequestFullscreen(); // 兼容 Safari
    } else if (elem.msRequestFullscreen) {
      elem.msRequestFullscreen(); // 兼容 IE/Edge
    }
  } else {
    const doc = document as Document & {
      webkitExitFullscreen?: () => Promise<void>;
      msExitFullscreen?: () => Promise<void>;
    };

    if (doc.exitFullscreen) {
      doc.exitFullscreen();
    } else if (doc.webkitExitFullscreen) {
      doc.webkitExitFullscreen(); // 兼容 Safari
    } else if (doc.msExitFullscreen) {
      doc.msExitFullscreen(); // 兼容 IE/Edge
    }
  }
  isFullScreen.value = !isFullScreen.value;

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

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

<style scoped>
.layout-container {
  display: flex;
  height: 100%;
  width: 100%;
  /* overflow: hidden; */
}

.nav-sidebar {
  background-color: #001529;
  color: #fff;
  min-height: 100vh;
  border-top-left-radius: 5px;
  border-bottom-left-radius: 5px;
  transition: width 1s ease;
}

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

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

.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;
  /* max-height: 100%; */
  height: 100vh;
  /* overflow-x: auto;   */
  /* min-width: 0;   */
}

.nav-header {
  background: linear-gradient(to right, rgba(0, 21, 41, 1), rgba(2, 16, 99, 0.9));
  padding: 10px;
  font-size: 18px;
  font-weight: bold;
  color: white;
  height: 68px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  /* justify-content: right; */
  /* max-width: 100%; */
}

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

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

.main-content {
  background-color: #f5f8fc;
  flex-grow: 1;
  max-height: 100vh;
  margin: 0;
  padding: 0;
  width: 100%;
  overflow-y: hidden;
}

.nav-footer {
  background-color: #001529;
  /* background: linear-gradient(to top, rgb(3, 158, 185,0.7), rgb(123, 3, 153,0.7)); */
  font-size: 12px;
  color: #fff;
  font-weight: bold;
  height: 2vh;
  display: flex;
  justify-content: center;
  text-align: center;
  align-items: center;
  margin: 0;
  padding: 0;
  border: 1px solid #001529;
}
.exit-fullscreen-button {
  position: fixed;
  bottom: 3vh;
  left: 0px;
  height: 50px;
  /* border-radius: 25px; */
  background-color: #1890ff;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0px 4px 12px rgba(0, 0, 0, 0.15);
  width: 100px;
  border-radius: 0 25px 25px 0;
}
</style>
