<template>
    <div>
        <!-- 表头 -->
        <TableHeader date-picker>
            <template v-slot:operation>
                <el-button :icon='Download' type='primary' @click='download'>波形下载</el-button>
            </template>
        </TableHeader>
        <!-- 表格 -->
        <Table ref='tableRef' :checkboxConfig='checkboxConfig' />
    </div>
</template>
<script setup lang='ts'>
import { Download } from '@element-plus/icons-vue'
import { ref, onMounted, provide, reactive } 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 { VxeTablePropTypes } from 'vxe-table'
import { downloadWaveFile, getMonitorEventAnalyseWave } from '@/api/event-boot/transient'

const dictData = useDictData()
const eventTypeOptions = dictData.getBasicData('Event_Statis')
const monitoringPoint = useMonitoringPoint()
const tableStore = new TableStore({
    publicHeight: 60,
    url: '/event-boot/monitor/getMonitorEventAnalyseQuery',
    method: 'POST',
    column: [
        { width: '60', type: 'checkbox' },
        { title: '序号', type: 'seq', width: '60' },
        { title: '变电站名称', field: 'subName', minWidth: '130' },
        { title: '监测点名称', field: 'lineName', minWidth: '130' },
        { title: '网络参数', field: 'ip', minWidth: '130' },
        { title: '电压等级(kV)', field: 'voltageScale', width: '100' },
        { title: '暂降发生时刻', field: 'startTime', width: '200' },
        { title: '暂降类型', field: 'advanceType', minWidth: '130' },
        { title: '暂降原因', field: 'advanceReason', minWidth: '130' },
        { title: '触发类型', field: 'eventTypeName', minWidth: '80' },
        { title: '暂降(骤升)幅值(%)', field: 'featureAmplitude', minWidth: '80' },
        { title: '持续时间(s)', field: 'duration', minWidth: '80' },
        { title: '严重度', field: 'severity', minWidth: '80' },
        {
            title: '操作',
            width: '180',
            render: 'buttons',
            fixed: 'right',
            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('暂无可下载的波形文件!')
                        })
                    }
                }
            ]
        }
    ],
    beforeSearchFun: () => {
        tableStore.table.params.lineId = monitoringPoint.state.lineId
        console.log('beforeSearchFun')
    },
    loadCallback: () => {
        tableStore.table.data.forEach((item: any) => {
            item.eventTypeName = eventTypeOptions.find(item2 => item2.id === item.eventType)?.name
            item.featureAmplitude = (item.featureAmplitude * 100).toFixed(0)
        })
    }
})
provide('tableStore', tableStore)
tableStore.table.params.searchState = 0
onMounted(() => {
    // 加载数据
    tableStore.index()
})
const checkboxConfig = reactive<VxeTablePropTypes.CheckboxConfig<any>>({
    checkMethod: ({ row }) => {
        return row.fileFlag === 1
    }
})
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.success('下载中。。。!')
        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) //释放标签
    })
}
</script>
