<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')" :close-on-click-modal="false" :close-on-press-escape="false">
        <el-form ref="dialogForm" :model="formData" :rules="rules" label-width="100px" label-position="top">
            <el-form-item label="单双面" prop="duplex">
                <el-radio-group v-model="formData.duplex">
                    <el-radio-button :label="item.key" v-for="(item,index) in duplexType" :key="index">{{ item.name }}</el-radio-button>
                </el-radio-group>
                <div class="el-form-item-msg"></div>
            </el-form-item>
            <el-form-item label="打印机类型" prop="color">
                <el-radio-group v-model="formData.color">
                    <el-radio-button :label="item.key" v-for="(item,index) in colorType" :key="index">{{ item.name }}</el-radio-button>
                </el-radio-group>
                <div class="el-form-item-msg"></div>
            </el-form-item>
            <el-form-item label="数量" prop="number">
                <el-input v-model="formData.number" clearable placeholder="请输入">
                    <template #append>张/页</template>
                </el-input>
            </el-form-item>
            <el-form-item label="折扣" prop="rate">
                <el-input v-model="formData.rate" clearable placeholder="请输入"></el-input>
                <div class="el-form-item-msg">如：数量设置10，折扣设置0.8。表示打印对应的类型页面颜色的纸张数达到10或10张以上时，打8折</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 {
    components: {
        
    },
    data(){
        return {
            printType:['A3','A4','A5','A6','B4','B5'],
            duplexType:[{"name":"单面","key":1},{"name":"双面","key":2}],
            colorType:[{"name":"彩色","key":1},{"name":"黑白","key":2}],
            loading: false,
            mode:"add",
            titleMap:{
                add:"新建折扣",
                edit:"编辑折扣"
            },
            visible: false,
            isSaveing: false,
            formData:{},
            rules:{
                color: [
                    {required: true, message: '请选择'}
                ],
                duplex: [
                    {required: true, message: '请选择'}
                ],
                rate: [
                    {required: true, message: '请输入'}
                ],
                number: [
                    {required: true, message: '请输入'}
                ],
            },
            category:[],
            batchData:[],
            type:1
        }
    },
    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;
            var resp = await this.$API.mDiscount.save.post(this.formData);
            this.isSaveing = false;
            if (resp.code == 0) {
                return this.$message.error(resp.msg);
            }
            this.$message.success(resp.msg);
            this.visible = false;
            this.formData = {};
            this.$emit("success");
        }
    }
}
</script>