<template>
    <el-popover :width="220" placement="bottom" trigger="hover">
        <template #reference>
            <div class="header-icon-bar">
                <HcIcon class="header-icon" name="question"/>
            </div>
        </template>
        <div class="header-pover-menu-list">
            <div class="list-item">
                <span class="text">开启功能气泡提示</span>
                <el-switch v-model="bubbleVal" @change="bubbleUpdate"/>
            </div>
            <div v-if="excelUrl" class="list-item" @click="excelPreviewClick">
                <span class="text">查看系统操作文档</span>
                <img :src="getAssetsHomeFile('word.png')" alt="" class="icon"/>
                <img :src="getAssetsHomeFile('word1.png')" alt="" class="icon1"/>
            </div>
            <div v-if="videoUrl" class="list-item" @click="videoPreviewModal = true">
                <span class="text">当前页面功能讲解</span>
                <img :src="getAssetsHomeFile('video.png')" alt="" class="icon"/>
                <img :src="getAssetsHomeFile('video1.png')" alt="" class="icon1"/>
            </div>
        </div>
    </el-popover>
    <!--当前页面功能讲解-->
    <el-dialog v-model="videoPreviewModal" :before-close="videoPreviewModalClose" destroy-on-close width="62rem">
        <video :src="videoUrl" autoplay="autoplay" class="preview-video" controls="controls">
            您的浏览器不支持 video
        </video>
    </el-dialog>
</template>

<script setup>
import {ref, watch, nextTick} from "vue";
import {useRouter, useRoute} from 'vue-router'
import {useAppStore} from "~src/store";
import {getStoreValue} from '~src/utils/storage'
import {getObjValue, getToObjVal} from "js-fast-way"

//初始变量
const router = useRouter()
const useRoutes = useRoute()
const useAppState = useAppStore()

//相关变量
const route = getObjValue(getStoreValue('route'))
const bubbleVal = ref(useAppState.getBubble);
const videoUrl = ref('')
const excelUrl = ref('')
const videoPreviewModal = ref(false)
const webRtcVal = ref(false)
const fullScreenVal = ref(true)

//监听
watch(() => [
    useAppState.getBubble,
    useRoutes.name,
], ([Bubble, routeName]) => {
    bubbleVal.value = Bubble
    getVideoUrl(routeName)
    getExcelUrl(routeName)
})

//渲染完成
nextTick(() => {
    getVideoUrl(useRoutes?.name)
    getExcelUrl(useRoutes?.name)
})

//取文档地址
const getExcelUrl = (name) => {
    excelUrl.value = getToObjVal(route, name, 'excelUrl')
}

//取视频地址
const getVideoUrl = (name) => {
    videoUrl.value = getToObjVal(route, name, 'videoUrl')
}

//开关值改变
const bubbleUpdate = (val) => {
    bubbleVal.value = val;
    useAppState.setBubble(val);
}

// 获取assets静态资源
const getAssetsHomeFile = (url) => {
    const path = `../../assets/images/${url}`;
    const modules = import.meta.globEager("../../assets/images/*");
    return modules[path].default;
}

//关闭视频弹窗
const videoPreviewModalClose = () => {
    videoPreviewModal.value = false
}

//查看系统操作文档
const excelPreviewClick = () => {
    if (excelUrl.value) {
        window.open(excelUrl.value, '_blank')
    } else {
        window?.$message.warning('暂无文档')
    }
}
</script>

<style lang="scss" scoped>
.header-icon-bar {
    position: relative;
    height: 40px;
    width: 40px;
    border-radius: 100px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 26px;
    cursor: pointer;
    margin-right: 30px;
    border: 1px solid #00000000;
    background: #f1f5f8;
    color: #202532;
    box-shadow: var(--hc-shadow);
}
.hc-layout-box .hc-container-view.home .hc-header-view .hc-header-content .header-icon-bar {
    border: 1px solid white;
    color: inherit;
    box-shadow: initial;
    background: initial;
}

.header-pover-menu-list {
    position: relative;
    margin: -5px -12px;
    .list-item {
        position: relative;
        padding: 10px 24px;
        cursor: pointer;
        display: flex;
        align-items: center;
        transition: background-color 0.2s;
        .text {
            flex: auto;
        }
        .icon, .icon1 {
            width: 24px;
            height: 24px;
        }
        .icon1 {
            display: none;
        }
        &:hover {
            color: #ffffff;
            background-color: var(--el-color-primary);
            .icon {
                display: none;
            }
            .icon1 {
                display: block;
            }
        }
    }
}
.preview-video {
    width: 100%;
}
</style>
