<template>
    <el-dialog :title="form.id ? '编辑台桌' : '添加台桌'" :visible.sync="dialogVisible" @open="tbShopAreaGet" @close="reset">
        <el-form ref="form" :model="form" :rules="rules" label-width="120px" label-position="left">
            <el-form-item label="选择区域" prop="areaId">
                <el-select v-model="form.areaId" placeholder="请选择区域">
                    <el-option :label="item.name" :value="item.id" v-for="item in areaList" :key="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="台桌名称">
                <el-input v-model="form.name" placeholder="请输入台桌名称"></el-input>
            </el-form-item>
            <el-form-item label="客座数">
                <el-input-number v-model="form.maxCapacity" :min="0" controls-position="right"></el-input-number>
            </el-form-item>
            <el-form-item label="网络预定开关">
                <el-switch v-model="form.isPredate" :active-value="1" :inactive-value="2"></el-switch>
            </el-form-item>
            <el-form-item label="类型">
                <el-radio-group v-model="form.type">
                    <el-radio-button :label="0">低消</el-radio-button>
                    <el-radio-button :label="2">计时</el-radio-button>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="最低消费" v-if="form.type == 0">
                <el-input-number v-model="form.amount" :min="0" controls-position="right"></el-input-number>
            </el-form-item>
            <el-form-item label="每小时收费" v-if="form.type == 2">
                <el-input-number v-model="form.perhour" :min="0" controls-position="right"></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 { tbShopTable, tbShopAreaGet } from '@/api/table'
export default {
    data() {
        return {
            dialogVisible: false,
            resetForm: '',
            loading: false,
            form: {
                id: '',
                name: '',
                areaId: '',
                maxCapacity: 0,
                isPredate: 1,
                type: 2,
                perhour: 0,
                amount: 0
            },
            rules: {
                areaId: [
                    {
                        required: true,
                        message: '请选择区域',
                        trigger: 'blur'
                    }
                ]
            },
            areaList: []
        }
    },
    mounted() {
        this.resetForm = { ...this.form }
    },
    methods: {
        onSubmitHandle() {
            this.$refs.form.validate(async valid => {
                if (valid) {
                    this.loading = true
                    try {
                        let res = await tbShopTable({
                            ...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'
                        });
                        this.loading = false
                    } catch (error) {
                        this.loading = false
                        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 = { ...this.resetForm }
        },
        // 获取区域
        async tbShopAreaGet() {
            try {
                const { content } = await tbShopAreaGet({
                    shopId: localStorage.getItem('shopId')
                })
                this.areaList = content
            } catch (error) {
                console.log(error);
            }
        }
    }
}
</script>