<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :width="600" destroy-on-close @closed="$emit('closed')">
        <el-form ref="dialogForm" :model="formData" :rules="rules" label-width="100px" label-position="top">
            <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-form-item label="等级标识" prop="level">
                <el-select v-model="formData.level" clearable placeholder="等级标识" :style="{width: '100%'}">
                    <el-option :value="item" :label="item" v-for="(item,index) in 20" :key="index"></el-option>
                </el-select>
                <div class="el-form-item-msg">数字越大等级越高</div>
            </el-form-item>
            <fieldset>
                <legend><el-tag>升级条件</el-tag></legend>
                <el-row :gutter="10">
                    <el-col :span="12">
                        <el-form-item label="素材GMV大于" prop="gmv_start">
                            <el-input v-model="formData.gmv_start" 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="素材GMV小于" prop="gmv_end">
                            <el-input v-model="formData.gmv_end" clearable placeholder="请输入">
                                <template #append>元</template>
                            </el-input>
                            <div class="el-form-item-msg"></div>
                        </el-form-item>
                    </el-col>
                </el-row>
            </fieldset>
            <el-form-item label="状态" prop="status">
                <el-radio-group v-model="formData.status">
                    <el-radio-button :label="1">正常</el-radio-button>
                    <el-radio-button :label="2">禁用</el-radio-button>
                </el-radio-group>
            </el-form-item>
        </el-form>
		<template #footer>
            <el-button @click="visible=false" size="large">取 消</el-button>
            <el-button v-if="mode!='show'" size="large" 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:{},
            rules:{
                status: [
                    {required: true, message: '请选择'}
                ],
                name: [
                    {required: true, message: '请输入'}
                ],
                level: [
                    {required: true, message: '请选择'}
                ],
                gmv_start: [
                    {required: true, message: '请输入'}
                ],
                gmv_end: [
                    {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));
            submitData.type = 1;
            var resp = await this.$API.level.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 = {};
            this.submitState = false;
            this.$emit("success");
        }
    }
}
</script>

<style>
.text-center{text-align: center;}
</style>