<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">
            <template v-if="type==1">
                <el-form-item label="纸张类型" prop="paper_size">
                    <el-radio-group v-model="formData.paper_size">
                        <el-radio-button :label="item" v-for="(item,index) in printType" :key="index">{{ item }}</el-radio-button>
                    </el-radio-group>
                    <div class="el-form-item-msg"></div>
                </el-form-item>
                <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>
            </template>
            <template v-if="type==2">
                <el-form-item label="纸张类型" prop="paper_size">
                    <el-radio-group v-model="formData.paper_size" disabled>
                        <el-radio-button :label="item" v-for="(item,index) in printType" :key="index">{{ item }}</el-radio-button>
                    </el-radio-group>
                    <div class="el-form-item-msg"></div>
                </el-form-item>
                <el-form-item label="单双面" prop="duplex">
                    <el-radio-group v-model="formData.duplex" disabled>
                        <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>
            </template>
            <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="price">
                <el-input v-model="formData.price" clearable placeholder="请输入">
                    <template #append>元</template>
                </el-input>
            </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:{
                paper_size: [
                    {required: true, message: '请选择'}
                ],
                color: [
                    {required: true, message: '请选择'}
                ],
                duplex: [
                    {required: true, message: '请选择'}
                ],
                price: [
                    {required: true, message: '请输入'}
                ],
            },
            category:[],
            batchData:[],
            type:1
        }
    },
    methods: {
        open(mode = 'add',type){
            this.mode = mode;
            this.visible = true;
            this.type = type;
            if (type == 1) {
                this.titleMap.add = "新建打印价格";
                this.titleMap.edit = "编辑打印价格";
            } else {
                this.titleMap.add = "新建复印价格";
                this.titleMap.edit = "编辑复印价格";
                this.formData.duplex = 1;
                this.formData.paper_size = 'A4';
            }
            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.formData.type = this.type;
            this.isSaveing = true;
            var resp = await this.$API.mPrice.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>