<template>
    <el-dialog v-model="dialogVisible" :title= "titleType"  v-bind="dialogBig" >
        <!-- <el-descriptions :column="5" border>
            <el-descriptions-item label="脚本名称">测试</el-descriptions-item>
            <el-descriptions-item label="参照标准名称">GBT 19862</el-descriptions-item>
            <el-descriptions-item label="标准推行年份">2025</el-descriptions-item>
            <el-descriptions-item label="模板类型">模版</el-descriptions-item>
            <el-descriptions-item label="值类型">相对值</el-descriptions-item>
        </el-descriptions> -->
        <!-- tableData?.channelList -->
        <el-tabs type="border-card">
            <el-tab-pane :label="`L${item}`" v-for="item in 3">
                <div class="tabPane">
                    <el-descriptions :column="4" border>
                        <el-descriptions-item label-align="right" label="电压有效值(%)">
                            {{
                                tableData?.channelList[item * 2 - 2].famp == null
                                    ? '/'
                                    : tableData?.channelList[item * 2 - 2].famp
                            }}
                        </el-descriptions-item>
                        <el-descriptions-item label-align="right" label="电压相角(°)">
                            {{
                                tableData?.channelList[item * 2 - 2].fphase == null
                                    ? '/'
                                    : tableData?.channelList[item * 2 - 2].fphase
                            }}
                        </el-descriptions-item>
                        <el-descriptions-item label-align="right" label="电流有效值(%)">
                            {{
                                tableData?.channelList[item * 2 - 1].famp == null
                                    ? '/'
                                    : tableData?.channelList[item * 2 - 1].famp
                            }}
                        </el-descriptions-item>
                        <el-descriptions-item label-align="right" label="电流相角(°)">
                            {{
                                tableData?.channelList[item * 2 - 1].fphase == null
                                    ? '/'
                                    : tableData?.channelList[item * 2 - 1].fphase
                            }}
                        </el-descriptions-item>
                        <el-descriptions-item label-align="right" label="变动频度(次/min)">
                            {{ tableData?.channelList[item * 2 - 2].flickerData.fchagFre || '/' }}
                        </el-descriptions-item>
                        <el-descriptions-item label-align="right" label="变动量(%)">
                            {{ tableData?.channelList[item * 2 - 2].flickerData.fchagValue || '/' }}
                        </el-descriptions-item>
                        <el-descriptions-item label-align="right" label="波类型">
                            {{
                                tableData?.channelList[item * 2 - 2].flickerData.fchagValue == ''
                                    ? '/'
                                    : tableData?.channelList[item * 2 - 2].flickerData.waveFluType
                            }}
                        </el-descriptions-item>
                        <el-descriptions-item label-align="right" label="占空比(%)">
                            {{
                                tableData?.channelList[item * 2 - 2].flickerData.fchagValue == ''
                                    ? '/'
                                    : tableData?.channelList[item * 2 - 2].flickerData.fdutyCycle
                            }}
                        </el-descriptions-item>
                        <el-descriptions-item label-align="right" label="设定幅度(%)">
                            {{ tableData?.channelList[item * 2 - 2].dipData.ftransValue || '/' }}
                        </el-descriptions-item>
                        <el-descriptions-item label-align="right" label="持续时间(s)">
                            {{ tableData?.channelList[item * 2 - 2].dipData.retainTime || '/' }}
                        </el-descriptions-item>
                    </el-descriptions>
                    <!-- <el-descriptions :column="3" title="谐波电压" border class="mt10">
                        <div v-for="k in tableData?.channelList[item * 2 - 2].harmList">
                            <el-descriptions-item label-align="right" label="次数">{{ k.harm }}</el-descriptions-item>
                            <el-descriptions-item label-align="right" label="谐波含有率(%)">{{k.famp}}</el-descriptions-item>
                            <el-descriptions-item label-align="right" label="谐波相角(°)">{{k.fphase}}</el-descriptions-item>
                        </div>
                    </el-descriptions>
                    <el-descriptions :column="3" title="谐波电流" border class="mt10">
                         <div v-for="k in tableData?.channelList[item * 2 - 2].harmList">
                            <el-descriptions-item label-align="right" label="次数">{{ k.harm }}</el-descriptions-item>
                            <el-descriptions-item label-align="right" label="谐波含有率(%)">{{k.famp}}</el-descriptions-item>
                            <el-descriptions-item label-align="right" label="谐波相角(°)">{{k.fphase}}</el-descriptions-item>
                        </div>
                    </el-descriptions> -->
                    <div style="display: flex" class="mt10">
                        <el-tabs type="border-card" style="flex: 1" v-if="harmVIsShow">
                            <el-tab-pane label="谐波电压" >
                                <el-table
                                    :data="tableData?.channelList[item * 2 - 2].harmList"
                                    border
                                    size="small"
                                    :header-cell-style="{
                                        textAlign: 'center',
                                        backgroundColor: '#003078',
                                        color: '#fff'
                                    }"
                                    stripe
                                    :cell-style="{ textAlign: 'center' }"
                                    height="250px"
                                >
                                    <el-table-column prop="harm" label="次数" />
                                    <el-table-column prop="famp" label="谐波含有率(%)" />
                                    <el-table-column prop="fphase" label="谐波相角(°)" />
                                </el-table>
                            </el-tab-pane>
                        </el-tabs>
                        <el-tabs type="border-card" class="ml10" style="flex: 1" v-if="harmAIsShow"> 
                            <el-tab-pane label="谐波电流">
                                <el-table
                                    :data="tableData?.channelList[item * 2 - 1].harmList"
                                    border
                                    size="small"
                                    :header-cell-style="{
                                        textAlign: 'center',
                                        backgroundColor: '#003078',
                                        color: '#fff'
                                    }"
                                    stripe
                                    :cell-style="{ textAlign: 'center' }"
                                    height="250px"
                                >
                                    <el-table-column prop="harm" label="次数" />
                                    <el-table-column prop="famp" label="谐波含有率(%)" />
                                    <el-table-column prop="fphase" label="谐波相角(°)" />
                                </el-table>
                            </el-tab-pane>
                        </el-tabs>
                    </div>
                    <div style="display: flex" class="mt10">
                        <el-tabs type="border-card" style="flex: 1" v-if="iHarmVIsShow">
                            <el-tab-pane label="间谐波电压">
                                <el-table
                                    :data="tableData?.channelList[item * 2 - 2].inharmList"
                                    border
                                    size="small"
                                    :header-cell-style="{
                                        textAlign: 'center',
                                        backgroundColor: '#003078',
                                        color: '#fff'
                                    }"
                                    stripe
                                    :cell-style="{ textAlign: 'center' }"
                                    height="250px"
                                >
                                    <el-table-column prop="inharm" label="次数" />
                                    <el-table-column prop="famp" label="间谐波含有率(%)" />
                                    <el-table-column prop="fphase" label="间谐波相角(°)" />
                                </el-table>
                            </el-tab-pane>
                        </el-tabs>
                        <el-tabs type="border-card" class="ml10" style="flex: 1" v-if="iHarmAIsShow">
                            <el-tab-pane label="间谐波电流">
                                <el-table
                                    :data="tableData?.channelList[item * 2 - 1].inharmList"
                                    border
                                    size="small"
                                    :header-cell-style="{
                                        textAlign: 'center',
                                        backgroundColor: '#003078',
                                        color: '#fff'
                                    }"
                                    stripe
                                    :cell-style="{ textAlign: 'center' }"
                                    height="250px"
                                >
                                    <el-table-column prop="inharm" label="次数" />
                                    <el-table-column prop="famp" label="间谐波含有率(%)" />
                                    <el-table-column prop="fphase" label="间谐波相角(°)" />
                                </el-table>
                            </el-tab-pane>
                        </el-tabs>
                    </div>
                </div>
            </el-tab-pane>

            <el-tab-pane label="参考设定值列表">
                <div class="tabPane">
                <el-table
                :data="setValue_TableData"
                :header-cell-style="{
                    textAlign: 'center',
                    backgroundColor: '#003078',
                    color: '#fff'
                }"
                stripe
                :cell-style="{ textAlign: 'center' }"
                height="450px"
                >
            <el-table-column type="index" label="序号" width="60" />
            <el-table-column prop="pname" label="参考设定值类型" />
            <el-table-column prop="name" label="参考设定值子类型" width="250">
                <template #default="{ row }">{{ row.harmNum ? `(${row.harmNum}次)` : '' }} {{ row.name }}</template>
            </el-table-column>
            <el-table-column prop="dataType" label="值类型">
                <template #default="{ row }">
                    <el-select v-model="row.dataType" v-if="!row.show">
                        <el-option v-for="item in typeList" :key="item.value" :label="item.label" :value="item.value" />
                    </el-select>
                    <span v-else>
                        {{ typeList.find(item => item.value == row.dataType)?.label || row.dataType }}
                    </span>
                </template>
            </el-table-column>
            <el-table-column prop="phase" label="相别" />
            <el-table-column prop="value" label="参考设定值">
                <template #default="{ row }">
                    <span v-if="row.show">{{ row.value }}</span>
                    <el-input type="number" v-else v-model="row.value" placeholder="请输入值" />
                </template>
            </el-table-column>
            <el-table-column prop="value" label="参与误差比较">
                <template #default="{ row }">
                    {{ row.errorFlag == 0 ? '否' : '是' }}
                </template>
            </el-table-column>
                </el-table>
                </div>
            </el-tab-pane>
        </el-tabs>
    </el-dialog>
