<script setup lang="ts">
import { ref, computed, onMounted, onUnmounted } from 'vue';
import { useAdminLocale, type AdminLocale } from '../composables/useAdminLocale';
import AdminLocaleFlag from './AdminLocaleFlag.vue';

withDefaults(
  defineProps<{
    /** gold：与玩家端登录一致；admin：管理后台顶栏绿色主题 */
    variant?: 'gold' | 'admin';
  }>(),
  { variant: 'admin' },
);

const { locale, locales, setLocale, t } = useAdminLocale();

const open = ref(false);
const root = ref<HTMLElement | null>(null);

const currentLabel = computed(
  () => locales.find((l) => l.code === locale.value)?.label ?? locale.value,
);

function pick(code: AdminLocale) {
  open.value = false;
  setLocale(code);
}

function toggle() {
  open.value = !open.value;
}

function onDocClick(e: MouseEvent) {
  if (!root.value?.contains(e.target as Node)) open.value = false;
}

onMounted(() => document.addEventListener('click', onDocClick));
onUnmounted(() => document.removeEventListener('click', onDocClick));
</script>

<template>
  <div ref="root" class="admin-locale" :class="[variant, { open }]">
    <button
      type="button"
      class="admin-locale-trigger"
      :aria-expanded="open"
      aria-haspopup="listbox"
      :title="t('lang')"
      @click.stop="toggle"
    >
      <AdminLocaleFlag :locale="locale" :size="20" />
      <span class="admin-locale-label">{{ currentLabel }}</span>
      <span class="locale-chevron" aria-hidden="true">▾</span>
    </button>
    <ul v-show="open" class="admin-locale-menu" role="listbox" :aria-label="t('lang')">
      <li
        v-for="l in locales"
        :key="l.code"
        role="option"
        :aria-selected="locale === l.code"
        class="admin-locale-option"
        :class="{ active: locale === l.code }"
        @click="pick(l.code)"
      >
        <AdminLocaleFlag :locale="l.code" :size="18" />
        <span>{{ l.label }}</span>
      </li>
    </ul>
  </div>
</template>

<style scoped>
.admin-locale {
  position: relative;
  display: inline-flex;
}

.admin-locale-trigger {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-width: 120px;
  padding: 5px 10px 5px 8px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  line-height: 1.2;
}

.admin-locale-label {
  flex: 1;
  text-align: left;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.locale-chevron {
  font-size: 10px;
  opacity: 0.7;
  transition: transform 0.15s ease;
}

.admin-locale.open .locale-chevron {
  transform: rotate(180deg);
}

.admin-locale-menu {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  z-index: 50;
  min-width: 100%;
  margin: 0;
  padding: 4px;
  list-style: none;
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.55);
}

.admin-locale-option {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

/* 管理顶栏 */
.admin-locale.admin .admin-locale-trigger {
  background: transparent;
  color: #a3a3a3;
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.admin-locale.admin .admin-locale-menu {
  background: #141414;
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.admin-locale.admin .admin-locale-option {
  color: #a3a3a3;
}

.admin-locale.admin .admin-locale-option:hover,
.admin-locale.admin .admin-locale-option.active {
  background: rgba(255, 255, 255, 0.06);
  color: #f5f5f5;
}

/* 登录页：与玩家端金色主题一致 */
.admin-locale.gold .admin-locale-trigger {
  background: #0d0d0d;
  color: #f0d875;
  border: 1px solid rgba(212, 175, 55, 0.28);
}

.admin-locale.gold .admin-locale-menu {
  background: #141414;
  border: 1px solid rgba(212, 175, 55, 0.28);
}

.admin-locale.gold .admin-locale-option {
  color: #8e8e93;
}

.admin-locale.gold .admin-locale-option:hover,
.admin-locale.gold .admin-locale-option.active {
  background: rgba(212, 175, 55, 0.1);
  color: #f0d875;
}

/* Light admin refresh */
.admin-locale-trigger {
  min-height: 32px;
  border-radius: 7px;
}

.admin-locale-menu {
  border-radius: 8px;
  box-shadow: 0 18px 44px rgba(56, 49, 37, 0.12);
}

.admin-locale.admin .admin-locale-trigger,
.admin-locale.gold .admin-locale-trigger {
  background: #ffffff;
  color: var(--text);
  border: 1px solid var(--border);
}

.admin-locale.admin .admin-locale-trigger:hover,
.admin-locale.gold .admin-locale-trigger:hover {
  background: var(--accent-hover);
  border-color: #d5cfc3;
}

.admin-locale.admin .admin-locale-menu,
.admin-locale.gold .admin-locale-menu {
  background: #ffffff;
  border: 1px solid var(--border);
}

.admin-locale.admin .admin-locale-option,
.admin-locale.gold .admin-locale-option {
  color: var(--text-muted);
}

.admin-locale.admin .admin-locale-option:hover,
.admin-locale.admin .admin-locale-option.active,
.admin-locale.gold .admin-locale-option:hover,
.admin-locale.gold .admin-locale-option.active {
  background: var(--accent-hover);
  color: var(--text);
}
</style>
