<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="inpactloadtypeName">
                <el-input
                    v-model.trim="form.inpactloadtypeName"
                    clearable
                    maxlength="32"
                    show-word-limit
                    placeholder="请输入负荷类型"
                />
            </el-form-item>
            <el-form-item label="启动方式" prop="inpactloadStartup">
                <el-input
                    v-model.trim="form.inpactloadStartup"
                    clearable
                    maxlength="32"
                    show-word-limit
                    placeholder="请输入启动方式"
                />
            </el-form-item>

            <el-form-item label="启动容量倍数" prop="inpactloadMultiple">
                <el-input-number
                    v-model.trim="form.inpactloadMultiple"
                    show-word-limit
                    placeholder="请输入值"
                    max="99"
                    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 { loadAdd, loadEdit } from '@/api/advance-boot/assess'
const emit = defineEmits(['submit'])
const dialogVisible = ref(false)
const title = ref('')
const formRef = ref()
// 注意不要和表单ref的命名冲突
const form: any = ref({
    inpactloadtypeName: '',
    inpactloadStartup: '',
    inpactloadMultiple: 0
})
const rules: any = ref({
    inpactloadtypeName: [{ required: true, message: '请输入负荷类型', trigger: 'blur' }],
    inpactloadStartup: [{ required: true, message: '请输入启动方式', trigger: 'blur' }],
    inpactloadMultiple: [{ 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 = {
            inpactloadtypeName: '',
            inpactloadStartup: '',
            inpactloadMultiple: 0
        }
    }
}
onMounted(() => {})
const submit = () => {
    formRef.value.validate(async (valid: boolean) => {
        if (valid) {
            if (title.value == '新增冲击负荷参数字典') {
                loadAdd(form.value).then(res => {
                    ElMessage.success('新增成功')
                    dialogVisible.value = false
                    emit('submit')
                })
            } else {
                loadEdit(form.value).then(res => {
                    ElMessage.success('编辑成功')
                    dialogVisible.value = false
                    emit('submit')
                })
            }
        }
    })
}

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