<template>
    <el-dialog :title="form.id ? '编辑' : '添加'" :visible.sync="dialogVisible" @close="reset">
        <el-form ref="form" :model="form" :rules="rules" label-width="120px" label-position="left">
            <el-form-item label="字典名称" prop="name">
                <el-input v-model="form.name" placeholder="请输入分类名称"></el-input>
            </el-form-item>
            <el-form-item label="状态">
                <el-switch v-model="form.status" :active-value="1" :inactive-value="0"></el-switch>
            </el-form-item>
            <!-- <el-form-item label="排序">
                <el-input-number v-model="form.sort" controls-position="right" :min="0"></el-input-number>
            </el-form-item> -->
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" :loading="loading" @click="onSubmitHandle">确 定</el-button>
        </span>
    </el-dialog>
</template>

<script>
import { tbCouponCategoryPostPut } from "@/api/setting";
export default {
    data() {
        return {
            dialogVisible: false,
            loading: false,
            form: {
                id: "",
                name: "",
                status: 1
            },
            rules: {
                name: [
                    {
                        required: true,
                        message: " ",
                        trigger: "blur"
                    }
                ]
            },
            resetForm: "",
        };
    },
    mounted() {
        this.resetForm = { ...this.form };
    },
    methods: {
        onSubmitHandle() {
            this.$refs.form.validate(async valid => {
                if (valid) {
                    try {
                        this.loading = true
                        let res = await tbCouponCategoryPostPut(this.form, this.form.id ? "put" : "post");
                        this.$emit("success", res);
                        this.close();
                        this.$notify({
                            title: "成功",
                            message: `${this.form.id ? "编辑" : "添加"}成功`,
                            type: "success"
                        });
                        this.loading = false
                    } catch (error) {
                        console.log(error);
                    }
                }
            });
        },
        show(obj) {
            this.dialogVisible = true;
            if (obj && obj.id) {
                this.form = { ...obj };
            }
        },
        close() {
            this.dialogVisible = false;
        },
        reset() {
            this.form = { ...this.resetForm };
        }
    }
};
</script>