<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="linetypeName">
                <el-input
                    v-model.trim="form.linetypeName"
                    clearable
                    maxlength="32"
                    show-word-limit
                    placeholder="请输入导线型号"
                />
            </el-form-item>
            <el-form-item label="线缆类型" prop="linetypeType">
                <el-select v-model="form.linetypeType" placeholder="请选择线缆类型" filterable>
                    <el-option label="架空线" value="01"></el-option>
                    <el-option label="电缆" value="02"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="正序电阻R1(Ω/km)" prop="posR1">
                <el-input-number v-model.trim="form.posR1" show-word-limit placeholder="请输入值" max="99" min="-99"></el-input-number>
            </el-form-item>
            <el-form-item label="正序电抗X1(Ω/km)" prop="posX1">
                <el-input-number v-model.trim="form.posX1" show-word-limit placeholder="请输入值" max="99" min="-99"></el-input-number>
            </el-form-item>
            <el-form-item label="正序电纳Y1(S/km)" prop="posY1">
                <el-input-number v-model.trim="form.posY1" show-word-limit placeholder="请输入值" max="99" min="-99"></el-input-number>
            </el-form-item>
            <el-form-item label="零序电阻R0(Ω/km)" prop="zeroR0">
                <el-input-number v-model.trim="form.zeroR0" show-word-limit placeholder="请输入值" max="99" min="-99"></el-input-number>
            </el-form-item>
            <el-form-item label="零序电抗X0(Ω/km)" prop="zeroX0">
                <el-input-number v-model.trim="form.zeroX0" show-word-limit placeholder="请输入值" max="99" min="-99"></el-input-number>
            </el-form-item>
            <el-form-item label="零序电纳Y0(S/km)" prop="zeroY0">
                <el-input-number v-model.trim="form.zeroY0" show-word-limit placeholder="请输入值" max="99" min="-99"></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 { cableAdd, cableEdit } from '@/api/advance-boot/assess'
const emit = defineEmits(['submit'])
const dialogVisible = ref(false)
const title = ref('')
const formRef = ref()
// 注意不要和表单ref的命名冲突
const form: any = ref({
    linetypeName: '',
    linetypeType: '',
    posR1: 0,
    posX1: 0,
    posY1: 0,
    zeroR0: 0,
    zeroX0: 0,
    zeroY0: 0
})
const rules: any = ref({
    linetypeName: [{ required: true, message: '请输入导线型号', trigger: 'blur' }],
    linetypeType: [{ required: true, message: '请选择线缆类型', trigger: 'blur' }],
    posR1: [{ required: true, message: '请输入正序电阻', trigger: 'blur' }],
    posX1: [{ required: true, message: '请输入正序电抗', trigger: 'blur' }],
    posY1: [{ required: true, message: '请输入正序电纳', trigger: 'blur' }],
    zeroR0: [{ required: true, message: '请输入零序电阻', trigger: 'blur' }],
    zeroX0: [{ required: true, message: '请输入零序电抗', trigger: 'blur' }],
    zeroY0: [{ 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 = {
            linetypeName: '',
            linetypeType: '',
            posR1: 0,
            posX1: 0,
            posY1: 0,
            zeroR0: 0,
            zeroX0: 0,
            zeroY0: 0
        }
    }
}
onMounted(() => {})
const submit = () => {
    formRef.value.validate(async (valid: boolean) => {
        if (valid) {
            if (title.value == '新增线缆类型字典') {
                cableAdd(form.value).then(res => {
                    ElMessage.success('新增成功')
                    dialogVisible.value = false
                    emit('submit')
                })
            } else {
                cableEdit(form.value).then(res => {
                    ElMessage.success('编辑成功')
                    dialogVisible.value = false
                    emit('submit')
                })
            }
        }
    })
}

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