<template>
    <div>
        <el-collapse v-model="activeNames">
            <el-collapse v-model="activeNames">
                <el-collapse-item title="用户参数维护" :name="1">
                    <el-descriptions :column="4" border>
                        <el-descriptions-item label="所在地市">{{ form.deptName }}</el-descriptions-item>
                    <el-descriptions-item label="用户名称">{{ form.assessName }}</el-descriptions-item>
                    <el-descriptions-item label="用户协议容量(MVA)">{{ form.agreedCapacity }}</el-descriptions-item>
                    <el-descriptions-item label="是否包含电容器">{{ form.withCapacitor }}</el-descriptions-item>
                    <el-descriptions-item :label=windfarmCapacityLabel>{{ form.windfarmCapacity }}</el-descriptions-item>
                    </el-descriptions>
                </el-collapse-item>
                <el-collapse-item title="系统参数维护" :name="2">
                    <el-descriptions :column="4" border>
                        <el-descriptions-item label="接入变电站">{{ form.powerstationName }}</el-descriptions-item>
                        <el-descriptions-item label="变电站电压等级">{{ form.powerstationScale }}</el-descriptions-item>
                        <el-descriptions-item label="接入母线">{{ form.busName }}</el-descriptions-item>
                        <el-descriptions-item label="母线电压等级">{{ form.busScale }}</el-descriptions-item>
                        <el-descriptions-item label="供电设备容量(MVA)">{{ form.powersupplyCapacity }}</el-descriptions-item>
                        <el-descriptions-item label="最小短路容量(MVA)">{{ form.minshortcircuitCapacity }}</el-descriptions-item>
                        <el-descriptions-item label="PCC点名称">{{ form.lineName }}</el-descriptions-item>
                        <el-descriptions-item label="PCC点电网电阻">{{ form.lineGridreactance }}</el-descriptions-item>
                        <el-descriptions-item label="长度">{{ form.lineLen }}</el-descriptions-item>
                        <el-descriptions-item label="回路数">{{ form.lineNum }}</el-descriptions-item>
                        <el-descriptions-item label="导线类型">{{ form.linetypeId }}</el-descriptions-item>
                    </el-descriptions>
                </el-collapse-item>
                <el-collapse-item :title="item.name" v-for="(item, index) in column" :key="index" :name="index + 3">
                    <div style="height: 200px">
                        <vxe-table height="auto" :data="form[item.key]" v-bind="defaultAttribute" v-loading="item.loading">
                            <vxe-column type="seq" title="序号" width="80px"></vxe-column>
                            <vxe-column
                                v-for="(k, index) in item.child"
                                :key="index"
                                :field="k.field"
                                :title="k.title"
                            ></vxe-column>
                        </vxe-table>
                    </div>
                </el-collapse-item>
            </el-collapse>
        </el-collapse>
    </div>
</template>
<script setup lang="ts">
import { ref, reactive } from 'vue'
import { defaultAttribute } from '@/components/table/defaultAttribute'
import {    userGetInfo ,
            cableList, 
            assessTransformerParamList,
            transformerList,
            capacitorList,
            windPowerList,
            loadParamList,
            loadList,
            shockList,
            characteristicList
} from '@/api/advance-boot/assess'
import { useDictData } from '@/stores/dictData'

const dictData = useDictData()
// 定义 props
const props = defineProps({
  nodeId: {
    type: [String, Number],
    default: null
  }
})

