<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :width="600" :close-on-click-modal="false" destroy-on-close @closed="$emit('closed')">
        <el-form ref="dialogForm" :model="submitData" :rules="rules" label-width="100px" label-position="top">
            <el-form-item label="充值店铺">
                <el-input v-model="formData.shop_name" clearable placeholder="请输入" readonly disabled></el-input>
                <div class="el-form-item-msg"></div>
            </el-form-item>
            <el-form-item label="充值套餐" prop="combo">
                <el-select v-model="submitData.combo" clearable placeholder="请选择充值套餐" :style="{width: '100%'}">
                    <el-option :value="item.id" :label="item.name" v-for="(item,index) in comboData" :key="index"></el-option>
                </el-select>
                <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,
            isSaveing: false,
            mode:"add",
            titleMap:{
                add:"店铺套餐充值",
                edit:"店铺套餐充值"
            },
            visible:false,
            formData:{},
            submitData:{},
            comboData:[],
            rules:{
                combo: [
                    {required: true, message: '请选择'}
                ],
            }
        }
    },
    methods:{
        open(mode = 'add'){
            this.mode = mode;
            this.visible = true;
            this.getCombo()
            return this
        },
        async getCombo(){
            var resp = await this.$API.combo.list.get({"pageSize":100,"page":1});
            this.comboData = resp.data.rows;
        },
        //表单注入数据
        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;
            this.submitData.shop_id = this.formData.shop_id;
            var resp = await this.$API.shop.combo.post(this.submitData);
            this.isSaveing = false;
            if (resp.code == 0) {
                return this.$message.error(resp.msg);
            }
            this.$message.success(resp.msg);
            this.visible = false;
            this.formData = {};
            this.submitState = false;
            this.$emit("success");
        }
    }
}
</script>