<template>
  <el-container class="default-layout">
    <el-aside
      :width="collapsed ? '64px' : '200px'"
      class="aside"
    >
      <div class="logo">
        <span v-if="!collapsed">Novalon</span>
        <span v-else>N</span>
      </div>
      <el-menu
        :default-active="activeMenu"
        class="menu"
        :collapse="collapsed"
        background-color="#f5f7fa"
        text-color="#606266"
        active-text-color="#409eff"
        router
      >
        <el-menu-item index="/dashboard">
          <el-icon><Odometer /></el-icon>
          <span>仪表盘</span>
        </el-menu-item>
        <el-sub-menu index="system">
          <template #title>
            <el-icon><Setting /></el-icon>
            <span>系统管理</span>
          </template>
          <el-menu-item index="/users">
            用户管理
          </el-menu-item>
          <el-menu-item index="/roles">
            角色管理
          </el-menu-item>
          <el-menu-item index="/menus">
            菜单管理
          </el-menu-item>
        </el-sub-menu>
        <el-sub-menu index="config">
          <template #title>
            <el-icon><Tools /></el-icon>
            <span>系统配置</span>
          </template>
          <el-menu-item index="/dict">
            字典管理
          </el-menu-item>
          <el-menu-item index="/sys/config">
            参数配置
          </el-menu-item>
        </el-sub-menu>
        <el-sub-menu index="audit">
          <template #title>
            <el-icon><Document /></el-icon>
            <span>审计中心</span>
          </template>
          <el-menu-item index="/loginlog">
            登录日志
          </el-menu-item>
          <el-menu-item index="/oplog">
            操作日志
          </el-menu-item>
          <el-menu-item index="/exceptionlog">
            异常日志
          </el-menu-item>
        </el-sub-menu>
        <el-sub-menu index="notify">
          <template #title>
            <el-icon><Bell /></el-icon>
            <span>通知中心</span>
          </template>
          <el-menu-item index="/notice">
            通知公告
          </el-menu-item>
        </el-sub-menu>
        <el-sub-menu index="file">
          <template #title>
            <el-icon><Folder /></el-icon>
            <span>文件管理</span>
          </template>
          <el-menu-item index="/files">
            文件列表
          </el-menu-item>
        </el-sub-menu>
      </el-menu>
    </el-aside>
    <el-container>
      <el-header class="header">
        <el-icon
          class="trigger"
          @click="collapsed = !collapsed"
        >
          <Fold v-if="!collapsed" />
          <Expand v-else />
        </el-icon>
        <div class="header-right">
          <el-dropdown @command="handleCommand">
            <el-avatar :size="32">
              {{ username }}
            </el-avatar>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">
                  个人中心
                </el-dropdown-item>
                <el-dropdown-item
                  command="logout"
                  divided
                >
                  退出登录
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      <el-main class="content">
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import {
  Odometer, Setting, Tools, Document, Bell, Folder,
  Fold, Expand
} from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()
const collapsed = ref(false)
const username = ref(localStorage.getItem('username') || 'Admin')

const activeMenu = computed(() => route.path)

const handleCommand = (command: string) => {
  if (command === 'profile') {
    router.push('/profile')
  } else if (command === 'logout') {
    localStorage.clear()
    router.push('/login')
  }
}

onMounted(() => {
  const token = localStorage.getItem('token')
  if (!token) {
    router.push('/login')
  }
})
</script>

<style scoped lang="css">
.default-layout {
  min-height: 100vh;
}

.aside {
  background-color: #f5f7fa;
  transition: width 0.3s;
  overflow: hidden;
}

.logo {
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #303133;
  font-size: 20px;
  font-weight: bold;
}

.menu {
  border-right: none;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 16px;
  background: #fff;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);

  .trigger {
    font-size: 18px;
    cursor: pointer;
    transition: color 0.3s;
    &:hover { color: #409eff; }
  }

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

.content {
  margin: 16px;
  padding: 16px;
  background: #fff;
  min-height: calc(100vh - 96px);
}
</style>
