<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'"
                        style="width: 100%"
                        v-model="form[item.field]"
                        show-word-limit
                        placeholder="请输入值"
                    ></el-input-number>
                    <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 { transformerList, assessTransformerParamAdd } from '@/api/advance-boot/assess'
import { ElMessage, ElMessageBox, ElDatePicker } from 'element-plus'
const emits = defineEmits<{
    (e: 'init'): void
}>()
const child: any = ref([])
const transformer: any = ref([])
const form: any = reactive({})
const rules = {
    transName: [{ required: true, message: '请输入用户变压器名称', trigger: 'blur' }],
    transtypeId: [{ required: true, message: '请选择变压器型号', trigger: 'change' }],
    transNum: [{ required: true, message: '请输入变压器台数', trigger: 'blur' }]
}
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[item.field] = ''
        })
    } else {
        row.item.child.forEach((item: any) => {
            form[item.field] = row.row[item.field]
        })
    }
    form.assessId = row.assessId
    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).then((res: any) => {
                    ElMessage.success('新增成功')
                    emits('init')
                    dialogVisible.value = false
                })
        }
    })
}

defineExpose({ open })
</script>
