<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)">新增</el-button>
            <el-button type="primary" size="small" icon="el-icon-Delete" @click="deleteFform(item, index)">
                删除
            </el-button>
            <!-- <el-button type="primary" size="small" icon="el-icon-Download">导出</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="60px"></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)">编辑</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,
    assessTransformerParamDelete,
    capacitorList,
    capacitorDelete,
    windPowerList,
    windPowerDelete
} 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 }
})
const dictData = useDictData()
//字典获取电压等级
const voltageleveloption = dictData.getBasicData('Dev_Voltage_Stand')
const transformer: 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: 'name'
            },
            {
                title: '风机闪变系数',
                field: 'name1'
            },
            {
                title: '风机额定视在功率',
                field: 'name2'
            }
        ]
    },
    {
        loading: false,
        name: '用户负荷',

        key: 'loadList',
        child: [
            {
                title: '配电窒名称',
                field: 'name'
            },
            {
                title: '进线电压',
                field: 'name1'
            },
            {
                title: '配变容量(kVA)',
                field: 'name2'
            },
            {
                title: '配电变压器阻抗(%)',
                field: 'name3'
            },
            {
                title: '谐波源特性',
                field: 'name4'
            },
            {
                title: '配变低压侧电压',
                field: 'name5'
            },
            {
                title: '非线性负荷功率(kW)',
                field: 'name6'
            },
            {
                title: '功率因素',
                field: 'name7'
            }
        ]
    },
    {
        loading: false,
        name: '冲击性负荷',

        key: 'impactList',
        child: [
            {
                title: '冲击负荷类型',
                field: 'name'
            },
            {
                title: '冲击负荷功率(MVA)',
                field: 'name1'
            },
            {
                title: '数量',
                field: 'name2'
            },
            {
                title: '启动方式',
                field: 'name3'
            },
            {
                title: '启动容量倍数',
                field: 'name4'
            },
            {
                title: '冲击负荷高压侧电压',
                field: 'name5'
            },
            {
                title: '有无隔离变',
                field: 'name6'
            },
            {
                title: '隔离变阻抗(%)',
                field: 'name7'
            },
            {
                title: '低压侧电压',
                field: 'name8'
            },
            {
                title: '负荷频度选择',
                field: 'name9'
            },
            {
                title: '闪变叠加系数',
                field: 'name10'
            }
        ]
    }
])
const dataList = ref({
    transformerList: [], //变压器参数维护
    capacitorList: [], //电容器
    windPowerList: [], //风电
    loadList: [], //用户负荷
    impactList: [] //冲击性负荷
})
// 新增
const addFform = (item: any) => {
    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(() => {
        if (i == 0) {
            assessTransformerParamDelete(xTableRef.value[i].getCheckboxRecords().map(item => item.transId)).then(
                res => {
                    ElMessage({
                        type: 'success',
                        message: '删除成功!'
                    })
                    info(i)
                }
            )
        } else if (i == 1) {
            capacitorDelete(xTableRef.value[i].getCheckboxRecords().map(item => item.capacitorId)).then(res => {
                ElMessage({
                    type: 'success',
                    message: '删除成功!'
                })
                info(i)
            })
        } else if (i == 2) {
            windPowerDelete(xTableRef.value[i].getCheckboxRecords().map(item => item.windgeneratorId)).then(res => {
                ElMessage({
                    type: 'success',
                    message: '删除成功!'
                })
                info(i)
            })
        }
    })
}
// 编辑
const edit = (item: any, row: any) => {
    addformRef.value.open({
        title: '编辑',
        item: item,
        row: row
    })
}
// 过滤数据
const formatter = (row: any) => {
    if (row.column.field == 'transtypeId') {
        const found = transformer.value.find((item: any) => item.transtypeId == row.cellValue)
        return found.transtypeName || '' // 使用可选链并提供默认值
    } else if (row.column.field == 'capacitorScale') {
        const found = voltageleveloption.find((item: any) => item.id == row.cellValue)
        return found.name || ''
    } else {
        return row.cellValue
    }
}

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
        })
    }
    column.value[i].loading = false
}

// 字典
const dictionary = () => {
    // 获取变压器参数字典列表
    transformerList({}).then((res: any) => {
        transformer.value = res.data
    })
}
onMounted(async () => {
    await dictionary()
    info(0) // 变压器参数维护
    info(1) // 变压器参数维护
    info(2) // 变压器参数维护
    // info(3) // 变压器参数维护
})
</script>
<style lang="scss" scoped></style>
