<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
        <el-form :model="formData" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-position="top" label-width="120px">
            <el-form-item label="名称" prop="name">
				<el-input v-model="formData.name" clearable></el-input>
			</el-form-item>
            <el-form-item label="首字母" prop="letter">
				<el-input v-model="formData.letter" clearable></el-input>
			</el-form-item>
        </el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
    </el-dialog>
</template>
<script>
export default{
    data(){
        return {
            loading: false,
            mode:"edit",
            titleMap:{
                add:"新增预设项",
                edit:"编辑预设项"
            },
            visible: false,
            isSaveing: false,
            formData:{},
            rules:{
                name: [
                    {required: true, message: '请输入颜色名称'}
                ]
            }
        }
    },
    methods:{
        // 显示
        open(mode = 'add'){
            this.mode = mode;
            this.visible = true;
            this.isSaveing = false;
            return this;
        },
        //表单注入数据
        setData(index,data,type){
            this.formData = data;
            this.formData.num = index;
            this.formData.type = type;
            this.loading = true;
        },
        /**
         * 提交
         */
        submit(){
            const { formData, $message } = this;
            this.isSaveing = true;
            this.$refs.dialogForm.validate(async (valid) => {
                if (valid) {
                    var result = await this.$API.cloth.color.post(formData);
                    if(result.code !== 1){
                        $message.warning(result.msg)
                        this.isSaveing = false;
                        return ;
                    }
                    this.isSaveing = false;
                    this.formData = {};
                    this.$emit('success')
                    this.visible = false;
                    $message.success("操作成功")
                } else {
                    this.isSaveing = false;
                    return false;
                }
            })
        }
    }
}
</script>