<template>
  <div class="layout-container">
    <!-- 左侧导航栏 -->
    <div class="sidebar">
      <div class="logo">
        <h2>快递系统</h2>
      </div>
      <el-menu
        default-active="/home"
        class="sidebar-menu"
        router
        background-color="#304156"
        text-color="#fff"
      >
        <el-menu-item index="/home">
          <el-icon><HomeFilled /></el-icon>
          <span>首页</span>
        </el-menu-item>
        <el-menu-item index="/delivery">
          <el-icon><Van /></el-icon>
          <span>寄件</span>
        </el-menu-item>
        <el-menu-item index="/check">
          <el-icon><Search /></el-icon>
          <span>查件</span>
        </el-menu-item>
        <el-menu-item index="/service">
          <el-icon><Service /></el-icon>
          <span>服务查询</span>
        </el-menu-item>
        
        <el-sub-menu index="/problem">
          <template #title>
            <el-icon><Warning /></el-icon>
            <span>问题反馈</span>
          </template>
          <el-menu-item index="/feedback">我有意见</el-menu-item>
          <el-menu-item index="/complaint">快递投诉</el-menu-item>
        </el-sub-menu>
        
        <el-sub-menu index="/profile">
          <template #title>
            <el-icon><User /></el-icon>
            <span>个人中心</span>
          </template>
          <el-menu-item index="/profile">我的资料/地址簿</el-menu-item>
        </el-sub-menu>
      </el-menu>
    </div>

    <!-- 顶部标题栏 -->
    <div class="header">
      <div class="header-left">
        <el-icon class="toggle-icon" @click="toggleSidebar">
          <Fold v-if="!isCollapse" />
          <Expand v-else />
        </el-icon>
        <span class="current-route">{{ currentRoute }}</span>
      </div>
      <div class="header-right">
        <el-dropdown>
          <span class="user-info">
            <el-avatar :size="30" :src="avatarUrl" />
            <span class="username">{{ username }}</span>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item @click="goToProfile">个人中心</el-dropdown-item>
              <el-dropdown-item @click="logout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
  </div>
</template>

<style scoped>
.layout-container {
  height: 100vh;
  width: 100%;
}

/* 侧边栏样式 */
.sidebar {
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  width: 220px;
  background-color: #304156;
  transition: width 0.3s;
  z-index: 1000;
  overflow-y: auto;
  overflow-x: hidden;
}

.sidebar.collapsed {
  width: 64px;
}

/* 顶部标题栏样式 */
.header {
  position: fixed;
  top: 0;
  right: 0;
  width: calc(100% - 220px); /* 减去侧边栏宽度 */
  height: 60px;
  background-color: #fff;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  z-index: 999;
  transition: width 0.3s;
}

.sidebar.collapsed + .header {
  width: calc(100% - 64px); /* 侧边栏折叠时的宽度 */
}

.header-left {
  display: flex;
  align-items: center;
  gap: 15px;
}

.toggle-icon {
  font-size: 20px;
  cursor: pointer;
  color: #606266;
}

.current-route {
  font-size: 16px;
  color: #303133;
  font-weight: 500;
}

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

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

.username {
  font-size: 14px;
  color: #606266;
}

/* Logo样式 */
.logo {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
}

.logo h2 {
  margin: 0;
  font-size: 18px;
  white-space: nowrap;
}

/* 菜单样式 */
.sidebar-menu {
  border-right: none;
}

/* 响应式布局 */
@media screen and (max-width: 768px) {
  .sidebar {
    width: 64px;
  }
  
  .header {
    width: calc(100% - 64px);
  }
  
  .username {
    display: none;
  }
  
  .current-route {
    font-size: 14px;
  }
}

/* Element Plus 样式覆盖 */
:deep(.el-menu-item.is-active) {
  background-color: #263445 !important;
}

:deep(.el-menu-item:hover),
:deep(.el-sub-menu__title:hover) {
  background-color: #263445 !important;
}

:deep(.el-menu) {
  border-right: none;
}
</style>

<script setup>
import { ref, computed } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { Fold, Expand } from '@element-plus/icons-vue';

const router = useRouter();
const route = useRoute();
const isCollapse = ref(false);
const avatarUrl = ref('path/to/avatar.jpg');
const username = ref('用户名');

// 计算当前路由名称
const currentRoute = computed(() => {
  return route.meta.title || route.name || '首页';
});

// 切换侧边栏
const toggleSidebar = () => {
  isCollapse.value = !isCollapse.value;
  const sidebar = document.querySelector('.sidebar');
  if (sidebar) {
    sidebar.classList.toggle('collapsed');
  }
};

// 跳转到个人中心
const goToProfile = () => {
  router.push('/profile');
};

// 退出登录
const logout = () => {
  router.push({ name: 'login' });
};
</script>
