<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="deviceCode">
                <el-select
                    clearable
                    filterable
                    style="width: 100%"
                    v-model="addOrEditForm.parentId"
                    placeholder="请选择上级节点"
                >
                    <el-option v-for="item in KeeperList" :key="item.id" :label="item.name" :value="item.id" />
                </el-select>
            </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>
export default {
    name: 'AddOrEditForm',

    data() {
        return {
            addOrEditForm: {
                deviceName: '',
                deviceCode: '',
                Keeper: '',
                remark: '',
            },
            addOrEditFormRules: {
                deviceName: [{ required: true, message: '请输入设备名称', trigger: 'blur' }],
                deviceCode: [{ required: true, message: '请输入设备编码', trigger: 'blur' }],
                Keeper: [{ required: true, message: '请选择设备保管人', trigger: 'change' }],
            },

            KeeperList: [
                {
                    id: '张三',
                    name: '张三',
                },
                {
                    id: '李四',
                    name: '李四',
                },
            ],
        }
    },
    methods: {
        cancel() {
            this.$emit('closeAddOrEditFormDialog')
        },
        submitForm() {
            // this.$emit('submitForm')

            this.$refs.addOrEditFormRef.validate((valid) => {
                if (valid) {
                    this.$emit('closeAddOrEditFormDialog')
                } else {
                    return false
                }
            })
        },
        resetForm() {
            this.$refs.addOrEditFormRef.resetFields()
        },

        setFormData(data) {
            const { deviceName, deviceCode, remark, Keeper } = data
            this.addOrEditForm = {
                deviceName,
                deviceCode,
                remark,
                Keeper,
            }
        },
    },
}
</script>
