<template>
    <el-dialog draggable width="800px" v-model="dialogVisible" :title="title">
        <el-scrollbar>
            <el-form :inline="false" ref="formRef" :model="form" label-width="auto" :rules="rules" class="form-two">
                <!-- 变压器参数维护 -->
                <el-form-item v-for="(item, index) in child" :key="index" :label="item.title" :prop="item.field">
                    <el-select
                        v-if="item.field == 'transtypeId'"
                        v-model="form[item.field]"
                        filterable
                        clearable
                        placeholder="请选择变压器型号"
                    >
                        <el-option
                            v-for="item in transformer"
                            :key="item.transtypeId"
                            :label="item.transtypeName"
                            :value="item.transtypeId"
                        ></el-option>
                    </el-select>
                    <el-select
                        v-else-if="item.field == 'harmId'"
                        v-model="form[item.field]"
                        filterable
                        clearable
                        placeholder="请选择谐波源"
                    >
                        <el-option
                            v-for="item in characteristic"
                            :key="item.harmId"
                            :label="item.harmName"
                            :value="item.harmId"
                        ></el-option>
                    </el-select>

                    <el-input-number
                        v-else-if="
                            item.field == 'transNum' ||
                            item.field == 'capacitorNum' ||
                            item.field == 'capacitorCapacity' ||
                            item.field == 'capacitorReactance' ||
                            item.field == 'flickerCoeff' ||
                            item.field == 's'||
                            item.field == 'transhighCapacity'||
                            item.field == 'transImpedance'||
                            item.field == 'nonlinearloadPower'||
                            item.field == 'powerFactor'
                        "
                        style="width: 100%"
                        v-model="form[item.field]"
                        show-word-limit
                        :min="0"
                        placeholder="请输入值"
                    ></el-input-number>

                    <el-select
                        v-else-if="
                            item.field == 'capacitorScale' ||
                            item.field == 'linevoltageScale' ||
                            item.field == 'translowScale'
                        "
                        v-model="form[item.field]"
                        filterable
                        placeholder="请选择电压等级"
                    >
                        <el-option
                            v-for="item in voltageleveloption"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        ></el-option>
                    </el-select>
                    <el-input v-else v-model.trim="form[item.field]" placeholder="请输入内容" />
                </el-form-item>
            </el-form>
        </el-scrollbar>

        <template #footer>
            <span class="dialog-footer">
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="submit">确认</el-button>
            </span>
        </template>
    </el-dialog>
