<template>
    <header>
        <div class="p-2">
            <div class="panel-shell">
                <nav class="fluent-nav px-4 lg:px-6 py-2.5">
                    <div class="flex flex-wrap justify-between items-center">
                        <a href="#" class="flex items-center">
                            <svg width="40" height="40" viewBox="0 0 24 24" fill="none"
                                xmlns="http://www.w3.org/2000/svg">
                                <path fill-rule="evenodd" clip-rule="evenodd"
                                    d="M5 6C5 4.89543 5.89543 4 7 4H12C13.6569 4 15 5.34315 15 7V17C15 18.6569 13.6569 20 12 20H7C5.89543 20 5 19.1046 5 18V6ZM7 6V10H12C12.5523 10 13 9.55228 13 9C13 8.44772 12.5523 8 12 8H8.5C7.67157 8 7 7.32843 7 6.5V6ZM7 18L7 14H10C10.5523 14 11 13.5523 11 13C11 12.4477 10.5523 12 10 12H7V17.5C7 17.7761 7.22386 18 7.5 18H12C12.5523 18 13 17.5523 13 17V17C13 16.4477 12.5523 16 12 16H8.5C7.67157 16 7 16.6716 7 17.5V18Z"
                                    fill="#0056D2" />
                                <circle cx="17.5" cy="6.5" r="2.5" fill="#FFAB00" />
                            </svg>
                            <span class="self-center text-xl font-semibold whitespace-nowrap">英语教育</span>
                        </a>
                        <div class="flex items-center lg:order-2">
                            <template v-if="userName">
                                <div class="relative" ref="menuRef">
                                    <button @click="menuOpen = !menuOpen"
                                        class="fluent-btn font-medium rounded-lg text-sm px-4 lg:px-5 py-2 lg:py-2.5 mr-2 flex items-center">
                                        <span class="mr-2">{{ userName }}</span>
                                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                                d="M19 9l-7 7-7-7" />
                                        </svg>
                                    </button>
                                    <div v-if="menuOpen" class="fluent-card absolute right-0 mt-2 z-50">
                                        <router-link to="/admid" @click="menuOpen = false"
                                            class="block px-4 py-2 fluent-link">
                                            后台
                                        </router-link>
                                        <button @click="handleLogout"
                                            class="w-full text-left block px-4 py-2 fluent-link">
                                            登出
                                        </button>
                                    </div>
                                </div>
                            </template>
                            <button data-collapse-toggle="mobile-menu-2" type="button" @click="openMobileSidebar()"
                                class="inline-flex items-center p-2 ml-1 text-sm rounded-lg lg:hidden fluent-btn"
                                aria-controls="mobile-menu-2" aria-expanded="false">
                                <span class="sr-only">Open main menu</span>
                                <svg class="w-6 h-6" fill="currentColor" viewBox="0 0 20 20"
                                    xmlns="http://www.w3.org/2000/svg">
                                    <path fill-rule="evenodd"
                                        d="M3 5a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zM3 10a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zM3 15a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1z"
                                        clip-rule="evenodd"></path>
                                </svg>
                                <svg class="hidden w-6 h-6" fill="currentColor" viewBox="0 0 20 20"
                                    xmlns="http://www.w3.org/2000/svg">
                                    <path fill-rule="evenodd"
                                        d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z"
                                        clip-rule="evenodd"></path>
                                </svg>
                            </button>
                        </div>
                        <div class="hidden justify-between items-center w-full lg:flex lg:w-auto lg:order-1"
                            id="mobile-menu-2">

                        </div>
                    </div>
                </nav>
            </div>
        </div>
        <LoginDialog v-model="showLogin" @success="refreshUser" />
    </header>
</template>

<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue'
import LoginDialog from '@/layouts/components/LoginDialog.vue'
import { getUserInfo, logout } from '@/api/user'
import { removeToken } from '@/composables/auth'
import { useRouter } from 'vue-router'
import { showMessage } from '@/composables/util.js'
import { openMobileSidebar } from '@/composables/ui.js'
const showLogin = ref(false)
const userName = ref('')
const menuOpen = ref(false)
const menuRef = ref(null)
const router = useRouter()
async function refreshUser() {
    try {
        const r = await getUserInfo()
        const d = r?.data
        console.log("header" + d.success)
        if (d?.success) {
            userName.value = d?.data?.name || ''
        } else {
            handleLogout()
        }
    } catch {
        handleLogout()
    }
}
async function handleLogout() {
    try {
        await logout()
    } finally {
        removeToken()
        userName.value = ''
        menuOpen.value = false
        showMessage('已退出登录', 'success')
        router.push('/login')
    }
}
function onDocClick(e) {
    if (!menuOpen.value) return
    const el = menuRef.value
    if (el && !el.contains(e.target)) {
        menuOpen.value = false
    }
}
onMounted(() => {
    refreshUser()
    document.addEventListener('click', onDocClick)
})
onBeforeUnmount(() => {
    document.removeEventListener('click', onDocClick)
})
</script>
<style scoped>
.fluent-nav {
    background: transparent;
    border-bottom: 0;
    backdrop-filter: none;
    min-height: 56px;
}

:global(.dark) .fluent-nav {
    background: transparent;
    border-bottom: 0;
}

.fluent-card {
    background: rgba(255, 255, 255, 0.6);
    border: 1px solid rgba(255, 255, 255, 0.4);
    border-radius: 12px;
    box-shadow: 0 8px 24px rgba(15, 23, 42, 0.12);
    backdrop-filter: blur(16px);
    transition: box-shadow 200ms ease, transform 200ms ease;
}

:global(.dark) .fluent-card {
    background: rgba(55, 65, 81, 0.4);
    border-color: rgba(148, 163, 184, 0.25);
}

.fluent-card:hover {
    box-shadow: 0 12px 28px rgba(15, 23, 42, 0.16);
    transform: translateY(-1px);
}

.fluent-btn {
    color: #0f172a;
    background: rgba(255, 255, 255, 0.6);
    border: 1px solid rgba(255, 255, 255, 0.4);
    border-radius: 10px;
    box-shadow: 0 2px 8px rgba(15, 23, 42, 0.08);
    transition: background 200ms ease, box-shadow 200ms ease, transform 200ms ease;
}

.fluent-btn:hover {
    background: rgba(255, 255, 255, 0.7);
    box-shadow: 0 4px 12px rgba(15, 23, 42, 0.12);
}

:global(.dark) .fluent-btn {
    color: #e5e7eb;
    background: rgba(55, 65, 81, 0.4);
    border-color: rgba(148, 163, 184, 0.25);
}

.fluent-link {
    color: #2563eb;
    border-radius: 10px;
    transition: color 200ms ease, background 200ms ease, box-shadow 200ms ease;
}

.fluent-link:hover {
    color: #1d4ed8;
    background: rgba(255, 255, 255, 0.35);
    box-shadow: inset 0 0 0 1px rgba(37, 99, 235, 0.25);
}

:global(.dark) .fluent-link:hover {
    background: rgba(55, 65, 81, 0.35);
    box-shadow: inset 0 0 0 1px rgba(37, 99, 235, 0.25);
}

.fluent-card {
    overflow: visible;
}

:global(.el-header) {
    overflow: visible;
    padding: 0;
    position: relative;
    z-index: 1000;
}
</style>