const activeNames = ref([1, 2, 3, 4, 5, 6, 7])
const form: any = ref({
    deptName: '',              // 所在地市
    assessName: '',          // 用户名称
    agreedCapacity: '',    // 用户协议容量(MVA)
    withCapacitor: '',      // 是否包含电容器
    windFarms: '',         // 风电场装机容量
    powerstationName: '',        // 接入变电站
    powerstationScale: '', // 变电站电压等级
    busName: '',               // 接入母线
    busScale: '',        // 母线电压等级
    powersupplyCapacity: '', // 供电设备容量(MVA)
    minshortcircuitCapacity: '', // 最小短路容量(MVA)
    lineName: '',           // PCC点名称
    lineGridreactance: '',     // PCC点电网电阻
    lineLen: '',            // 长度
    lineNum: '',          // 回路数
    linetypeId: '',          // 导线类型
    transformer: [], //变压器
    capacitor: [], //电容器
    windPower: [], //风电
    load: [], //用户负荷
    impact: [] //冲击性负荷
})
const column: any = ref([
    {
        loading: false,
        name: '变压器参数维护',
        key: 'transformer',
        child: [
            {
                title: '用户变压器名称',
                field: 'transName'
            },
            {
                title: '变压器型号',
                field: 'transtypeId'
            },
            {
                title: '变压器台数',
                field: 'transNum'
            }
        ]
    },
    {   
        loading: false,
        name: '电容器组参数维护',
        key: 'capacitor',
        child: [
            {
                title: '用户电容器名称',
                field: 'capacitorName'
            },
            {
                title: '电容器组数量',
                field: 'capacitorNum'
            },
            {
                title: '单组容量(kVar)',
                field: 'capacitorCapacity'
            },
            {
                title: '电抗率(%)',
                field: 'capacitorReactance'
            },
            {
                title: '电压等级',
                field: 'capacitorScale'
            }
        ]
    },
    {
        loading: false,
        name: '风电参数',
        key: 'windPower',
        child: [
            {
                title: '风机名称',
                field: 'windgeneratorName'
            },
            {
                title: '风机闪变系数',
                field: 'flickerCoeff'
            },
            {
                title: '风机额定视在功率',
                field: 's'
            }
        ]
    },
    {
        loading: false,
        name: '用户负荷',
        key: 'load',
        child: [
            {
                title: '配电窒名称',
                field: 'loadName'
            },
            {
                title: '进线电压',
                field: 'linevoltageScale'
            },
            {
                title: '配变容量(kVA)',
                field: 'transhighCapacity'
            },
            {
                title: '配电变压器阻抗(%)',
                field: 'transImpedance'
            },
            {
                title: '谐波源特性',
                field: 'harmId'
            },
            {
                title: '配变低压侧电压',
                field: 'translowScale'
            },
            {
                title: '非线性负荷功率(kW)',
                field: 'nonlinearloadPower'
            },
            {
                title: '功率因素',
                field: 'powerFactor'
            }
        ]
    },
    {
        loading: false,
        name: '冲击性负荷',
        key: 'impact',
        child: [
            {
                title: '冲击负荷类型',
                field: 'inpactloadtypeId'
            },
             {
                title: '启动方式',
                field: 'inpactloadStartup',
            },
            {
                title: '数量',
                field: 'inpactloadNum'
            },
           
            {
                title: '启动容量倍数',
                field: 'inpactloadPower'
            },
            {
                title: '高压侧电压',
                field: 'inpactloadhighScale'
            },
            {
                title: '有无隔离变',
                field: 'isIsolationtrans'
            },
            {
                title: '隔离变阻抗(%)',
                field: 'isolationtransImpedance'
            },
            {
                title: '低压侧电压',
                field: 'inpactloadlowScale'
            },
            {
                title: '负荷频度选择',
                field: 'inpactloadFreq'
            },
            {
                title: '闪变叠加系数',
                field: 'flickerCoeff'
            }
        ]
    }
])
const voltageleveloption = dictData.getBasicData('Dev_Voltage_Stand')

// 创建计算属性来动态生成标签文本
const windfarmCapacityLabel = computed(() => {
  if (form.value.isWindfarm === '01') {
    return '风电场装机容量'
  } else {
    return '用户有功功率'
  }
})



const filteredColumn = computed(() => {
    // const windfarmValue = form.value.isWindfarm;
    // if (windfarmValue === '01') {
    //     // 如果是风电场，显示除冲击负荷外的所有项
    //     return column.value.filter((item: any) => item.key !== 'impact')
    // } else {
    //     // 如果不是风电场，显示除风电参数外的所有项
    //     return column.value.filter((item: any) => item.key !== 'windPower')
    // }
})

const emit = defineEmits(['data-loaded'])

