<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>
                        <!--<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>
                    <span class="g-icon--notice"></span>
                </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="g-icon--language"></span>
                <template #dropdown>
                    <el-dropdown-menu>
                        <template v-for="item in getLanguageList()" :key="item.value">
                            <el-dropdown-item @click="switchLanguage(item.value)">{{ item.label }}</el-dropdown-item>
                        </template>
                    </el-dropdown-menu>
                </template>
            </el-dropdown>
            <el-dropdown class="user-dropdown" trigger="click">
                <span class="user-dropdown__link">
                    <span>{{ userStore.userInfo.username }}</span>
                    <span>({{ userStore.userInfo.logincode }})</span>
                    <el-icon class="el-icon--right">
                        <arrow-down />
                    </el-icon>
                </span>
                <template #dropdown>
                    <el-dropdown-menu>
                        <el-dropdown-item :icon="Key">修改密码</el-dropdown-item>
                        <el-dropdown-item :icon="SwitchButton" @click="eventBus.$emit('Logout')">退出登录</el-dropdown-item>
                    </el-dropdown-menu>
                </template>
            </el-dropdown>
        </div>
    </div>
</template>

<script lang="ts" setup>
import { ref, onMounted } from 'vue'
import { ArrowRight, Key, SwitchButton, ArrowDown } from '@element-plus/icons-vue'
import { getLanguageList, Language } from '@/constants/language'
import { useGlobalStore, useUserStore, i18n } from '@/stores'
import { localData } from '@/stores/storage'
import eventBus from '@/services/bus'

const globalStore = useGlobalStore()
const userStore = useUserStore()
const fullScreen = ref(false)

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

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

// 切换语言
const switchLanguage = (lange: Language) => {
    i18n.global.locale = lange
    localData.setValue('appLanguage', lange)
}

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

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