<template>
    <div class="view">
        <TableHeader datePicker ref="headerRef" v-if="!isWaveCharts" :showReset="false"></TableHeader>
        <Table ref="tableRef" v-if="!isWaveCharts" />
        <waveFormAnalysis
            v-loading="loading"
            v-if="isWaveCharts"
            ref="waveFormAnalysisRef"
            @handleHideCharts="isWaveCharts = false"
            :wp="wp"
        />
    </div>
</template>
<script lang="ts" setup>
import { ref, onMounted, provide, nextTick, defineEmits } from 'vue'
import { getTabsDataByType } from '@/api/cs-device-boot/EquipmentDelivery'
import TableStore from '@/utils/tableStore'
import Table from '@/components/table/index.vue'
import TableHeader from '@/components/table/header/index.vue'
import waveFormAnalysis from './components/waveFormAnalysis.vue'
import { ArrowLeft, Message } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { analyseWave, getFileByEventId } from '@/api/common'
import { getFileZip } from '@/api/cs-harmonic-boot/datatrend'
const tableParams: any = ref({})
const refheader = ref()
const view = ref(true)
const view2 = ref(false)
const loading = ref(false)
const showBoxi = ref(true)
const bxactiveName = ref('ssbx')
const boxoList: any = ref([])
const wp = ref({})
const value = ref(1)
const waveFormAnalysisRef = ref()
const headerRef = ref()

const props = defineProps({
    deviceType: {
        type: String,
        default: '0'
    }
})

const tableStore: any = new TableStore({
    url: '/cs-device-boot/csGroup/deviceDataByType',
    publicHeight: 215,
    method: 'POST',
    column: [
        // { width: '60', type: 'checkbox', fixed: 'left' },
        {
            title: '序号',
            width: 80,
            formatter: (row: any) => {
                return (tableStore.table.params.pageNum - 1) * tableStore.table.params.pageSize + row.rowIndex + 1
            }
        },
        { field: 'startTime', title: '发生时刻', minWidth: 170, sortable: true },
        { field: 'showName', title: '事件描述', minWidth: 170 },
        {
            field: 'phaseType',
            title: '相别',
            minWidth: 100,
            formatter: (row: any) => {
                row.cellValue = row.cellValue ? row.cellValue : '/'
                return row.cellValue
            }
        },
        {
            field: 'persistTime',
            title: '持续时间(s)',
            minWidth: 100,
            formatter: (row: any) => {
                console.log('row.cellValue', row.cellValue)
                row.cellValue = row.cellValue ? row.cellValue.toFixed(2) : '/'
                return row.cellValue
            },
            sortable: true
        },
        {
            field: 'featureAmplitude',
            title: '暂降(聚升)幅值(%)',
            width: 100,
            formatter: (row: any) => {
                //row.cellValue = row.cellValue + '' ? row.cellValue.toFixed(2) : '/'
                row.cellValue = row.cellValue != null ? Number(row.cellValue).toFixed(2) : '/'
                if (String(row.cellValue).split('.')[1] == '00') {
                    row.cellValue = String(row.cellValue).split('.')[0]
                }
                return row.cellValue
            }
        },
        {
            title: '操作',
            width: 180,
            render: 'buttons',
            fixed: 'right',
            buttons: [
                {
                    name: 'edit',
                    title: '波形分析',
                    type: 'primary',
                    icon: 'el-icon-Check',
                    render: 'basicButton',
                    loading: 'loading1',
                    disabled: row => {
                        // && row.evtParamTm < 20
                        return !row.wavePath
                    },
                    click: async row => {
                        row.loading1 = true
                        isWaveCharts.value = true
                        loading.value = true
                        await analyseWave(row.id)
                            .then(res => {
                                row.loading1 = false
                                if (res != undefined) {
                                    boxoList.value = row
                                    boxoList.value.systemType = 'YPT'
                                    boxoList.value.engineeringName = tableParams.value.engineeringName
                                    console.log("🚀 ~ tableParams.value.engineeringName:", tableParams.value.engineeringName)
                                    wp.value = res.data
                                    view.value = false
                                    view2.value = true
                                }
                                loading.value = false
                            })
                            .catch(() => {
                                row.loading1 = false
                                loading.value = false
                            })

                        nextTick(() => {
                            waveFormAnalysisRef.value && waveFormAnalysisRef.value.getWpData(wp.value, boxoList.value)
                            waveFormAnalysisRef.value && waveFormAnalysisRef.value.setHeight(200, 345)
                        })
                    }
                },
                {
                    name: 'edit',
                    text: '暂无波形',
                    type: 'info',
                    icon: 'el-icon-DataLine',
                    render: 'basicButton',
                    disabled: row => {
                        return row.showName != '未知'
                    }
                },
                {
                    name: 'edit',
                    title: '波形下载',
                    type: 'primary',
                    loading: 'loading2',
                    icon: 'el-icon-Check',
                    render: 'basicButton',
                    disabled: row => {
                        // && row.evtParamTm < 20
                        return !row.wavePath
                    },
                    click: row => {
                        getFileZip({ eventId: row.id }).then(res => {
                            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 = row.wavePath.split('/')[2] || '波形文件' // 设置下载的文件名
                            document.body.appendChild(link)
                            link.click() //执行下载
                            document.body.removeChild(link) //释放标签
                        })
                    }
                },
                {
                    name: 'edit',
                    title: '波形补召',
                    type: 'primary',
                    icon: 'el-icon-Check',
                    render: 'basicButton',
                    disabled: row => {
                        return (props.deviceType === '2' && row.wavePath) || row.showName === '未知'
                    },
                    click: row => {
                        getFileByEventId(row.id).then(res => {
                            ElMessage.success(res.message)
                            tableStore.index()
                        })
                    }
                }
            ]
        }
    ],

    beforeSearchFun: () => {
        tableStore.table.params.devId = tableParams.value.devId
        tableStore.table.params.lineId = tableParams.value.lineId
        tableStore.table.params.list = tableParams.value.list
        console.log('🚀 ~ ableParams.value:', tableParams.value)
        tableStore.table.params.type = 3
    },
    loadCallback: () => {}
})
provide('tableStore', tableStore)
const isWaveCharts = ref(false)
//获取请求参数
const getTableParams = (val: any) => {
    tableParams.value = val
    isWaveCharts.value = false
    tableStore.index()
}
//返回
const handleBack = async () => {
    isWaveCharts.value = false
    emit('activeTabsType', '')
    await tableStore.index()
}
defineExpose({ getTableParams })
onMounted(() => {
    tableStore.index()
})
</script>
<style lang="scss" scoped>
.header_btn {
    width: 100%;
    height: 30px;
    display: flex;
    justify-content: flex-end;
    align-items: center;
}
</style>
