<template>
    <el-dialog draggable width="800px" v-model="dialogVisible" :title="title">
        <el-form :inline="false" :model="form" label-width="auto" class="form-two" :rules="rules" ref="formRef">
            <el-form-item label="变压器型号" prop="transtypeName">
                <el-input
                    v-model.trim="form.transtypeName"
                    clearable
                    maxlength="32"
                    show-word-limit
                    placeholder="请输入变压器型号"
                />
            </el-form-item>

            <el-form-item label="额定容量(kVA)" prop="transCapa">
                <el-input-number v-model.trim="form.transCapa" show-word-limit placeholder="请输入值"></el-input-number>
            </el-form-item>
            <el-form-item label="高压侧电压" prop="transhighScale">
                <el-select v-model="form.transhighScale" placeholder="请选择高压侧电压" filterable>
                    <el-option
                        v-for="item in voltageLevelList"
                        :key="item.id"
                        :label="item.name"
                        :value="item.id"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="低压侧电压" prop="translowScale">
                <el-select v-model="form.translowScale" placeholder="请选择低压侧电压" filterable>
                    <el-option
                        v-for="item in voltageLevelList"
                        :key="item.id"
                        :label="item.name"
                        :value="item.id"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="联结组标号" prop="trnasNo">
                <el-input-number v-model.trim="form.trnasNo" show-word-limit placeholder="请输入值" max="99999999" min="0"></el-input-number>
            </el-form-item>
            <el-form-item label="空载损耗(kW)" prop="noloadLoss">
                <el-input-number
                    v-model.trim="form.noloadLoss"
                    show-word-limit
                    placeholder="请输入值空载损耗"
                    max="99999999" min="0"
                ></el-input-number>
            </el-form-item>
            <el-form-item label="负载损耗(kW)" prop="loadLoss">
                <el-input-number
                    v-model.trim="form.loadLoss"
                    show-word-limit
                    placeholder="请输入值负载损耗"
                    max="99999999" min="0"
                ></el-input-number>
            </el-form-item>
            <el-form-item label="空载电流(A)" prop="noloadCur">
                <el-input-number v-model.trim="form.noloadCur" show-word-limit placeholder="请输入值" max="99999999" min="0"></el-input-number>
            </el-form-item>
            <el-form-item label="短路阻抗(%)" prop="shortCircuitImpedance">
                <el-input-number
                    v-model.trim="form.shortCircuitImpedance"
                    show-word-limit
                    placeholder="请输入值"
                    max="99999999" min="0"
                ></el-input-number>
            </el-form-item>
        </el-form>
        <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, onMounted } from 'vue'
import { reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { transformerAdd, transformerEdit } from '@/api/advance-boot/assess'
import { useDictData } from '@/stores/dictData'
const dictData = useDictData()
//字典电压等级
const voltageLevelList = dictData.getBasicData('Dev_Voltage_Stand')
const emit = defineEmits(['submit'])
const dialogVisible = ref(false)
const title = ref('')
const formRef = ref()
// 注意不要和表单ref的命名冲突
const form: any = ref({
    transtypeName: '',
    translowScale: '',
    transCapa: 0,
    transhighScale: '',
    trnasNo: 0,
    noloadLoss: 0,
    loadLoss: 0,
    noloadCur: 0,
    shortCircuitImpedance: 0
})
const rules: any = ref({
    transtypeName: [{ required: true, message: '请输入变压器型号', trigger: 'blur' }],
    translowScale: [{ required: true, message: '请选择低压侧电压', trigger: 'change' }],
    transCapa: [{ required: true, message: '请输入额定容量', trigger: 'blur' }],
    transhighScale: [{ required: true, message: '请选择高压侧电压', trigger: 'change' }],
    trnasNo: [{ required: true, message: '请输入联结组标号', trigger: 'blur' }],
    noloadLoss: [{ required: true, message: '请输入空载损耗', trigger: 'blur' }],
    loadLoss: [{ required: true, message: '请输入空载损耗', trigger: 'blur' }],
    noloadCur: [{ required: true, message: '请输入空载电流', trigger: 'blur' }],
    shortCircuitImpedance: [{ required: true, message: '请输入短路阻抗', trigger: 'blur' }]
})

const open = (text: string, data?: anyObj) => {
    title.value = text
    dialogVisible.value = true

    if (data) {
        // 表单赋值
        form.value = JSON.parse(JSON.stringify(data))
    } else {
        form.value = {
            transtypeName: '',
            translowScale: '',
            transCapa: 0,
            transhighScale: '',
            trnasNo: 0,
            noloadLoss: 0,
            loadLoss: 0,
            noloadCur: 0,
            shortCircuitImpedance: 0
        }
    }
}
onMounted(() => {})
const submit = () => {
    formRef.value.validate(async (valid: boolean) => {
        if (valid) {
            if (title.value == '新增变压器参数字典') {
                transformerAdd(form.value).then(res => {
                    ElMessage.success('新增成功')
                    dialogVisible.value = false
                    emit('submit')
                })
            } else {
                transformerEdit(form.value).then(res => {
                    ElMessage.success('编辑成功')
                    dialogVisible.value = false
                    emit('submit')
                })
            }
        }
    })
}

defineExpose({ open })
</script>
<style lang="scss" scoped>
.el-input-number {
    width: 100%;
}
</style>
