<template>
    <web-view :update-title="title" :webview-styles="webStyle" :style="webStyle" :src="pdfSrc" v-if="pdfSrc"/>
</template>

<script setup>
import {ref, watch, onMounted} from "vue";

//初始变量
const props = defineProps({
    ui: {
        type: Object,
        default: () => ({})
    },
    title: {
        type: Boolean,
        default: true,
    },
    src: String,
});

const windowInfo = uni.getWindowInfo()
const pdfSrc = ref('')
const webStyle = ref({})

//渲染完成
onMounted(() => {
    setPdfSrc(props.src)
    setWebStyle(props.ui)

    // #ifdef APP-PLUS
    //ios 禁用缓存，测试生效！！
    let cache1 = plus.ios.newObject('NSURLCache');
    let cache = plus.ios.invoke(cache1, 'sharedURLCache');
    plus.ios.invoke(cache, 'removeAllCachedResponses');
    plus.ios.invoke(cache, 'setDiskCapacity:', 0);
    plus.ios.invoke(cache, 'setMemoryCapacity:', 0);

    //安卓端缓存清理。
    plus.cache.clear();
    // #endif
})

//监听变化
watch(() => [
    props.src
], ([src]) => {
    setPdfSrc(src)
})

//监听样式变化
watch(() => [
    props.ui
], ([ui]) => {
    setWebStyle(ui)
}, {deep: true})

//设置pdf的url
const setPdfSrc = (src) => {
    const url = '/hybrid/html/pdf/index.html?src='
    if (src) {
        pdfSrc.value = url + src
    } else {
        pdfSrc.value = ''
    }
}

//设置webview的样式
const setWebStyle = (data) => {
    const {windowHeight} = windowInfo
    webStyle.value = {
        ...data,
        height: data.height ?? windowHeight + 'px'
    }
}
</script>