</template>
<script setup lang="ts">
import { ref, reactive } from 'vue'
import { dialogBig } from '@/utils/elementBind'
import { getDictTreeByCode } from '@/api/system/dictionary/dictTree'
import { scriptDtlsCheckDataList } from '@/api/device/testScript/index'
import { Dictionary } from 'lodash'
const dialogVisible = ref(false)
const titleType = ref('')
const tableData: any = ref({})
const setValue_TableData: any = ref([])
const copyRowList = ref({})
const harmVIsShow = ref(false)
const harmAIsShow = ref(false)
const iHarmVIsShow = ref(false)
const iHarmAIsShow = ref(false)
const props = defineProps({
    activeName: {
        type: String,
        required: true
    },

    formContent: {
        type: [Object, Array],
        required: true
    },

})
// 表格配置项
const typeList = [
    {
        label: '实时',
        value: 'real'
    },
    {
        label: 'CP95值',
        value: 'cp95'
    },
    {
        label: '平均值',
        value: 'avg'
    },
    {
        label: '最小值',
        value: 'min'
    },
    {
        label: '最大值',
        value: 'max'
    }
]

const tabVisibilityMap: { [key: string]: { harmVIsShow: boolean, harmAIsShow: boolean, iHarmVIsShow: boolean, iHarmAIsShow: boolean } } = {
    '谐波电压': { harmVIsShow: true, harmAIsShow: false, iHarmVIsShow: false, iHarmAIsShow: false },
    '谐波电流': { harmVIsShow: false, harmAIsShow: true, iHarmVIsShow: false, iHarmAIsShow: false },
    '间谐波电压': { harmVIsShow: false, harmAIsShow: false, iHarmVIsShow: true, iHarmAIsShow: false },
    '间谐波电流': { harmVIsShow: false, harmAIsShow: false, iHarmVIsShow: false, iHarmAIsShow: true },
    '谐波有功功率': { harmVIsShow: true, harmAIsShow: true, iHarmVIsShow: false, iHarmAIsShow: false },
};
const open = async (row: any,communicationList:any,parentTabName:string,childrenTabName:string) => {
    
    //对应表格显示隐藏
    const visibilitySettings = tabVisibilityMap[parentTabName] || { harmVIsShow: false, harmAIsShow: false, iHarmVIsShow: false, iHarmAIsShow: false };
    harmVIsShow.value = visibilitySettings.harmVIsShow;
    harmAIsShow.value = visibilitySettings.harmAIsShow;
    iHarmVIsShow.value = visibilitySettings.iHarmVIsShow;
    iHarmAIsShow.value = visibilitySettings.iHarmAIsShow;
        
    titleType.value = parentTabName + '_' + childrenTabName+ '_详情'
    //console.log('🚀 ~ open ~ row:', row)
    tableData.value = row  
    copyRowList.value = JSON.parse(JSON.stringify(row))


    let treeData: any = []
    await getDictTreeByCode({
        name: '',
        id: '',
        pid: '',
        pids: '',
        code: 'Script_Error',
        sort: 0
    }).then((res: any) => {
        treeData = res.data[0].children
    })
    
    let checkDataList: any = []
    await communicationList.forEach((item: any) => {
        item.children.forEach((k: any) => {
            if (k.enable != 0 || k.errorFlag != 0) {
               
                checkDataList.push({
                    pid: k.pid,
                    valueType: k.id,
                    dataType: k.dataType,
                    enable: k.enable,
                    errorFlag: k.errorFlag
                })
            }
        })
    })

    let form = handleHarmData(JSON.parse(JSON.stringify(row)))
    let retryCompute = isEqual(form, copyRowList)

    await scriptDtlsCheckDataList({
        ...form,
        scriptId: props.formContent?.id,
        scriptType: props.activeName,
        checkDataList: checkDataList,
        retryCompute: retryCompute
    }).then((res: any) => {
        res.data.forEach((item: any) => {
            let pList = treeData.filter((i: any) => i.id == item.pid)[0]
            item.pname = pList.name
            item.name = pList.children.filter((i: any) => i.id == item.valueType)[0].name
            item.show = true
        })
        setValue_TableData.value = res.data
    })


  
    dialogVisible.value = true
}




