<template>
    <hc-new-card>
        <template #extra>
            <el-button type="primary" @click="goBack">
                <HcIcon name="close" />
                关闭
            </el-button>
        </template>
        <hc-pdf :src="pdfUrl" :download="isDownload" :print="isPrint" />
    </hc-new-card>
</template>

<script setup>
import { isNullES } from 'js-fast-way'
import { onActivated, ref } from 'vue'
import { useRoute } from 'vue-router'
import { btnAuth, decode } from '~uti/btn-auth'
import router from '~src/router/index'

//初始变量
const useRoutes = useRoute()
const isDownload = ref(btnAuth('client-pdf-download'))
const isPrint = ref(btnAuth('client-pdf-print'))

defineOptions({
    name: 'Pdf',
})

//渲染完成
const pdfUrl = ref('')
onActivated(() => {
    const { url, code } = useRoutes.query
    //如果url和code都为空，不做处理
    if (isNullES(url) && isNullES(code)) {
        return
    }
    //如果url不为空，code为空，以url为准
    if (!isNullES(url) && isNullES(code)) {
        pdfUrl.value = url
        return
    }
    //如果url为空，code不为空，以code为准
    if (isNullES(url) && !isNullES(code)) {
        pdfUrl.value = decode(code ?? '')
        return
    }
    //两个都有的情况下，以code为准
    pdfUrl.value = decode(code ?? '')
})
const goBack = ()=>{
    router.back()
}
</script>
