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

withDefaults(defineProps<{
  showModule?: boolean
  moduleLabel?: string
  homePath?: string
  logsPath?: string
}>(), {
  showModule: false,
  moduleLabel: '',
  homePath: '',
  logsPath: '',
})

const route = useRoute()
const router = useRouter()
const userStore = useUserStore()

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

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

<template>
  <header class="navbar">
    <div class="navbar-left">
      <a
        href="javascript:void(0)"
        class="navbar-home"
        @click="handleHome"
      >
        运行维护平台
      </a>
      <span v-if="showModule" class="navbar-separator">/</span>
      <a
        v-if="showModule && homePath"
        href="javascript:void(0)"
        class="navbar-module navbar-module-link"
        @click="router.push(homePath)"
      >
        {{ moduleLabel }}
      </a>
      <span v-else-if="showModule" class="navbar-module">{{ moduleLabel }}</span>
      <a
        v-if="logsPath && route.path !== logsPath"
        :href="logsPath"
        class="navbar-logs"
        @click.prevent="router.push(logsPath)"
      >
        📋 操作日志
      </a>
    </div>
    <div class="navbar-right">
      <AppVersion />
      <span class="navbar-user">{{ userStore.displayName }}</span>
      <button class="btn-logout" @click="handleLogout">退出</button>
    </div>
  </header>
</template>

<style scoped lang="scss">
.navbar {
  position: fixed;
  left: 0;
  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) {
    padding: 0 12px;
  }
}

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

.navbar-home {
  color: #fff;
  text-decoration: none;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;

  &:hover {
    opacity: 0.85;
  }
}

.navbar-separator {
  color: rgba(255, 255, 255, 0.5);
  font-size: 16px;
}

.navbar-module {
  color: rgba(255, 255, 255, 0.85);
  font-size: 14px;
}

.navbar-module-link {
  cursor: pointer;
  text-decoration: none;

  &:hover {
    color: #fff;
  }
}

.navbar-logs {
  color: rgba(255, 255, 255, 0.85);
  font-size: 13px;
  text-decoration: none;
  background: rgba(255, 255, 255, 0.12);
  padding: 3px 10px;
  border-radius: 4px;
  cursor: pointer;

  &:hover {
    background: rgba(255, 255, 255, 0.22);
    color: #fff;
  }
}

.navbar-right {
  display: flex;
  align-items: center;
  gap: 14px;
}

.navbar-user {
  color: #fff;
  font-size: 14px;

  @media screen and (max-width: 768px) {
    font-size: 13px;
    max-width: 100px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}

.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;
  }
}
</style>