<template>
    <div class="app-header">
        <div class="app-header__left">
            <slot name="left"></slot>
            <span>多元世纪</span>
        </div>
        <div class="app-header__right">
            <slot name="right"></slot>
            <div class="iconbar">
                <el-badge type="danger" :is-dot="noticeStore.unreadList.length > 0">
                    <app-icon icon="g-icon--notice" @click="openComponent('notice')" />
                </el-badge>
                <span class="g-icon--minimize" @click="exitFullSreen" v-if="fullScreen"></span>
                <span class="g-icon--maximize" @click="setFullSreen" v-else></span>
            </div>
            <el-dropdown class="user-dropdown" trigger="click">
                <span class="user-dropdown__link">
                    <img class="g-image--avatar" :title="userStore.accountName" :src="userAvatar" />
                    <span v-if="!globalStore.isMobile">{{ userStore.accountName }}</span>
                    <app-icon class="el-icon--right" icon="ArrowDown" />
                </span>
                <template #dropdown>
                    <el-dropdown-menu>
                        <el-dropdown-item :icon="Unlock"
                            @click="openComponent('modify')">修改密码</el-dropdown-item>
                        <el-dropdown-item :icon="SwitchButton"
                            @click="eventBus.$emit('LogoutNotify')">退出登录</el-dropdown-item>
                    </el-dropdown-menu>
                </template>
            </el-dropdown>
        </div>
        <component :is="componentMap.get(componentId)" @closed="closeComponent" v-if="componentId" />
    </div>
</template>

<script lang="ts" setup>
import { ref, onMounted, computed, defineAsyncComponent } from 'vue'
import { SwitchButton, Unlock } from '@element-plus/icons-vue'
import { getFileUrl } from '@/filters'
import { useComponent } from '@/hooks/component'
import { useUserStore, useGlobalStore, useNoticeStore } from '@/stores'
import eventBus from '@/services/bus'
import AppIcon from '@pc/components/base/icon/index.vue'

const componentMap = new Map<string, unknown>([
    ['notice', defineAsyncComponent(() => import('./components/notice/index.vue'))],
    ['modify', defineAsyncComponent(() => import('./components/modify/index.vue'))],
])

const { componentId, openComponent, closeComponent } = useComponent()
const globalStore = useGlobalStore()
const userStore = useUserStore()
const noticeStore = useNoticeStore()
const fullScreen = ref(false)

// 用户头像
const userAvatar = computed(() => {
    const file = userStore.userInfo?.headurl
    return file ? getFileUrl(file) : ''
})

// 全屏
const setFullSreen = () => {
    document.documentElement.requestFullscreen();
}

// 退出全屏
const exitFullSreen = () => {
    document.exitFullscreen();
}

onMounted(() => {
    // 监听全屏变化
    window.addEventListener('fullscreenchange', () => {
        if (document.fullscreenElement) {
            fullScreen.value = true;
        } else {
            fullScreen.value = false;
        }
    })
})
</script>

<style lang="less" scoped>
@import './index.less';
</style>