<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-input-number
                        v-else-if="
                            item.field == 'transNum' ||
                            item.field == 'capacitorNum' ||
                            item.field == 'capacitorCapacity' ||
                            item.field == 'capacitorReactance'
                        "
                        style="width: 100%"
                        v-model="form[item.field]"
                        show-word-limit
                        placeholder="请输入值"
                    ></el-input-number>

                    <el-select
                        v-else-if="item.field == 'capacitorScale'"
                        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,
    assessTransformerParamAdd,
    assessTransformerParamEdit,
    capacitorAdd,
    capacitorEdit
} 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 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' }],
}
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
    })
}
const submit = async () => {
    formRef.value.validate((valid: any) => {
        if (valid) {
            if (title.value == '新增_变压器参数维护') {
                assessTransformerParamAdd(form.value).then((res: any) => {
                    ElMessage.success('新增成功')
                    emits('init', 0)
                    dialogVisible.value = false
                })
            } else if (title.value == '编辑_变压器参数维护') {
                assessTransformerParamEdit(form.value).then((res: any) => {
                    ElMessage.success('修改成功')
                    emits('init', 0)
                    dialogVisible.value = false
                })
            } else if (title.value == '新增_电容器组参数维护') {
                capacitorAdd(form.value).then((res: any) => {
                    ElMessage.success('新增成功')
                    emits('init', 1)
                    dialogVisible.value = false
                })
            } else if (title.value == '编辑_电容器组参数维护') {
                capacitorEdit(form.value).then((res: any) => {
                    ElMessage.success('修改成功')
                    emits('init', 1)
                    dialogVisible.value = false
                })
            }
        }
    })
}

defineExpose({ open })
</script>
