<template>
  <div class="navbar">
    <!-- 左侧 -->
    <div class="navbar-left">
      <!-- 折叠按钮 -->
      <div class="hamburger-container" @click="toggleSideBar">
        <el-icon class="hamburger" :class="{ 'is-active': appStore.sidebarOpened }">
          <Fold v-if="appStore.sidebarOpened" />
          <Expand v-else />
        </el-icon>
      </div>
      
      <!-- 面包屑 -->
      <breadcrumb class="breadcrumb-container" />
    </div>
    
    <!-- 右侧 -->
    <div class="navbar-right">
      <!-- 主题切换 -->
      <div class="right-menu-item" @click="toggleTheme">
        <el-icon>
          <Sunny v-if="appStore.isDark" />
          <Moon v-else />
        </el-icon>
      </div>
      
      <!-- 全屏 -->
      <div class="right-menu-item" @click="toggleFullscreen">
        <el-icon>
          <FullScreen />
        </el-icon>
      </div>
      
      <!-- 用户菜单 -->
      <el-dropdown class="avatar-container" trigger="click">
        <div class="avatar-wrapper">
          <img
            v-if="authStore.user?.avatar"
            :src="authStore.user.avatar"
            class="user-avatar"
            alt="avatar"
          >
          <el-avatar v-else class="user-avatar" :size="32">
            {{ authStore.userName.charAt(0) }}
          </el-avatar>
          <span class="user-name">{{ authStore.userName }}</span>
          <el-icon class="el-icon-caret-bottom">
            <CaretBottom />
          </el-icon>
        </div>
        
        <template #dropdown>
          <el-dropdown-menu>
            <router-link to="/profile">
              <el-dropdown-item>
                <el-icon><User /></el-icon>
                个人中心
              </el-dropdown-item>
            </router-link>
            
            <el-dropdown-item divided @click="logout">
              <el-icon><SwitchButton /></el-icon>
              退出登录
            </el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<script setup>
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  Fold,
  Expand,
  Sunny,
  Moon,
  FullScreen,
  CaretBottom,
  User,
  SwitchButton
} from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import { useAppStore } from '@/stores/app'
import { useAuthStore } from '@/stores/auth'
import Breadcrumb from './Breadcrumb.vue'

const router = useRouter()
const appStore = useAppStore()
const authStore = useAuthStore()

// 切换侧边栏
const toggleSideBar = () => {
  appStore.toggleSidebar()
}

// 切换主题
const toggleTheme = () => {
  appStore.toggleTheme()
  ElMessage.success(`已切换到${appStore.isDark ? '暗色' : '亮色'}主题`)
}

// 切换全屏
const toggleFullscreen = () => {
  if (!document.fullscreenElement) {
    document.documentElement.requestFullscreen()
  } else {
    if (document.exitFullscreen) {
      document.exitFullscreen()
    }
  }
}

// 退出登录
const logout = async () => {
  try {
    await ElMessageBox.confirm('确定要退出登录吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    
    await authStore.logout()
    ElMessage.success('退出登录成功')
    await router.push('/login')
  } catch (error) {
    if (error !== 'cancel') {
      console.error('退出登录失败:', error)
    }
  }
}
</script>

<style lang="scss" scoped>
@import '@/styles/variables.scss';

.navbar {
  height: $header-height;
  background: $header-bg;
  border-bottom: 1px solid $header-border-color;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  position: relative;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
}

.navbar-left {
  display: flex;
  align-items: center;
}

.hamburger-container {
  line-height: $header-height;
  height: $header-height;
  float: left;
  cursor: pointer;
  transition: background 0.3s;
  padding: 0 15px;
  margin-left: -15px;
  
  &:hover {
    background: rgba(0, 0, 0, 0.025);
  }
  
  .hamburger {
    display: inline-block;
    vertical-align: middle;
    width: 20px;
    height: 20px;
    color: #5a5e66;
  }
}

.breadcrumb-container {
  margin-left: 16px;
}

.navbar-right {
  display: flex;
  align-items: center;
  
  .right-menu-item {
    display: inline-block;
    padding: 0 12px;
    height: $header-height;
    line-height: $header-height;
    color: #5a5e66;
    vertical-align: text-bottom;
    cursor: pointer;
    transition: background 0.3s;
    
    &:hover {
      background: rgba(0, 0, 0, 0.025);
    }
    
    .el-icon {
      font-size: 18px;
    }
  }
  
  .avatar-container {
    margin-left: 10px;
    
    .avatar-wrapper {
      display: flex;
      align-items: center;
      cursor: pointer;
      padding: 5px 0;
      
      .user-avatar {
        cursor: pointer;
        border-radius: 50%;
      }
      
      .user-name {
        margin-left: 10px;
        font-size: 14px;
        color: $text-color-primary;
      }
      
      .el-icon-caret-bottom {
        cursor: pointer;
        margin-left: 5px;
        font-size: 12px;
        color: #5a5e66;
      }
    }
  }
}

// 暗色模式
.dark {
  .navbar {
    background: $header-bg-dark;
    border-color: $border-color-dark;
    
    .hamburger-container {
      &:hover {
        background: rgba(255, 255, 255, 0.1);
      }
      
      .hamburger {
        color: #cfd3dc;
      }
    }
    
    .right-menu-item {
      color: #cfd3dc;
      
      &:hover {
        background: rgba(255, 255, 255, 0.1);
      }
    }
    
    .avatar-container {
      .avatar-wrapper {
        .user-name {
          color: $text-color-primary-dark;
        }
        
        .el-icon-caret-bottom {
          color: #cfd3dc;
        }
      }
    }
  }
}

// 移动端适配
@media screen and (max-width: $sm) {
  .navbar {
    padding: 0 15px;
  }
  
  .breadcrumb-container {
    display: none;
  }
  
  .avatar-container {
    .avatar-wrapper {
      .user-name {
        display: none;
      }
    }
  }
}
</style>
