<template>
    <div class="default-main" :style="height">
        <!-- 标准库 -->

        <splitpanes
            style="height: 100%"
            class="default-theme"
            id="navigation-splitpanes"
            @resize="onPaneResize"
            @resized="onPaneResized"
        >
            <pane :size="size">
                <standardTree ref="treeRef"
                    :default-expanded-keys="monitoringPoint.state.lineId ? [monitoringPoint.state.lineId] : []"
                    :current-node-key="monitoringPoint.state.lineId" @node-click="handleNodeClick"
                    @init="handleNodeClick"></standardTree>
            </pane>
            <pane style="background: #fff" class="standard-pane">
                <div class="pd10 standard-pane__toolbar" style="display: flex; justify-content: end">
                    <el-button icon="el-icon-Plus" type="primary" @click="addUser" v-if="information">新增</el-button>
                    <el-button icon="el-icon-Edit" type="primary" @click="editUser" v-if="information">修改</el-button>
                    <el-button icon="el-icon-Delete" type="primary" @click="deleteEven"
                        v-if="information">删除</el-button>
                    <el-button icon="el-icon-Download" type="primary" @click="download" v-if="flag">下载</el-button>
                </div>
                <el-empty v-if="url.length == 0" description="暂无数据" class="custom-empty standard-pane__content" />
                <div v-else class="standard-pane__content">
                    <div v-show="!isPaneResizing" class="standard-preview">
                        <vue-office-docx v-if="urlKey.includes('.doc') || urlKey.includes('.docx')" :src="url" />
                        <vue-office-excel v-if="urlKey.includes('.xls') || urlKey.includes('.xlsx')" :src="url"
                            :options="excelOptions" />
                        <!-- <vue-office-pdf v-if="url.includes('.pdf')" :src="url"/> -->
                        <iframe v-if="urlKey.includes('.pdf')" :src="url" style="width: 100%; height: 100%"></iframe>
                        <img v-if="
                            urlKey.includes('.png') || urlKey.includes('.jpg') || urlKey.includes('.gif') || urlKey.includes('.bmp')
                        " :src="url" />
                    </div>
                </div>
            </pane>
        </splitpanes>
        <!-- 新增 -->

        <addTree ref="addTreeRef" @onSubmit="treeRef.loadData(dotList.id)"></addTree>
    </div>
</template>
<script setup lang="ts">
import { onMounted, ref, provide } from 'vue'
import 'splitpanes/dist/splitpanes.css'
import { Splitpanes, Pane } from 'splitpanes'
import standardTree from '@/components/tree/pqs/standardTree.vue'
import { mainHeight } from '@/utils/layout'
import addTree from './components/addTree.vue'
import { useMonitoringPoint } from '@/stores/monitoringPoint'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getFileNameAndFilePath, downloadFile } from '@/api/system-boot/file'

//引入相关样式
import '@vue-office/excel/lib/index.css'
//引入VueOfficeDocx组件
import VueOfficeDocx from '@vue-office/docx'
import VueOfficeExcel from '@vue-office/excel'
//引入VueOfficePdf组件
import VueOfficePdf from '@vue-office/pdf'
import { deleteyLibstandard } from '@/api/supervision-boot/database/index'
import { useAdminInfo } from '@/stores/adminInfo'
import {previewFile } from '@/utils/fileDownLoad'
const adminInfo = useAdminInfo()
defineOptions({
    name: 'database/standard'
})
const excelOptions = ref({})
const monitoringPoint = useMonitoringPoint()
const height = mainHeight(20)
const size = ref(19)
const treeRef = ref()
const addTreeRef = ref()
const url = ref('')
const dotList: any = ref({})
const flag: any = ref(false)
const isPaneResizing = ref(false)
const information = adminInfo.roleCode.includes('information_info')
const onPaneResize = () => {
    isPaneResizing.value = true
}
const onPaneResized = () => {
    requestAnimationFrame(() => {
        isPaneResizing.value = false
    })
}
onMounted(() => {
    const dom = document.getElementById('navigation-splitpanes')
    if (dom) {
        size.value = Math.round((180 / dom.offsetHeight) * 120)
    }
})