</template>
<script lang="ts" setup>
import { ref, inject } from 'vue'
import { reactive } from 'vue'
import { useDictData } from '@/stores/dictData'
import {
    transformerList,
    characteristicList,
    assessTransformerParamAdd,
    assessTransformerParamEdit,
    capacitorAdd,
    capacitorEdit,
    windPowerAdd,
    windPowerDdit,
    loadParamAdd,
    loadParamEdit,
    shockAdd,
    shockEdit
} from '@/api/advance-boot/assess'
import { ElMessage, ElMessageBox, ElDatePicker } from 'element-plus'
const emits = defineEmits(['init'])
const child: any = ref([])
const transformer: any = ref([])
const characteristic: any = ref([])
const form: any = ref({})
const dictData = useDictData()
//字典获取电压等级
const voltageleveloption = dictData.getBasicData('Dev_Voltage_Stand')
const rules = {
    transName: [{ required: true, message: '请输入用户变压器名称', trigger: 'blur' }],
    transtypeId: [{ required: true, message: '请选择变压器型号', trigger: 'change' }],
    transNum: [{ required: true, message: '请输入变压器台数', trigger: 'blur' }],
    capacitorName: [{ required: true, message: '请输入用户电容器名称', trigger: 'blur' }],
    capacitorNum: [{ required: true, message: '请输入电容器组数量', trigger: 'blur' }],
    capacitorCapacity: [{ required: true, message: '请输入单组容量', trigger: 'blur' }],
    capacitorReactance: [{ required: true, message: '请输入电抗率', trigger: 'blur' }],
    capacitorScale: [{ required: true, message: '请选择电压等级', trigger: 'change' }],
    windgeneratorName: [{ required: true, message: '请输入风机名称', trigger: 'blur' }],
    flickerCoeff: [{ required: true, message: '请输入风机闪变系数', trigger: 'blur' }],
    s: [{ required: true, message: '请输入风机额定视在功率', trigger: 'blur' }],
    loadName: [{ required: true, message: '请输入负荷名称', trigger: 'blur' }],
    transhighCapacity: [{ required: true, message: '请输入配变容量', trigger: 'blur' }],
    transImpedance: [{ required: true, message: '请输入配电变压器阻抗', trigger: 'blur' }],
    nonlinearloadPower: [{ required: true, message: '请输入非线性负荷功率', trigger: 'blur' }],
    powerFactor: [{ required: true, message: '请输入功率因数', trigger: 'blur' }],
    linevoltageScale: [{ required: true, message: '请选择进线电压', trigger: 'change' }],
    harmId: [{ required: true, message: '请选择谐波源特性', trigger: 'change' }],
    translowScale: [{ required: true, message: '请选择配变低压侧电压', trigger: 'change' }],
}
const formRef = ref()
const dialogVisible = ref(false)
const title = ref('')
const open = (row: any) => {
    console.log('🚀 ~ open ~ row:', row)
    info()
    title.value = row.title + `_${row.item.name}`
    child.value = row.item.child

    if (row.title == '新增') {
        row.item.child.forEach((item: any) => {
            form.value[item.field] = ''
        })
        form.value.assessId = row.assessId
    } else {
        row.item.child.forEach((item: any) => {
            form.value = JSON.parse(JSON.stringify(row.row))
        })
    }

    dialogVisible.value = true
}
const info = async () => {
    // 获取变压器参数字典列表
    transformerList({}).then((res: any) => {
        transformer.value = res.data
    })
    // 谐波源特特性
    characteristicList({}).then((res: any) => {
        characteristic.value = res.data
    })
}
const submit = async () => {
    formRef.value.validate(async (valid: any) => {
        if (valid) {
            if (title.value == '新增_变压器参数维护') {
                await assessTransformerParamAdd(form.value).then((res: any) => {
                    ElMessage.success('新增成功')
                    emits('init', 0)
                })
            } else if (title.value == '编辑_变压器参数维护') {
                await assessTransformerParamEdit(form.value).then((res: any) => {
                    ElMessage.success('修改成功')
                    emits('init', 0)
                })
            } else if (title.value == '新增_电容器组参数维护') {
                await capacitorAdd(form.value).then((res: any) => {
                    ElMessage.success('新增成功')
                    emits('init', 1)
                })
            } else if (title.value == '编辑_电容器组参数维护') {
                await capacitorEdit(form.value).then((res: any) => {
                    ElMessage.success('修改成功')
                    emits('init', 1)
                })
            } else if (title.value == '新增_风电参数') {
                await windPowerAdd(form.value).then((res: any) => {
                    ElMessage.success('新增成功')
                    emits('init', 2)
                })
            } else if (title.value == '编辑_风电参数') {
                await windPowerDdit(form.value).then((res: any) => {
                    ElMessage.success('修改成功')
                    emits('init', 2)
                })
            } else if (title.value == '新增_用户负荷') {
                await loadParamAdd(form.value).then((res: any) => {
                    ElMessage.success('新增成功')
                    emits('init', 3)
                })
            } else if (title.value == '编辑_用户负荷') {
                await loadParamEdit(form.value).then((res: any) => {
                    ElMessage.success('修改成功')
                    emits('init', 3)
                })
            }else if (title.value == '新增_冲击负荷') {
                await shockAdd(form.value).then((res: any) => {
                    ElMessage.success('新增成功')
                    emits('init', 4)
                })
            } else if (title.value == '编辑_冲击负荷') {
                await shockEdit(form.value).then((res: any) => {
                    ElMessage.success('修改成功')
                    emits('init', 4)
                })
            }
            dialogVisible.value = false
        }
    })
}

defineExpose({ open })
</script>
