<template>
    <div class="app-header">
        <div class="app-header__left">
            <slot name="left"></slot>
            <el-breadcrumb separator-icon="ArrowRight" v-show="!globalStore.isMobile">
                <template v-for="(item, index) in $route.matched" :key="index">
                    <el-breadcrumb-item :to="{ path: item.path }">
                        <!--<i :class="item.meta.icon" v-if="item.meta.icon"></i>-->
                        <span>{{ item.meta.title ?? 'item.name' }}</span>
                    </el-breadcrumb-item>
                </template>
            </el-breadcrumb>
        </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>
                <!-- <app-icon icon="g-icon--message" @click="routerTo('bargain_buy')" /> -->
                <app-icon icon="g-icon--counter" @click="openComponent('calculator')" />
                <app-icon icon="g-icon--certificate" @click="openComponent('certificate')" />
                <app-icon icon="g-icon--minimize" @click="exitFullSreen" v-if="fullScreen" />
                <app-icon icon="g-icon--maximize" @click="setFullSreen" v-else />
            </div>
            <el-dropdown class="user-dropdown" trigger="click">
                <span class="user-dropdown__link">
                    <img class="g-image--avatar" :title="userStore.accountName" />
                    <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="Key">修改密码</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, defineAsyncComponent } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore, useGlobalStore, useNotice } from '@/stores'
import { useComponent } from '@/hooks/component'
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'))],
    ['calculator', defineAsyncComponent(() => import('./components/calculator/index.vue'))],
    ['certificate', defineAsyncComponent(() => import('./components/certificate/index.vue'))],
])

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

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

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

// 路由跳转
const routerTo = (name: string) => {
    router.push({ name })
}

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

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