<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>
        <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="onSubmitHandle">确 定</el-button>
        </span>
    </el-dialog>
</template>

<script>
import { tbShopArea } from '@/api/table'
export default {
    data() {
        return {
            dialogVisible: false,
            form: {
                id: '',
                name: ''
            },
            rules: {
                name: [
                    {
                        required: true,
                        message: '请输入区域名称',
                        trigger: 'blur'
                    }
                ]
            }
        }
    },
    methods: {
        onSubmitHandle() {
            this.$refs.form.validate(async valid => {
                if (valid) {
                    try {
                        let res = await tbShopArea({
                            ...this.form,
                            shopId: localStorage.getItem('shopId')
                        }, this.form.id ? 'put' : 'post')
                        this.$emit('success', res)
                        this.close()
                        this.$notify({
                            title: '成功',
                            message: `${this.form.id ? '编辑' : '添加'}成功`,
                            type: 'success'
                        });
                    } catch (error) {
                        console.log(error)
                    }
                }
            })
        },
        show(obj) {
            this.dialogVisible = true
            if (obj && obj.id) {
                this.form = JSON.parse(JSON.stringify(obj))
            }
        },
        close() {
            this.dialogVisible = false
        },
        reset() {
            this.form.id = ''
            this.form.name = ''
        }
    }
}
</script>