<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 class="list-item" @click="toOrderService">
                <span class="text">工单服务\需求反馈</span>
                <img :src="getAssetsHomeFile('screen.png')" alt="" class="icon" @click.stop="screenShortClick">
                <img :src="getAssetsHomeFile('screen1.png')" alt="" class="icon1" @click.stop="screenShortClick">
            </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 { nextTick, ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useAppStore } from '~src/store'
import ScreenShot from 'js-web-screen-shot'
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,
    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 = getToObjVal(route, name, 'excelUrl')
}

//取视频地址
const getVideoUrl = (name) => {
    videoUrl.value = getToObjVal(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>
