<template>
    <el-collapse-item :title="item.name"  v-for="(item, index) in column" :key="index" :name="index + 3" >
        <div class="mb10">
            <el-button 
                type="primary" 
                size="small" 
                icon="el-icon-Plus" 
                @click="addFform(item)"
                :disabled="item.key === 'capacitorList' && props.isWithCapacitor === '00' || props.disabled"
            >
            新增</el-button>
            <el-button 
                type="primary" 
                size="small" 
                icon="el-icon-Delete" 
                @click="deleteFform(item, index)"
              :disabled="item.key === 'capacitorList' && props.isWithCapacitor === '00' || props.disabled"
            >
                删除
            </el-button>
        </div>
        <div style="height: 250px">
            <vxe-table
                height="auto"
                ref="xTableRef"
                :data="dataList[item.key]"
                v-bind="defaultAttribute"
                v-loading="item.loading"
            >
                <vxe-column type="checkbox" width="60"></vxe-column>
                <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"
                    :formatter="formatter"
                   
                ></vxe-column>
                <vxe-column title="操作" width="150px">
                    <template #default="{ row }">
                        <el-button 
                        type="text" 
                        @click="edit(item, row)"
                        :disabled="item.key === 'capacitorList' && props.isWithCapacitor === '00' || props.disabled"
                        >
                        编辑</el-button>
                    </template>
                </vxe-column>
            </vxe-table>
        </div>
    </el-collapse-item>
    <!-- 新增 -->
    <addform ref="addformRef" @init="info" />
</template>
<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import addform from './addform.vue'
import { useDictData } from '@/stores/dictData'
import {
    assessTransformerParamList,
    transformerList,
    characteristicList,
    assessTransformerParamDelete,
    capacitorList,
    capacitorDelete,
    windPowerList,
    windPowerDelete,
    loadParamList,
    loadParamDelete,
    shockList,
    shockDelete,
    loadList
} from '@/api/advance-boot/assess'
import { defaultAttribute } from '@/components/table/defaultAttribute'
import { ElMessage, ElMessageBox, ElDatePicker } from 'element-plus'
const props = defineProps({
    assessId: { 
        type: String, 
        required: true 
    },
    isWindfarm: {
        type: String,
        default: ''
    },
    isWithCapacitor:{
        type: String,
        default: ''
    },
    disabled: { // 定义 disabled prop
        type: Boolean,
        required: false,
        default: false
    }
})
const dictData = useDictData()
//字典获取电压等级
const voltageleveloption = dictData.getBasicData('Dev_Voltage_Stand')
const transformer: any = ref([])
const characteristic: any = ref([])
const shockRef: any = ref([])
const addformRef = ref()
const xTableRef = ref()

const column: any = ref([
    {
        loading: false,

        name: '变压器参数维护',
        key: 'transformerList',
        child: [
            {
               
                title: '用户变压器名称',
                field: 'transName'
            },
            {
                title: '变压器型号',
                field: 'transtypeId'
            },
            {
                title: '变压器台数',
                field: 'transNum'
            }
        ]
    },
    {
        loading: false,
        name: '电容器组参数维护',
        key: 'capacitorList',
        child: [
            {
                title: '用户电容器名称',
                field: 'capacitorName'
            },
            {
                title: '电容器组数量',
                field: 'capacitorNum'
            },
            {
                title: '单组容量(kVar)',
                field: 'capacitorCapacity'
            },
            {
                title: '电抗率(%)',
                field: 'capacitorReactance'
            },
            {
                title: '电压等级',
                field: 'capacitorScale'
            }
        ]
    },
    {
        loading: false,
        name: '风电参数',

        key: 'windPowerList',
        child: [
            {
                title: '风机名称',
                field: 'windgeneratorName'
            },
            {
                title: '闪变系数',
                field: 'flickerCoeff'
            },
            {
                title: '额定视在功率(kVA)',
                field: 's'
            }
        ]
    },
    {
        loading: false,
        name: '用户负荷',

        key: 'loadList',
        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: 'impactList',
        child: [
            {
                title: '冲击负荷类型',
                field: 'inpactloadtypeId'
            },
            {
                title: '启动方式',
                field: 'inpactloadStartup',
    
            },

            {
                title: '数量',
                field: 'inpactloadNum'
            },
            {
                title: '容量',
                field: 'inpactloadPower'
            },

            {
                title: '是否隔离变',
                field: 'isIsolationtrans'
            },
            {
                title: '隔离变阻抗(%)',
                field: 'isolationtransImpedance'
            },
            {
                title: '高压侧电压',
                field: 'inpactloadhighScale'
            },
            {
                title: '低压侧电压',
                field: 'inpactloadlowScale'
            },
            {
                title: '负荷频度',
                field: 'inpactloadFreq',
                
            },
            {
                title: '闪变叠加系数',
                field: 'flickerCoeff'
            }
        ]
    }
])

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

