<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')" :close-on-click-modal="false" :close-on-press-escape="false">
        <el-form ref="dialogForm" :model="formData" :rules="rules" label-width="120px" label-position="top">
            <el-form-item label="分类名称" prop="name">
                <el-input v-model="formData.name" placeholder="请输入"></el-input>
                <div class="el-form-item-msg"></div>
            </el-form-item>
            <el-form-item label="字段名称" prop="code">
                <el-input v-model="formData.code" placeholder="请输入"></el-input>
                <div class="el-form-item-msg">只支持英文字母、数字，不支持中文</div>
            </el-form-item>
        </el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保存</el-button>
		</template>
    </el-dialog>
</template>

<script>
export default {
    data() {
        return {
            loading: false,
            mode:"add",
            visible: false,
            isSaveing: false,
            titleMap:{
                add:"新增字典分类",
                edit:"编辑字典分类"
            },
            rules:{
                name: [
                    {required: true, message: '请输入'}
                ],
                code: [
                    {required: true, message: '请输入'}
                ],
            },
            formData:{}
        }
    },
    methods: {
        open(mode = 'add'){
            this.mode = mode;
            this.visible = true;
            return this
        },
        //表单注入数据
        setData(data){
            this.formData = JSON.parse(JSON.stringify(data));
        },
        async submit(){
            var validate = await this.$refs.dialogForm.validate().catch(()=>{});
            if(!validate){ return false }
            this.isSaveing = true;
            var resp = await this.$API.dic.save.post(this.formData);
            this.isSaveing = false;
            if (resp.code == 0) {
                return this.$message.error(resp.msg);
            }
            this.$message.success(resp.msg);
            this.visible = false;
            this.formData = {};
            this.$emit("success");
        }
    }
}
</script>