<template>
    <div class="app-header">
        <div class="app-header__left">
            <slot name="left"></slot>
            <el-breadcrumb :separator-icon="ArrowRight" v-show="!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">
                <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="accountName" :src="userAvatar" />
                    <span v-if="!isMobile">{{ accountName }}</span>
                    <app-icon class="el-icon--right" icon="ArrowDown" />
                </span>
                <template #dropdown>
                    <el-dropdown-menu>
                        <el-dropdown-item :icon="SwitchButton"
                            @click="eventBus.$emit('LogoutNotify')">退出登录</el-dropdown-item>
                    </el-dropdown-menu>
                </template>
            </el-dropdown>
        </div>
    </div>
</template>

<script lang="ts" setup>
import { ref, onMounted, computed } from 'vue'
import { ArrowRight, SwitchButton } from '@element-plus/icons-vue'
import { getFileUrl } from '@/filters'
import { userStore, themeStore } from '@/stores'
import eventBus from '@/services/bus'
import AppIcon from '@pc/components/base/icon/index.vue'

const { isMobile } = themeStore.$mapState()
const { userInfo, accountName } = userStore.$mapGetters()
const fullScreen = ref(false)

// 用户头像
const userAvatar = computed(() => {
    const file = userInfo.value?.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>