const dataList = ref({
    transformerList: [], //变压器参数维护
    capacitorList: [], //电容器
    windPowerList: [], //风电
    loadList: [], //用户负荷
    impactList: [] //冲击性负荷
})
// 新增
const addFform = (item: any) => {

    if (item.key === 'transformerList' && dataList.value.transformerList.length > 0) {
        ElMessage.warning('变压器参数已存在，不允许重复新增！');
        return;
    }
    if (item.key === 'capacitorList' && dataList.value.capacitorList.length > 0) {
        ElMessage.warning('电容器组参数已存在，不允许重复新增！');
        return;
    }

    addformRef.value.open({
        title: '新增',
        item: item,
        assessId: props.assessId
    })
}
// 删除
const deleteFform = (item: any, i: any) => {
    if (xTableRef.value[i].getCheckboxRecords().length == 0) {
        return ElMessage({
            type: 'warning',
            message: '请选择要删除的数据'
        })
    }
    ElMessageBox.confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    }).then(async () => {
        if (i == 0) {
            await assessTransformerParamDelete(xTableRef.value[i].getCheckboxRecords().map(item => item.transId)).then(
                res => {
                    ElMessage({
                        type: 'success',
                        message: '删除成功!'
                    })
                }
            )
        } else if (i == 1) {
            await capacitorDelete(xTableRef.value[i].getCheckboxRecords().map(item => item.capacitorId)).then(res => {
                ElMessage({
                    type: 'success',
                    message: '删除成功!'
                })
            })
        } else if (i == 2) {
            await windPowerDelete(xTableRef.value[i].getCheckboxRecords().map(item => item.windgeneratorId)).then(
                res => {
                    ElMessage({
                        type: 'success',
                        message: '删除成功!'
                    })
                }
            )
        } else if (i == 3) {
            await loadParamDelete(xTableRef.value[i].getCheckboxRecords().map(item => item.loadId)).then(res => {
                ElMessage({
                    type: 'success',
                    message: '删除成功!'
                })
            })
        } else if (i == 4) {
            await shockDelete(xTableRef.value[i].getCheckboxRecords().map(item => item.inpactloadId)).then(res => {
                ElMessage({
                    type: 'success',
                    message: '删除成功!'
                })
            })
        }
        info(i)
    })
}
// 编辑
const edit = (item: any, row: any) => {
    addformRef.value.open({
        title: '编辑',
        item: item,
        row: row
    })
}

const info = async (i: number) => {
    //column.value[i].loading = true
    if (i == 0) {
        // 变压器参数维护
        await assessTransformerParamList({ assessId: props.assessId }).then(res => {
            dataList.value.transformerList = res.data
        })
    } else if (i == 1) {
        // 电容器参数维护
        await capacitorList({ assessId: props.assessId }).then(res => {
            dataList.value.capacitorList = res.data
        })
    } else if (i == 2) {
        // 风电参数
        await windPowerList({ assessId: props.assessId }).then(res => {
            dataList.value.windPowerList = res.data
        })
    } else if (i == 3) {
        // 用户负荷参数
        await loadParamList({ assessId: props.assessId }).then(res => {
            dataList.value.loadList = res.data
        })
    } else if (i == 4) {
        // 冲击性负荷
        await shockList({ assessId: props.assessId }).then((res: { data: never[] }) => {
            dataList.value.impactList = res.data
        })
    }
    column.value[i].loading = false
    
}

const typeId = ref(null)
// 过滤数据
const formatter = (row: any) => {
    // 检查数据是否已加载完成
    if (!row.cellValue && row.cellValue !== 0 && row.column.field !='inpactloadStartup') {
        return ''; // 数据未加载时返回空字符串
    }

    if (row.column.field == 'transtypeId') {
        if (row.cellValue) {
            const found = transformer.value.find((item: any) => item.transtypeId == row.cellValue)
            return found?.transtypeName || ''
        } else {
            return '' // 或者返回默认值
        }
    }else if(row.column.field =='inpactloadtypeId'){
        const found = shockRef.value.find((item: any) => item.inpactloadtypeId == row.cellValue)
        typeId.value = found.inpactloadtypeId
        return  found.inpactloadtypeName|| '' // 使用可选链并提供默认值
    }else if(row.column.field =='inpactloadStartup'){ 
        const found = shockRef.value.find((item: any) => item.inpactloadtypeId == typeId.value)
        return found.inpactloadStartup
    }else if(row.column.field == 'isIsolationtrans'){
        return row.cellValue == '01'  ? '是' : '否'
    }else if(row.column.field =='inpactloadFreq'){
        if(row.cellValue == '01'){
            return 'N≤4次/d'
        }else if(row.cellValue == '02'){
            return 'N>4次/d且N≤2次/h'
        }else{
            return '2次/h<N≤10次/h'
        }
    }else if (row.column.field == 'harmId') {
        const found = characteristic.value.find((item: any) => item.harmId == row.cellValue)
        if(found){
            return found.harmName 
        }else{
            return ''
        }
    } else if (
        row.column.field == 'capacitorScale' ||
        row.column.field == 'linevoltageScale' ||
        row.column.field == 'translowScale' ||
        row.column.field == 'inpactloadhighScale'||  row.column.field == 'inpactloadlowScale' 
    ) {
        const found = voltageleveloption.find((item: any) => item.id == row.cellValue)
        return found?.name || ''
    } else {
        return row.cellValue
    }
}
// 字典
const dictionary = () => {
    // 获取变压器参数字典列表
    transformerList({}).then((res: any) => {
        transformer.value = res.data
    })
    // 谐波源特特性
    characteristicList({}).then((res: any) => {
        characteristic.value = res.data
    })
    //冲击负荷类型
    loadList({}).then((res: any) => {
        shockRef.value = res.data
    })
  
}
onBeforeMount(async () => {
   
    await dictionary()
    info(0) // 变压器参数维护
    info(1) // 电容器参数维护
    info(2) // 风电参数
    info(3) // 用户负荷
    info(4) // 冲击性负荷
})
</script>
<style lang="scss" scoped></style>
