<template>
    <div>
        <div v-show="view">
            <!-- 表头 -->
            <TableHeader ref="TableHeaderRef" date-picker showExport showCustomColumn>
                <template v-slot:operation>
                    <el-button :icon="Download" type="primary" @click="download">下载波形</el-button>
                </template>
            </TableHeader>
            <!-- 表格 -->
            <Table ref="tableRef" :checkboxConfig="checkboxConfig" />
        </div>
        <div :style="{ height: pageHeight.height }" style="padding: 10px; overflow: hidden" v-if="!view">
            <waveForm ref="waveFormRef" senior @backbxlb="backbxlb" />
        </div>
    </div>
</template>
<script setup lang="ts">
import { Download } from '@element-plus/icons-vue'
import { ref, onMounted, provide, reactive, nextTick } from 'vue'
import { ElMessageBox, ElMessage } from 'element-plus'
import TableStore from '@/utils/tableStore'
import Table from '@/components/table/index.vue'
import TableHeader from '@/components/table/header/index.vue'
import { mainHeight } from '@/utils/layout'
import { useMonitoringPoint } from '@/stores/monitoringPoint'
import { useDictData } from '@/stores/dictData'
import { checkUser } from '@/api/user-boot/user'
import waveForm from '@/components/echarts/waveForm.vue'
import { VxeTablePropTypes } from 'vxe-table'
import { downloadWaveFile, getMonitorEventAnalyseWave } from '@/api/event-boot/transient'
const props = defineProps({
    externalHeight: {
        type: Number,
        default: 0
    }
})
const view = ref(true)
const waveFormRef = ref()
const view2 = ref(false)
const pageHeight = mainHeight(20)
const dictData = useDictData()
const eventTypeOptions = dictData.getBasicData('Event_Statis')
const monitoringPoint = useMonitoringPoint()
const tableStore = new TableStore({
    publicHeight: 60 + props.externalHeight,
    url: '/event-boot/monitor/getMonitorEventAnalyseQuery',
    method: 'POST',
    filename: '暂态事件列表',
    column: [
        { width: '60', type: 'checkbox' },
        {
            title: '序号',
            width: 80,
            formatter: (row: any) => {
                return (tableStore.table.params.pageNum - 1) * tableStore.table.params.pageSize + row.rowIndex + 1
            }
        },
        { title: '暂降发生时刻', field: 'startTime', width: '200',    sortable: true, },
        {
            title: '暂降(骤升)幅值(%)',
            field: 'featureAmplitude',
            minWidth: '160',
            sortable: true,
            formatter: (row: any) => {
                return Math.floor(row.cellValue * 10000) / 100
            }
        },
        { title: '持续时间(s)', field: 'duration', minWidth: '120' ,   sortable: true, },
        {
            title: '触发类型',
            field: 'eventType',
            minWidth: '100',
            formatter: (row: any) => {
                return eventTypeOptions.find(item => item.id === row.cellValue)?.name || '/'
            }
        },
        { title: '暂降原因', field: 'advanceReason', minWidth: '130' },
        { title: '变电站名称', field: 'subName', minWidth: '140' },
        { title: '监测点名称', field: 'lineName', minWidth: '130' },
        { title: '网络参数', field: 'ip', width: '120px' },
        { title: '电压等级(kV)', field: 'voltageScale', width: '120' },

        // { title: '触发类型', field: 'advanceType', minWidth: '130' },

        // { title: '严重度', field: 'severity', minWidth: '80' },
        {
            title: '操作',
            fixed: 'right',
            width: '180',
            render: 'buttons',

            buttons: [
                {
                    name: 'edit',
                    title: '波形分析',
                    type: 'primary',
                    icon: 'el-icon-Lock',
                    render: 'basicButton',
                    disabled: row => {
                        return row.fileFlag === 0
                    },
                    click: row => {
                        // getMonitorEventAnalyseWave({
                        //     id: row.eventId,
                        //     systemType: 0,
                        //     type: 0
                        // }).then(res => {
                        //     console.log(res)
                        //     ElMessage.error('暂无可下载的波形文件!')
                        // })
                        view.value = false
                        setTimeout(() => {
                            waveFormRef.value.open(row)
                        }, 100)
                    }
                },
                {
                    name: 'edit',
                    title: '暂无波形',
                    type: '',
                    disabled: row => {
                        return row.fileFlag != 0
                    },
                    icon: 'el-icon-Plus',
                    render: 'basicButton'
                }
            ]
        }
    ],
    beforeSearchFun: () => {
        tableStore.table.params.lineId = monitoringPoint.state.lineId
    },
    loadCallback: () => {}
})
const TableHeaderRef = ref()
provide('tableStore', tableStore)
tableStore.table.params.searchState = 0
onMounted(() => {
    TableHeaderRef.value.setTheDate(1)
    // 加载数据
    nextTick(() => {
        // tableStore.index()
        if (monitoringPoint.state.lineId) {
            TableHeaderRef.value.onComSearch()
        }
    })
})
const checkboxConfig = reactive<VxeTablePropTypes.CheckboxConfig<any>>({
    checkMethod: ({ row }) => {
        return row.fileFlag === 1
    }
})
const backbxlb = () => {
    view.value = true
    view2.value = false
}
const download = () => {
    if (!tableStore.table.selection.length) {
        ElMessage.warning('请选择数据')
        return
    }
    downloadWaveFile({
        lineId: tableStore.table.selection.map((item: any) => item.eventId)
    }).then((res: any) => {
        if (res.type == 'application/json') {
            ElMessage.warning('暂无可下载的波形文件!')
            return
        }
        ElMessage.info('下载中......')
        let blob = new Blob([res], { type: 'application/zip' }) // console.log(blob) // var href = window.URL.createObjectURL(blob); //创建下载的链接
        const url = window.URL.createObjectURL(blob)
        const link = document.createElement('a') // 创建a标签
        link.href = url
        link.download = '波形分析下载' // 设置下载的文件名
        document.body.appendChild(link)
        link.click() //执行下载
        document.body.removeChild(link) //释放标签
           ElMessage.success('波形下载成功')
    })
}
</script>
