<template>
    <el-dialog draggable width="800px" v-model="dialogVisible" :title="title">
        <el-form :inline="false" :model="form" label-width="auto" :rules="rules" ref="formRef">
            <el-form-item label="谐波源名称" prop="harmName">
                <el-input
                    v-model.trim="form.harmName"
                    clearable
                    style="width: 475px"
                    maxlength="32"
                    show-word-limit
                    placeholder="请输入谐波源名称"
                />
            </el-form-item>
            <div class="form-four">
                <el-form-item v-for="item in 49" :label="`谐波${item + 1}次`" :key="item + 1" :prop="`i${item + 1}`">
                    <el-input-number v-model.trim="form[`i${item + 1}`]" show-word-limit placeholder="请输入值" min="-99" max="99"/>
                </el-form-item>
            </div>
        </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 { characteristicAdd, characteristicEdit } from '@/api/advance-boot/assess'
const emit = defineEmits(['submit'])
const dialogVisible = ref(false)
const title = ref('')
const formRef = ref()
// 注意不要和表单ref的命名冲突
const form: any = ref({
    harmName: ''
})
const rules: any = ref({
    harmName: [{ required: true, message: '谐波源名称', trigger: 'blur' }]
})

const open = (text: string, data?: anyObj) => {
    for (let i = 2; i < 51; i++) {
        rules.value[`i${i}`] = [{ required: true, message: '请输入值', trigger: 'blur' }]
    }

    title.value = text
    dialogVisible.value = true

    if (data) {
        // 表单赋值
        form.value = JSON.parse(JSON.stringify(data))
    } else {
        form.value = {
            harmName: ''
        }
        for (let i = 2; i < 51; i++) {
            form.value[`i${i}`] = 0
        }
    }
}
onMounted(() => {})
const submit = () => {
    formRef.value.validate(async (valid: boolean) => {
        if (valid) {
            if (title.value == '新增谐波源特性字典') {
                characteristicAdd(form.value).then(res => {
                    ElMessage.success('新增成功')
                    dialogVisible.value = false
                    emit('submit')
                })
            } else {
                characteristicEdit(form.value).then(res => {
                    ElMessage.success('编辑成功')
                    dialogVisible.value = false
                    emit('submit')
                })
            }
        }
    })
}

defineExpose({ open })
</script>
<style lang="scss" scoped>
.form-four {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    .el-form-item {
        display: flex;
        width: 33%;
        .el-form-item__content {
            flex: 1;
            .el-select,
            .el-cascader,
            .el-input__inner,
            .el-date-editor {
                width: 100%;
            }
        }
    }
}
</style>
