<template>
    <div class="default-main" :style="height">
        <splitpanes style="height: 100%" class="default-theme" id="navigation-splitpanes">
            <pane :size="size">
                <PointTree
                    :default-expand-all="false"
                    @node-click="handleNodeClick"
                    @init="handleNodeClick"
                ></PointTree>
            </pane>
            <pane style="background: #fff" :style="height">
                <TableHeader ref="TableHeaderRef" datePicker :show-search="false">
                    <template #operation>
                        <el-button icon="el-icon-Download" type="primary" @click="exportEvent" :loading="loading">
                            生成
                        </el-button>
                    </template>
                </TableHeader>
                <div class="box">
                    <div id="luckysheet">
                        <img
                            width="100%"
                            :style="`height: calc(${tableStore.table.height} + 40px)`"
                            src="@/assets/img/region.png"
                        />
                    </div>
                </div>
            </pane>
        </splitpanes>
    </div>
</template>

<script setup lang="ts">
import { onMounted, ref, provide } from 'vue'
import 'splitpanes/dist/splitpanes.css'
import { Splitpanes, Pane } from 'splitpanes'
import TableStore from '@/utils/tableStore'
import PointTree from '@/components/tree/pqs/areaTree.vue'
import TableHeader from '@/components/table/header/index.vue'
import { useDictData } from '@/stores/dictData'
import { mainHeight } from '@/utils/layout'
import { areaHarmonicReport } from '@/api/process-boot/reportForms'
import { genFileId, ElMessage, ElNotification } from 'element-plus'
import type { UploadProps, UploadUserFile } from 'element-plus'
import dayjs from 'dayjs'

defineOptions({
    name: 'harmonic-boot/region/word'
})
const height = mainHeight(20)
const size = ref(19)
const loading = ref(false)
const dictData = useDictData()
const TableHeaderRef = ref()
const dotList: any = ref({})
const Template: any = ref({})

const tableStore = new TableStore({
    url: '',
    method: 'POST',
    column: [],
    beforeSearchFun: () => {},
    loadCallback: () => {}
})
provide('tableStore', tableStore)

onMounted(() => {
    const dom = document.getElementById('navigation-splitpanes')

    if (dom) {
        size.value = Math.round((180 / dom.offsetHeight) * 120)
    }
})

const handleNodeClick = (data: any, node: any) => {
    dotList.value = data
}

// 生成
const exportEvent = () => {
    loading.value = true
    ElMessage('生成报告中...')
    areaHarmonicReport({
        deptId: dotList.value.id,
        areaReportFlag: 1,
        startTime: TableHeaderRef.value.datePickerRef.timeValue[0],
        endTime: TableHeaderRef.value.datePickerRef.timeValue[1]
    })
        .then((res: any) => {
            if (res == undefined) {
                loading.value = false
                return
            }
            let blob = new Blob([res], {
                type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=UTF-8'
            })

            // createObjectURL(blob); //创建下载的链接
            const url = window.URL.createObjectURL(blob)
            const link = document.createElement('a') // 创建a标签
            link.href = url
            link.download = dotList.value.name + '区域稳态报告' + dayjs().format('YYYYMMDD') // 设置下载的文件名
            document.body.appendChild(link)
            link.click() //执行下载
            document.body.removeChild(link)
            loading.value = false
        })
        .catch(() => {
            loading.value = false
        })
}
</script>
<style lang="scss">
.splitpanes.default-theme .splitpanes__pane {
    background: #eaeef1;
}

.box {
    padding: 10px;
}
</style>
