<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"
                    :default-expanded-keys="monitoringPoint.state.lineId ? [monitoringPoint.state.lineId] : []"
                    :current-node-key="monitoringPoint.state.lineId"
                    @node-click="handleNodeClick"
                    @init="handleNodeClick"
                ></PointTree>
            </pane>
            <pane style="background: #fff" :style="height">
                <!-- <div :style="height"></div> -->
                <TableHeader ref="TableHeaderRef" date-picker :show-search="false">
                    <template v-slot:select>
                        <el-form-item label=" 模板策略">
                            <el-select v-model="value" placeholder="请选择" @change="changeFn" clearable filterable> 
                                <el-option
                                    v-for="item in templatePolicy"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </template>
                    <template #operation>
                        <el-button icon="el-icon-Download" type="primary" @click="exportEvent" :loading="loading">
                            生成报告
                        </el-button>
                    </template>
                </TableHeader>
                <div class="box" :style="`height: calc(${tableStore.table.height} + 45px)`">
                    <el-row>
                        <el-col :span="12" class="mTop">
                            <div class="grid-content">
                                <div class="divBox">监测点详情</div>
                            </div>
                        </el-col>
                        <el-col :span="12" class="mTop">
                            <el-checkbox v-model="formd.xq">监测点详情</el-checkbox>
                        </el-col>
                    </el-row>
                    <el-divider></el-divider>
                    <el-row>
                        <el-col :span="12" class="mTop">
                            <div class="grid-content">
                                <div class="divBox">暂降事件列表</div>
                            </div>
                        </el-col>
                        <el-col :span="12" class="mTop">
                            <el-checkbox v-model="formd.lb">表格</el-checkbox>
                        </el-col>
                    </el-row>
                    <el-divider></el-divider>
                    <el-row>
                        <el-col :span="12" class="mTop">
                            <div class="grid-content">
                                <div class="divBox">暂降密度</div>
                            </div>
                        </el-col>
                        <el-col :span="12" class="mTop">
                            <el-checkbox v-model="formd.mdbg">表格</el-checkbox>
                            <el-checkbox v-model="formd.mdtx">图形</el-checkbox>
                        </el-col>
                    </el-row>
                    <el-divider></el-divider>
                    <el-row>
                        <el-col :span="12" class="mTop">
                            <div class="grid-content">
                                <div class="divBox">暂降事件点</div>
                            </div>
                        </el-col>
                        <el-col :span="12" class="mTop">
                            <el-checkbox v-model="formd.sjdITIC">ITIC</el-checkbox>
                            <el-checkbox v-model="formd.sjdF47">F47</el-checkbox>
                        </el-col>
                    </el-row>
                    <el-divider></el-divider>
                    <el-row>
                        <el-col :span="12" class="mTop">
                            <div class="grid-content">
                                <div class="divBox">概率分布</div>
                            </div>
                        </el-col>
                        <el-col :span="12" class="mTop">
                            <el-checkbox v-model="formd.glfbfz">暂降幅值</el-checkbox>
                            <el-checkbox v-model="formd.glfbsj">持续时间</el-checkbox>
                        </el-col>
                    </el-row>
                    <el-divider></el-divider>
                    <el-row>
                        <el-col :span="12" class="mTop">
                            <div class="grid-content">
                                <div class="divBox">月份统计</div>
                            </div>
                        </el-col>
                        <el-col :span="12" class="mTop">
                            <el-checkbox v-model="formd.tjbg">表格</el-checkbox>
                            <el-checkbox v-model="formd.tjtx">图形</el-checkbox>
                        </el-col>
                    </el-row>
                    <el-divider></el-divider>
                    <el-row>
                        <el-col :span="12" class="mTop">
                            <div class="grid-content">
                                <div class="divBox">暂降原因</div>
                            </div>
                        </el-col>
                        <el-col :span="12" class="mTop">
                            <el-checkbox v-model="formd.yybg">表格</el-checkbox>
                            <el-checkbox v-model="formd.yytx">图形</el-checkbox>
                        </el-col>
                    </el-row>
                    <el-divider></el-divider>
                    <el-row>
                        <el-col :span="12" class="mTop">
                            <div class="grid-content">
                                <div class="divBox">暂降类型</div>
                            </div>
                        </el-col>
                        <el-col :span="12" class="mTop">
                            <el-checkbox v-model="formd.lxbg">表格</el-checkbox>
                            <el-checkbox v-model="formd.lxtx">图形</el-checkbox>
                        </el-col>
                    </el-row>
                    <el-divider></el-divider>
                </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/pointTree.vue'