const urlKey=ref('')
const VITE_FLAG = false//import.meta.env.VITE_NAME == 'jibei'
const handleNodeClick = (data: any, node: any) => {
   // console.log("🚀 ~ handleNodeClick ~ data:", data)
    dotList.value = data
    url.value = ''
    flag.value = false
    if (data?.url != null && data?.url != '') {
        flag.value = true
        urlKey.value = data.url
        console.log('urlKey.value',urlKey.value)
        setTimeout(async () => {
             if(VITE_FLAG){
                   url.value = `/api-docx/excelreport` + data.url
                   excelOptions.value = { xls: data.url.split('.')[1] == 'xls' ? true : false }
             }else{
                   const previewUrl = await previewFile(data.url);
                    url.value = previewUrl;
             }
         
            // getFileNameAndFilePath({ filePath: data.url }).then(res => {
            //     url.value = res.data.url
            // })
        }, 100)
    }
}
// 删除
const deleteEven = () => {
    ElMessageBox.confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    }).then(() => {
        deleteyLibstandard({ id: dotList.value.id }).then(() => {
            ElMessage({
                type: 'success',
                message: '删除成功！'
            })
            setTimeout(() => {
                treeRef.value.loadData()
            }, 100)
        })
    })
}
const addUser = () => {
    addTreeRef.value.open('新增')
}
const editUser = () => {
    addTreeRef.value.open('修改', dotList.value)
}
// 下载
const download = () => {
    let url = dotList.value.url
    let urls = url
    let name = url.match(/\/([^/]+)\.(\w+)$/)[1]
    downloadFile({ filePath: url }).then((res: any) => {
        let blob = new Blob([res], {
            type: urls.includes('.pdf')
                ? 'application/pdf'
                : urls.includes('.docx')
                    ? 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
                    : urls.includes('.xls')
                        ? 'application/vnd.ms-excel'
                        : urls.includes('.xlsx')
                            ? 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
                            : urls.includes('.png')
                                ? 'image/png'
                                : urls.includes('.jpeg')
                                    ? 'image/jpeg'
                                    : urls.includes('.jpg')
                                        ? 'image/jpg'
                                        : ''
        })
        const url = window.URL.createObjectURL(blob)
        const link = document.createElement('a')
        link.href = url
        link.download = name
        document.body.appendChild(link)
        link.click()
        link.remove()
    })
}
</script>
<style lang="scss" scoped>
.splitpanes.default-theme .splitpanes__pane {
    background: #eaeef1;
}

:deep(.splitpanes__pane) {
    overflow: hidden;
}

.standard-pane {
    display: flex;
    flex-direction: column;
    height: 100%;
    overflow: hidden;
}

.standard-pane__toolbar {
    flex-shrink: 0;
}

.standard-pane__content {
    flex: 1;
    min-height: 0;
    overflow: auto;
}

.standard-preview {
    height: 99%;
    min-height: 99%;
}

:deep(.splitpanes--dragging) .standard-preview {
    visibility: hidden;
}

.grid-content {
    text-align: center;
}

.divBox {
    width: 250px;
    height: 31px;
    margin: auto;
    line-height: 32px;
    border: 1px solid #c9c9c9;

    &:hover {
        border: 1px solid #002255;
    }
}

.box {
    padding: 10px;
    // margin-top: 10px;
    overflow-y: auto;
    font-size: 15px;
}

.el-divider--horizontal {
    margin: 10px 0;
}

.mTop {
    margin-top: 5px;
    margin-bottom: 5px;
}

/* 自定义 el-empty 的样式 */
:deep(.custom-empty) {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    /* 调整高度 */
    padding: 20px;

    /* 调整内边距 */
    .el-empty__image {
        display: none;
        /* 隐藏默认图片 */
    }

    .el-empty__description {
        font-size: 14px;
        /* 调整字体大小 */
        color: var(--vxe-font-color);
    }
}
</style>