// 处理多余数据
const handleHarmData = (row: any) => {
    row.channelList.forEach((channel: any) => {
        // 筛选出 famp 和 fphase 不同时为 0 的对象
        channel.harmList = channel.harmList.filter((item: any) => item.famp != 0 || item.fphase != 0)
        channel.inharmList = channel.inharmList.filter(
            (item: any) => item.inharm !== '' || item.famp !== 0 || item.fphase !== 0
        )
    })
    return row
}
// 判断数据是否变化
const isEqual = (obj1: any, obj2: any) => {
    // 如果两个对象是同一个引用，直接返回 true
    if (obj1 == obj2) return true
    // 如果其中一个是 null 或者不是对象，返回 false
    if (obj1 === null || typeof obj1 !== 'object' || obj2 === null || typeof obj2 !== 'object') {
        return false
    }
    // 获取两个对象的键
    const keys1 = Object.keys(obj1)
    const keys2 = Object.keys(obj2)
    // 如果键的数量不同，返回 false
    if (keys1.length !== keys2.length) return false
    // 遍历所有键，递归比较值
    for (const key of keys1) {
        if (!keys2.includes(key) || !isEqual(obj1[key], obj2[key])) {
            return false
        }
    }

    return true
}


defineExpose({ open })
</script>
<style lang="scss" scoped>
:deep(.el-descriptions__cell) {
    width: 12.5%;
}
:deep(.tabPane) {
    height: 450px;
    overflow-y: auto;
}
</style>
