<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="formData" :rules="rules" label-width="100px" label-position="top">
            <el-row :gutter="15">
                <el-col :span="24">
                    <el-form-item label="套餐名称" prop="name">
                        <el-input v-model="formData.name" clearable placeholder="请输入"></el-input>
                        <div class="el-form-item-msg"></div>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="时长类型" prop="unit">
                        <el-radio-group v-model="formData.unit">
                            <el-radio border :label="1">按天</el-radio>
                            <el-radio border :label="2">按年</el-radio>
                        </el-radio-group>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="套餐时长" prop="time">
                        <el-input v-model="formData.time" clearable placeholder="请输入">
                            <template #append>{{ formData.unit==1?'天':'年' }}</template>
                        </el-input>
                        <div class="el-form-item-msg"></div>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="套餐金额" prop="money">
                        <el-input v-model="formData.money" clearable placeholder="请输入">
                            <template #append>元</template>
                        </el-input>
                        <div class="el-form-item-msg">实际支付金额</div>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="套餐划线金额" prop="old_money">
                        <el-input v-model="formData.old_money" clearable placeholder="请输入">
                            <template #append>元</template>
                        </el-input>
                        <div class="el-form-item-msg"></div>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="排序" prop="sort">
                        <el-input v-model="formData.sort" clearable placeholder="请输入"></el-input>
                        <div class="el-form-item-msg"></div>
                    </el-form-item>
                </el-col>
            </el-row>
        </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:{
                unit:2,
                sort:0
            },
            rules:{
                name: [
                    {required: true, message: '请输入'}
                ],
                money: [
                    {required: true, message: '请输入'}
                ],
                time: [
                    {required: true, message: '请输入'}
                ],
                old_money: [
                    {required: true, message: '请输入'}
                ],
                unit: [
                    {required: true, message: '请选择'}
                ],
            }
        }
    },
    methods:{
        open(mode = 'add'){
            this.mode = mode;
            this.visible = true;
            return this
        },
        //表单注入数据
        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;
            let submitData = JSON.parse(JSON.stringify(this.formData));
            var resp = await this.$API.combo.save.post(submitData);
            this.isSaveing = false;
            if (resp.code == 0) {
                return this.$message.error(resp.msg);
            }
            this.$message.success(resp.msg);
            this.visible = false;
            this.formData = {
                unit:2,
                sort:0
            };
            this.submitState = false;
            this.$emit("success");
        }
    }
}
</script>