<template>
    <div>
        <!-- 暂态事件详情 -->
        <el-dialog draggable title="暂态事件详情 " v-model="dialogVisible" append-to-body width="70%">
            <TableHeader datePicker showExport :showReset="false" ref="tableHeaderRef" @selectChange="selectChange">
                <template v-slot:select>
                    <el-form-item label="监测点" v-if="props.showLine">
                        <el-select v-model="tableStore.table.params.lineId" filterable placeholder="请选择监测点名称">
                            <el-option
                                v-for="item in options"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            />
                        </el-select>
                    </el-form-item>
                    <el-form-item label="暂态类型">
                        <el-select
                            v-model="tableStore.table.params.eventType"
                            style="min-width: 150px"
                            clearable
                            placeholder="请选择暂态类型"
                        >
                            <el-option
                                v-for="item in eventList"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            />
                        </el-select>
                    </el-form-item>
                </template>
            </TableHeader>
            <Table ref="tableRef" isGroup :height="heightRef"></Table>
        </el-dialog>
        <!-- 查看波形 -->
        <el-dialog
            v-model="isWaveCharts"
            draggable
            title="波形分析"
            append-to-body
            v-if="isWaveCharts"
            width="70%"
            @close="handleHideCharts"
        >
            <waveFormAnalysis
                v-loading="loading"
                ref="waveFormAnalysisRef"
                @handleHideCharts="handleHideCharts"
                :wp="wp"
            />
        </el-dialog>
    </div>
</template>
<script setup lang="ts">
import { ref, provide } from 'vue'
import Table from '@/components/table/index.vue'
import TableHeader from '@/components/table/header/index.vue'
import TableStore from '@/utils/tableStore'
import { mainHeight } from '@/utils/layout'
import waveFormAnalysis from '@/views/govern/device/control/tabs/components/waveFormAnalysis.vue'
import { analyseWave } from '@/api/common'
import { getSimpleLine } from '@/api/harmonic-boot/cockpit/cockpit'
interface Props {
    showLine?: boolean
}

const props = withDefaults(defineProps<Props>(), {
    showLine: true
})

const dialogVisible: any = ref(false)
const waveFormAnalysisRef: any = ref(null)
// 波形
const isWaveCharts = ref(false)
const loading = ref(false)
const wp = ref({})
const boxoList: any = ref({})

const tableHeaderRef = ref()

const options = ref()
const heightRef = ref(mainHeight(168, 2.1).height)
const selectChange = (flag: boolean, h: any) => {
    heightRef.value = mainHeight(h, 2.1).height
}
const eventList = [
    { label: '电压暂降', value: '1' },
    { label: '电压中断', value: '2' },
    { label: '电压暂升', value: '3' }
]
const getSimpleLineList = async () => {
    const res = await getSimpleLine()
    options.value = res.data
}

const tableStore: any = new TableStore({
    url: '/cs-harmonic-boot/event/pageEvent',
    method: 'POST',
    showPage: true,
    exportName: '暂态事件详情',
    column: [
        {
            field: 'index',
            title: '序号',
            width: '80',
            formatter: (row: any) => {
                return (tableStore.table.params.pageNum - 1) * tableStore.table.params.pageSize + row.rowIndex + 1
            }
        },
        {
            title: '暂态时间',
            field: 'startTime',
            minWidth: '180'
        },
        {
            title: '测点名称',
            field: 'lineName',
            minWidth: '150'
        },
        {
            title: '暂态类型',
            field: 'tag',
            minWidth: '100'
        },
        {
            title: '特征幅值(%)',
            field: 'amplitude',
            minWidth: '100'
        },
        {
            title: '暂降深度(%)',
            field: 'depth',
            minWidth: '100',
            formatter: (row: any) => {
                // 当暂态类型不是电压暂升时，计算暂降深度 = 100 - 特征幅值
                if (row.row.tag !== '电压暂升') {
                    const amplitude = parseFloat(row.row.amplitude)
                    if (!isNaN(amplitude)) {
                        return 100 - amplitude
                    }
                    return '-'
                } else {
                    // 电压暂升时不显示暂降深度
                    return '/'
                }
            }
        },
        {
            title: '持续时间(S)',
            field: 'persistTime',
            minWidth: '100'
        },
        {
            title: '严重度',
            field: 'severity',
            minWidth: '80'
        },
        {
            title: '波形',
            minWidth: '100',
            render: 'buttons',
            buttons: [
                {
                    name: 'edit',
                    text: '波形分析',
                    type: 'primary',
                    icon: 'el-icon-DataLine',
                    render: 'basicButton',
                    disabled: row => {
                        return !row.wavePath
                    },

                    click: async row => {
                        row.loading1 = true
                        loading.value = true
                        isWaveCharts.value = true
                        dialogVisible.value = false
                        // 在打开弹窗时立即设置高度
                        nextTick(() => {
                            if (waveFormAnalysisRef.value) {
                                // waveFormAnalysisRef.value.setHeight(false, 360)
                                waveFormAnalysisRef.value.setHeight(999, 130, 1.6666666)
                            }
                        })
                        await analyseWave(row.id)
                            .then(res => {
                                row.loading1 = false
                                if (res != undefined) {
                                    boxoList.value = row
                                    // boxoList.value = {
                                    //     ...row,
                                    //     duration: row.persistTime // 将 persistTime 值赋给 duration
                                    // }
                                    boxoList.value.featureAmplitude = (row.amplitude - 0) / 100
                                    // row.evtParamVVaDepth != '-' ? row.evtParamVVaDepth - 0 : null
                                    boxoList.value.systemType = 'YPT'
                                    wp.value = res.data
                                }
                                loading.value = false
                            })
                            .catch(() => {
                                row.loading1 = false
                                loading.value = false
                            })

                        nextTick(() => {
                            waveFormAnalysisRef.value &&
                                waveFormAnalysisRef.value.getWpData(wp.value, boxoList.value, true)
                        })
                    }
                },
                {
                    name: 'edit',
                    text: '暂无波形',
                    type: 'info',
                    icon: 'el-icon-DataLine',
                    render: 'basicButton',
                    disabled: row => {
                        return !!row.wavePath
                    }
                }
            ]
        }
    ],
    beforeSearchFun: () => {},
    loadCallback: () => {}
})
tableStore.table.params.eventType = ''
provide('tableStore', tableStore)
const open = async (time: any) => {
    tableStore.table.params.eventType = ''
    dialogVisible.value = true
    getSimpleLineList()
    tableStore.table.params.lineId = ''
    nextTick(() => {
        tableHeaderRef.value.setInterval(5)
        tableHeaderRef.value.setTimeInterval([time, time])
        tableStore.table.params.searchBeginTime = time
        tableStore.table.params.searchEndTime = time
        tableStore.index()
    })
}

const handleHideCharts = () => {
    isWaveCharts.value = false
    dialogVisible.value = true
}

defineExpose({ open })
</script>
<style lang="scss" scoped></style>
