<template>
    <div>
        <!-- 指标越限详情  -->
        <el-dialog draggable :title="title"  v-model="dialogVisible" append-to-body width="1200">
            <TableHeader datePicker showExport :showReset="false" ref="tableHeaderRef">
                <template v-slot:select>
                    <!-- <el-form-item label="监测点">
                        <el-select
                            v-model="tableStore.table.params.lineId"
                            placeholder="请选择监测点"
                            style="width: 150px"
                            filterable
                        >
                            <el-option
                                v-for="item in options"
                                :key="item.lineId"
                                :label="item.lineName"
                                :value="item.lineId"
                            />
                        </el-select>
                    </el-form-item> -->
                </template>
            </TableHeader>
            <Table ref="tableRef" @cell-click="cellClickEvent" isGroup :height="height"></Table>
        </el-dialog>
        <!-- 谐波电流、谐波电压占有率 -->
        <HarmonicRatio‌ ref="harmonicRatio‌Ref" v-if="dialogFlag" @close="onHarmonicRatioClose" />
    </div>
</template>
<script setup lang="ts">
import { ref, provide,nextTick } 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 HarmonicRatio‌ from '@/components/cockpit/overLimitStatistics/components/harmonicRatio‌.vue'
import { cslineList } from '@/api/harmonic-boot/cockpit/cockpit'

const dialogVisible: any = ref(false)
const harmonicRatio‌Ref: any = ref(null)
const title = ref('指标越限详情')
const options = ref()
const height = mainHeight(0, 2).height as any
const tableHeaderRef = ref()
const dialogFlag = ref(false)
const loop50 = (key: string) => {
    let list: any[] = []
    for (let i = 2; i < 26; i++) {
        list.push({
            title: i + '次',
            field: key + i + 'Overtime',
            width: '60',
            render: 'customTemplate',
            customTemplate: (row: any) => {
                return `<span style='cursor: pointer;text-decoration: underline;'>${row[key + i + 'Overtime']}</span>`
            }
        })
    }
    return list
}
const tableStore: any = new TableStore({
    url: '/cs-harmonic-boot/totalLimitStatistics/details',
    method: 'POST',
    publicHeight: 30,
    showPage: false,
    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: 'time',
            width: '120',
            sortable: true
        },
        {
            title: '名称',
            field: 'lineName',
            width: '120'
        },
        {
            title: '长时闪变越限(%)',
            field: 'flickerOvertime',
            width: '90',
            render: 'customTemplate',
            customTemplate: (row: any) => {
                return `<span style='cursor: pointer;text-decoration: underline;'>${row.flickerOvertime}</span>`
            }
        },
         {
            title: '电压偏差越限(%)',
            field: 'voltageDevOvertime',
            width: '100',
            render: 'customTemplate',
            customTemplate: (row: any) => {
                return `<span style='cursor: pointer;text-decoration: underline;'>${row.uaberranceOvertime}</span>`
            }
        },
        {
            title: '三相不平衡度越限(%)',
            field: 'ubalanceOvertime',
            width: '100',
            render: 'customTemplate',
            customTemplate: (row: any) => {
                return `<span style='cursor: pointer;text-decoration: underline;'>${row.ubalanceOvertime}</span>`
            }
        },
        {
            title: '谐波电压越限(%)',
            children: loop50('uharm')
        },
        {
            title: '谐波电流越限(%)',
            children: loop50('iharm')
        },


        // {
        //     title: '频率偏差越限(%)',
        //     field: 'freqDevOvertime',
        //     width: '100',
        //     render: 'customTemplate',
        //     customTemplate: (row: any) => {
        //         return `<span style='cursor: pointer;text-decoration: underline;'>${row.freqDevOvertime}</span>`
        //     }
        // }
    ],
     beforeSearchFun: () => {
    },
    loadCallback: () => {
    }
})


provide('tableStore', tableStore)
tableStore.table.params.sortBy = ''
tableStore.table.params.orderBy = ''
const open = async (row: any,searchBeginTime:any,searchEndTime:any,data: any) => {
    dialogVisible.value = true
    // initCSlineList()
       title.value = row.lineName + '_指标越限详情'
     options.value = data
    tableStore.table.params.lineId = row.lineId

    nextTick(() => {
        tableHeaderRef.value.setTimeInterval([searchBeginTime, searchEndTime])
        tableStore.table.params.searchBeginTime =searchBeginTime
        tableStore.table.params.searchEndTime = searchEndTime
        tableStore.index()
    })

}

// 点击行
const cellClickEvent = ({ row, column }: any) => {
    if (column.field != 'lineName' && column.field != 'name' && column.field != 'time') {
        dialogFlag.value = true
        dialogVisible.value = false
        nextTick(() => {
            harmonicRatio‌Ref.value.openDialog(row,column.field,column.title.replace(/次/g, ""))
        })

    }
}

// 谐波弹窗关闭时的回调
const onHarmonicRatioClose = () => {
     dialogFlag.value = false
    // 重新打开指标越限详情弹窗
    nextTick(() => {
        dialogVisible.value = true
    })
}

const initCSlineList = async () => {
    const res = await cslineList({})
    options.value = res.data
}



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