<script setup lang="ts">
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { useAppStore } from '@/stores/app'
import AppVersion from '@/components/common/AppVersion.vue'

withDefaults(defineProps<{
  showHome?: boolean
  showHamburger?: boolean
}>(), {
  showHome: false,
  showHamburger: false,
})

const router = useRouter()
const userStore = useUserStore()
const appStore = useAppStore()

function handleLogout() {
  userStore.logout().then(() => {
    router.push('/login')
  })
}

function handleHome() {
  router.push('/')
}
</script>

<template>
  <header class="topbar">
    <div class="topbar-left">
      <button
        v-if="showHamburger"
        class="hamburger"
        @click="appStore.toggleMobileSidebar()"
      >
        ☰
      </button>
      <a
        v-if="showHome"
        href="javascript:void(0)"
        class="topbar-home"
        @click="handleHome"
      >
        🏠 首页
      </a>
    </div>
    <div class="topbar-right">
      <AppVersion />
      <div class="topbar-user">
        <span>{{ userStore.displayName }}</span>
        <button class="btn-logout" @click="handleLogout">退出</button>
      </div>
    </div>
  </header>
</template>

<style scoped lang="scss">
.topbar {
  position: fixed;
  left: 200px;
  top: 0;
  right: 0;
  height: 56px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  z-index: 99;

  @media screen and (max-width: 768px) {
    left: 0;
    padding: 0 12px;
  }

  @media screen and (max-width: 480px) {
    padding: 0 10px;
    height: 52px;
  }
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 16px;
}

.hamburger {
  display: none;
  background: rgba(255, 255, 255, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.35);
  color: #fff;
  font-size: 18px;
  padding: 8px 12px;
  border-radius: 6px;
  cursor: pointer;

  @media screen and (max-width: 768px) {
    display: block;
  }

  @media screen and (max-width: 480px) {
    padding: 6px 10px;
    font-size: 16px;
  }
}

.topbar-home {
  background: rgba(255, 255, 255, 0.2);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.35);
  padding: 6px 14px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;

  &:hover {
    background: rgba(255, 255, 255, 0.3);
  }

  @media screen and (max-width: 768px) {
    display: none;
  }
}

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

.topbar-user {
  color: #fff;
  display: flex;
  align-items: center;
  gap: 14px;
  font-size: 14px;

  @media screen and (max-width: 768px) {
    font-size: 13px;
    gap: 8px;
  }
}

.btn-logout {
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.3);
  padding: 8px 16px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;

  &:hover {
    background: rgba(255, 255, 255, 0.25);
  }

  @media screen and (max-width: 768px) {
    padding: 6px 10px;
    font-size: 12px;
  }

  @media screen and (max-width: 480px) {
    padding: 5px 8px;
    font-size: 11px;
  }
}
</style>