// 监听 nodeId 变化并执行相应操作
watch(() => props.nodeId, async (newId) => {
  // 先清空表单数据
  form.value = {
    deptName: '',              // 所在地市
    assessName: '',            // 用户名称
    agreedCapacity: '',        // 用户协议容量(MVA)
    withCapacitor: '',          // 是否包含电容器
    windFarms: '',             // 风电场装机容量
    powerstationName: '',      // 接入变电站
    powerstationScale: '',     // 变电站电压等级
    busName: '',               // 接入母线
    busScale: '',              // 母线电压等级
    powersupplyCapacity: '',   // 供电设备容量(MVA)
    minshortcircuitCapacity: '', // 最小短路容量(MVA)
    lineName: '',              // PCC点名称
    lineGridreactance: '',     // PCC点电网电阻
    lineLen: '',               // 长度
    lineNum: '',               // 回路数
    linetypeId: '',            // 导线类型
    transformer: [],           // 变压器
    capacitor: [],             // 电容器
    windPower: [],             // 风电
    load: [],                  // 用户负荷
    impact: []                 // 冲击性负荷
  };
  if (newId) {
    userGetInfo({ assessId: newId }).then(async (res: any) => {
        form.value = res.data
        // 转换 withCapacitor 字段
        if (form.value.withCapacitor === '00') {
            form.value.withCapacitor = '否'
        } else if (form.value.withCapacitor === '01') {
            form.value.withCapacitor = '是'
        }
        //变电站电压等级
        const voltageLevel = voltageleveloption.find(item => item.id === form.value.powerstationScale);
        form.value.powerstationScale= voltageLevel?.name
        //母线电压等级
        const busScale = voltageleveloption.find(item => item.id === form.value.busScale);
        form.value.busScale = busScale?.name
        //导线类型
        await cableList({}).then(res => {
            form.value.linetypeId = res.data.find((item: any) => item.linetypeId === form.value.linetypeId)?.linetypeName
        })
  
        // 变压器参数维护
        await assessTransformerParamList({ assessId: props.nodeId}).then(res => {
            form.value.transformer = res.data
            // 获取变压器类型字典数据
            transformerList({}).then((res: any) => {
                // 使用 map 方法创建新数组
                form.value.transformer = form.value.transformer.map((transformerItem: any) => {
                    const found = res.data.find((item: any) => item.transtypeId == transformerItem.transtypeId)
                    if (found) {
                        return {
                            ...transformerItem,
                            transtypeId: found.transtypeName
                        }
                    }
                    return transformerItem
                })
            })
        })
   
        // 电容器参数维护
        await capacitorList({ assessId: props.nodeId }).then(res => {
            form.value.capacitor = res.data
        
            form.value.capacitor = form.value.capacitor.map((capacitorItem: any) => {
            const voltageLevel = voltageleveloption.find(item => item.id === capacitorItem.capacitorScale);
            if (voltageLevel) {
                return {
                    ...capacitorItem,
                    capacitorScale: voltageLevel.name
                }
            }
            return capacitorItem
            })
        })
  
        // 风电参数
        await windPowerList({ assessId: props.nodeId }).then(res => {
            form.value.windPower = res.data
           
        })
   
        // 用户负荷参数
        await loadParamList({ assessId: props.nodeId }).then(res => {
            form.value.load = res.data

            // 谐波源特特性
            characteristicList({}).then((res: any) => {
                form.value.load = form.value.load.map((transformerItem: any) => {
                    const found = res.data.find((item: any) => item.harmId == transformerItem.harmId)
                            if (found) {
                                return {
                                    ...transformerItem,
                                    harmId: found.harmName
                                }
                            }
                            return transformerItem           
                })            
            })

            form.value.load = form.value.load.map((loadItem: any) => {
                // 处理进线电压
                const lineVoltage = voltageleveloption.find(item => item.id === loadItem.linevoltageScale);
                // 处理配变低压侧电压
                const transLowVoltage = voltageleveloption.find(item => item.id === loadItem.translowScale)
                return {
                    ...loadItem,
                    linevoltageScale: lineVoltage?.name || loadItem.linevoltageScale,
                    translowScale: transLowVoltage?.name || loadItem.translowScale
                }
            })
        })
 
        // 冲击性负荷
        await shockList({ assessId: props.nodeId }).then(async (res: { data: never[] }) => {
            form.value.impact = res.data
            // 冲击负荷类型
            const loadRes = await loadList({})
             // 处理冲击负荷类型名称
            form.value.impact = form.value.impact.map((impactItem: any) => {
                const found = loadRes.data.find((item: any) => item.inpactloadtypeId == impactItem.inpactloadtypeId)
         
                if (found) {
                    return {
                        ...impactItem,
                        inpactloadtypeId: found.inpactloadtypeName,
                        inpactloadStartup: found.inpactloadStartup
                    }
                }
                return impactItem
            })
            
            form.value.impact = form.value.impact.map((loadItem: any) => {
                // 处理高压侧电压
                const HeightVoltage = voltageleveloption.find(item => item.id === loadItem.inpactloadhighScale);
                // 处理低压侧电压
                const LowVoltage = voltageleveloption.find(item => item.id === loadItem.inpactloadlowScale)
                return {
                    ...loadItem,
                    inpactloadhighScale: HeightVoltage?.name || loadItem.inpactloadhighScale,
                    inpactloadlowScale: LowVoltage?.name || loadItem.inpactloadlowScale
                }
            })

            
        })
        }

       
                
        )
         
  }

  // 等待 DOM 更新后延迟触发
  await nextTick()
  setTimeout(() => {
    emit('data-loaded')
  }, 1000) // 3秒延迟
 
}, { immediate: true })


</script>
<style lang="scss" scoped>
:deep(.el-collapse-item__header) {
    padding-left: 20px;
    font-size: 16px !important;
    &::before {
        content: '' !important; /* 添加星号 */
  
  
    }
}
:deep(.el-descriptions__cell) {
    width: 12.5%;
}
</style>
