<script setup lang="ts">
import { computed } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessageBox } from 'element-plus'
import { useAuthStore } from '@/stores/auth'

const emit = defineEmits<{
  search: []
}>()

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

const user = computed(() => authStore.user)

async function handleLogout() {
  try {
    await ElMessageBox.confirm('确定要退出登录吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    await authStore.logout()
    router.push('/login')
  } catch {
    // 取消
  }
}

function handleCommand(command: string) {
  switch (command) {
    case 'settings':
      router.push('/settings')
      break
    case 'devices':
      router.push('/devices')
      break
    case 'logout':
      handleLogout()
      break
  }
}
</script>

<template>
  <div class="app-layout">
    <!-- 顶部导航 -->
    <header class="app-header">
      <div class="header-left">
        <router-link to="/" class="logo">
          📚 书签管理
        </router-link>
      </div>

      <div class="header-center">
        <div class="search-trigger" @click="emit('search')">
          <el-icon><Search /></el-icon>
          <span>搜索书签...</span>
          <kbd>Ctrl+K</kbd>
        </div>
      </div>

      <div class="header-right">
        <el-dropdown trigger="click" @command="handleCommand">
          <div class="user-menu">
            <el-avatar :size="32" :src="user?.avatar">
              {{ user?.userName?.[0] || 'U' }}
            </el-avatar>
            <span class="user-name">{{ user?.userName || '用户' }}</span>
            <el-icon><ArrowDown /></el-icon>
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="settings">
                <el-icon><Setting /></el-icon>
                设置
              </el-dropdown-item>
              <el-dropdown-item command="devices">
                <el-icon><Monitor /></el-icon>
                设备管理
              </el-dropdown-item>
              <el-dropdown-item divided command="logout">
                <el-icon><SwitchButton /></el-icon>
                退出登录
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </header>

    <!-- 主内容 -->
    <main class="app-main">
      <slot />
    </main>
  </div>
</template>

<style lang="scss" scoped>
.app-layout {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background: #f5f6fa;
}

.app-header {
  height: 60px;
  background: #fff;
  border-bottom: 1px solid #e8e8e8;
  display: flex;
  align-items: center;
  padding: 0 24px;
  position: sticky;
  top: 0;
  z-index: 100;

  .header-left {
    .logo {
      font-size: 20px;
      font-weight: 700;
      color: #333;
      text-decoration: none;
    }
  }

  .header-center {
    flex: 1;
    display: flex;
    justify-content: center;
    padding: 0 40px;

    .search-trigger {
      display: flex;
      align-items: center;
      gap: 8px;
      padding: 8px 16px;
      background: #f5f5f5;
      border-radius: 8px;
      cursor: pointer;
      color: #999;
      min-width: 300px;
      transition: all 0.2s;

      &:hover {
        background: #ebebeb;
      }

      kbd {
        margin-left: auto;
        background: #e0e0e0;
        padding: 2px 6px;
        border-radius: 4px;
        font-size: 12px;
        font-family: inherit;
      }
    }
  }

  .header-right {
    .user-menu {
      display: flex;
      align-items: center;
      gap: 8px;
      cursor: pointer;
      padding: 4px 8px;
      border-radius: 8px;
      transition: background 0.2s;

      &:hover {
        background: #f5f5f5;
      }

      .user-name {
        font-size: 14px;
        color: #333;
      }
    }
  }
}

.app-main {
  flex: 1;
  padding: 24px;
  max-width: 1400px;
  margin: 0 auto;
  width: 100%;
}
</style>