import TableHeader from '@/components/table/header/index.vue'
import { mainHeight } from '@/utils/layout'
import { getLineExport, getList, selectReleation } from '@/api/event-boot/report'
import { useMonitoringPoint } from '@/stores/monitoringPoint'
import { ElMessage } from 'element-plus'
import dayjs from 'dayjs'
defineOptions({
    name: 'TransientReport/monitoringpointReport'
})
const monitoringPoint = useMonitoringPoint()
const height = mainHeight(20)
const size = ref(19)
const value = ref('')
const TableHeaderRef = ref()
const dotList: any = ref({
    name: monitoringPoint.state.lineName.split('>')[3],
    id: monitoringPoint.state.lineId,
    level: 6
})
const loading = ref(false)
const formd: any = ref({
    xq: false,
    lb: false,
    mdbg: false,
    mdtx: false,
    sjdITIC: false,
    sjdF47: false,
    glfbfz: false,
    glfbsj: false,
    tjbg: false,
    tjtx: false,
    yybg: false,
    yytx: false,
    lxbg: false,
    lxtx: false,
    type: 0
})

const templatePolicy: any = ref([])
const tableStore = new TableStore({
    url: '',
    method: 'post',
    column: []
})
provide('tableStore', tableStore)
onMounted(() => {
    const dom = document.getElementById('navigation-splitpanes')
    if (dom) {
        size.value = Math.round((180 / dom.offsetHeight) * 120)
    }
})
getList({
    pageNum: 1,
    pageSize: 100,
    type: 0
}).then(res => {
    templatePolicy.value = res.data.records
    // 默认选中第一个
    if (res.data.records && res.data.records.length > 0) {
        value.value = res.data.records[0].id
        // 触发 change 事件，加载对应的配置
        changeFn(res.data.records[0].id)
    }
})
const handleNodeClick = (data: any, node: any) => {
    dotList.value = data
}
const changeFn = (val: any) => {
    formd.value = {
        xq: false,
        lb: false,
        mdbg: false,
        mdtx: false,
        sjdITIC: false,
        sjdF47: false,
        glfbfz: false,
        glfbsj: false,
        tjbg: false,
        tjtx: false,
        yybg: false,
        yytx: false,
        lxbg: false,
        lxtx: false,
        type: 0
    }
    let data = {
        id: val
    }

    selectReleation(data).then(res => {
        res.data.forEach((item: any) => {
            for (let k in formd.value) {
                if (item.name == k) {
                    formd.value[k] = true
                    return
                }
            }
        })
    })
}
const exportEvent = () => {
    if (dotList.value.level != 6) {
        return ElMessage({
            message: '请选择监测点进行报告生成!',
            type: 'warning'
        })
    }
    let a = ''
    loading.value = true
    formd.value.lineId = dotList.value.id
    formd.value.lineName = dotList.value.name
    formd.value.searchBeginTime = TableHeaderRef.value.datePickerRef.timeValue[0]
    formd.value.searchEndTime = TableHeaderRef.value.datePickerRef.timeValue[1]
    formd.value.flag = TableHeaderRef.value.datePickerRef.interval
    ElMessage('生成报告中,请稍等!')
    getLineExport(formd.value)
        .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;
}
.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;
}
</style>
