<template>
    <!-- 基础管理-设备管理-新增或编辑 -->
    <div>
        <el-form :model="addOrEditForm" :rules="addOrEditFormRules" ref="addOrEditFormRef" label-width="100px">
            <el-form-item label="上级节点" prop="deviceName">
                <el-input v-model="addOrEditForm.deviceName" placeholder="请输入设备名称" />
            </el-form-item>
            <el-form-item label="分类名称" prop="proName">
                <el-input v-model="addOrEditForm.proName" placeholder="请输入设备编码" />
            </el-form-item>

            <el-form-item>
                <el-button @click="cancel">取消</el-button>
                <el-button type="primary" @click="submitForm">确定</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import { addModelApi } from '@/api/basic/model'
export default {
    name: 'AddOrEditForm',

    data() {
        return {
            addOrEditForm: {
                deviceName: '',
                proName: '',
                projectId: '',
                level: '',
                nodeCount: '',
                proName: '',
                parentId: '', //父级节点id
            },
            addOrEditFormRules: {
                deviceName: [{ required: true, message: '请输入设备名称', trigger: 'blur' }],
                proName: [{ required: true, message: '请输入设备编码', trigger: 'blur' }],
                Keeper: [{ required: true, message: '请选择设备保管人', trigger: 'change' }],
            },

            KeeperList: [
                {
                    id: '张三',
                    name: '张三',
                },
                {
                    id: '李四',
                    name: '李四',
                },
            ],
        }
    },
    methods: {
        cancel() {
            this.$emit('closeAddOrEditFormDialog', false)
        },
        submitForm() {
            this.$refs.addOrEditFormRef.validate(async (valid) => {
                if (valid) {
                    // 组装参数
                    const params = JSON.parse(JSON.stringify(this.addOrEditForm))
                    this.$delete(params, 'deviceName')
                    const res = await addModelApi(params)
                    if (res.code === 200) {
                        this.$message.success('新增成功')
                        this.$emit('closeAddOrEditFormDialog', true)
                    } else {
                        this.$message.error(res.msg)
                    }
                } else {
                    return false
                }
            })
        },
        resetForm() {
            this.$refs.addOrEditFormRef.resetFields()
        },

        setFormData(data) {
            const { deviceName, id, level, nodeCount, projectId } = data
            this.addOrEditForm = {
                deviceName,
                projectId,
                parentId: id,
                level: parseInt(level) + 1,
                nodeCount,
            }
        },
    },
}
</script>
