<template>
    <el-popover placement="bottom" :width="220" trigger="hover">
        <template #reference>
            <div class="header-icon-bar">
                <HcIcon name="question" class="header-icon"/>
            </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 class="list-item" @click="excelPreviewClick" v-if="excelUrl">
                <span class="text">查看系统操作文档</span>
                <img class="icon" :src="getAssetsHomeFile('word.png')" alt=""/>
                <img class="icon1" :src="getAssetsHomeFile('word1.png')" alt=""/>
            </div>
            <div class="list-item" @click="videoPreviewModal = true" v-if="videoUrl">
                <span class="text">当前页面功能讲解</span>
                <img class="icon" :src="getAssetsHomeFile('video.png')" alt=""/>
                <img class="icon1" :src="getAssetsHomeFile('video1.png')" alt=""/>
            </div>
            <div class="list-item" @click="toOrderService">
                <span class="text">工单服务\需求反馈</span>
                <img class="icon" :src="getAssetsHomeFile('screen.png')" alt="" @click.stop="screenShortClick"/>
                <img class="icon1" :src="getAssetsHomeFile('screen1.png')" alt="" @click.stop="screenShortClick"/>
            </div>
        </div>
    </el-popover>
    <!--当前页面功能讲解-->
    <el-dialog v-model="videoPreviewModal" width="62rem" destroy-on-close :before-close="videoPreviewModalClose">
        <video class="preview-video" :src="videoUrl" controls="controls" autoplay="autoplay">
            您的浏览器不支持 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 ScreenShot from "js-web-screen-shot";
import { getStoreData }  from '~src/utils/storage'
import { getOneObjValue, getObjValue } from "vue-utils-plus"

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

//相关变量
const route = getObjValue(getStoreData('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,
    useAppState.getShotWebRtc,
    useAppState.getFullScreen,
], ([Bubble,routeName,webRtc,fullScreen]) => {
    bubbleVal.value = Bubble
    getVideoUrl(routeName)
    getExcelUrl(routeName)
    setScreenShotData(webRtc,fullScreen)
})

//渲染完成
nextTick(() => {
    getVideoUrl(useRoutes?.name)
    getExcelUrl(useRoutes?.name)
    const webRtc = useAppState.getShotWebRtc
    const fullScreen = useAppState.getFullScreen
    setScreenShotData(webRtc,fullScreen)
})

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

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

//处理屏幕截图的配置
const setScreenShotData = (webRtc,fullScreen) => {
    webRtcVal.value = parseInt(webRtc) === 1
    fullScreenVal.value = parseInt(fullScreen) === 1
}

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

//工单服务\需求反馈
const screenShortClick = () => {
    nextTick(() => {
        new ScreenShot({
            enableWebRtc: webRtcVal.value,        //截图方式
            clickCutFullScreen: fullScreenVal.value,   //全屏
            loadCrossImg: true,         //跨域
            level: 999999,
            completeCallback: getScreenShotImg,
            noScroll: false
        });
        /*setTimeout(() => {}, 800)*/
    })
}

//获取裁剪区域图片信息
const getScreenShotImg = (base64) => {
    window.sessionStorage.setItem('screenShort-base64', base64);
    //跳转到工单服务\需求反馈
    const useRoutePath = useRoutes.path
    useAppState.setScreenShort(true)
    if (useRoutePath !== "/other/order-service") {
        router.push({name: 'order-service'});
    }
}

//工单服务\需求反馈
const toOrderService = () => {
    router.push({name: 'order-service'});
}

// 获取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>
