<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="120px" label-position="top">
            <el-form-item label="打印类型" prop="type">
                <el-checkbox-group v-model="formData.type">
                    <el-checkbox-button :label="item.key" v-for="(item,index) in typeData" :key="index">{{ item.name }}</el-checkbox-button>
                </el-checkbox-group>
                <div class="el-form-item-msg"></div>
            </el-form-item>
            <el-form-item label="支持纸型" prop="paper_size">
                <el-checkbox-group v-model="formData.paper_size">
                    <el-checkbox-button :label="item" v-for="(item,index) in printType" :key="index">{{ item }}</el-checkbox-button>
                </el-checkbox-group>
                <div class="el-form-item-msg"></div>
            </el-form-item>
            <el-form-item label="打印机类型" prop="color">
                <el-checkbox-group v-model="formData.color">
                    <el-checkbox-button :label="item.key" v-for="(item,index) in colorType" :key="index">{{ item.name }}</el-checkbox-button>
                </el-checkbox-group>
                <div class="el-form-item-msg"></div>
            </el-form-item>
            <el-form-item label="打印方向" prop="direction">
                <el-checkbox-group v-model="formData.direction">
                    <el-checkbox-button :label="item.key" v-for="(item,index) in directionType" :key="index">{{ item.name }}</el-checkbox-button>
                </el-checkbox-group>
                <div class="el-form-item-msg"></div>
            </el-form-item>
            <el-form-item label="支持单双面" prop="duplex">
                <el-checkbox-group v-model="formData.duplex">
                    <el-checkbox-button :label="item.key" v-for="(item,index) in duplexType" :key="index">{{ item.name }}</el-checkbox-button>
                </el-checkbox-group>
                <div class="el-form-item-msg"></div>
            </el-form-item>
            <el-form-item label="取件方式" prop="package">
                <el-checkbox-group v-model="formData.package">
                    <el-checkbox-button :label="item.key" v-for="(item,index) in packageType" :key="index">{{ item.name }}</el-checkbox-button>
                </el-checkbox-group>
                <div class="el-form-item-msg"></div>
            </el-form-item>
            <el-form-item label="额外收费" prop="is_price">
                <el-radio-group v-model="formData.is_price">
                    <el-radio-button :label="1">关闭</el-radio-button>
                    <el-radio-button :label="2">启用</el-radio-button>
                </el-radio-group>
                <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,
            mode:"add",
            titleMap:{
                add:"更新地址",
                edit:"更新地址"
            },
            typeData:[{"name":"文档","key":1}],
            printType:['A3','A4','A5','A6','B4','B5'],
            colorType:[{"name":"彩色","key":1},{"name":"黑白","key":2}],
            directionType:[{"name":"自适应","key":1},{"name":"横向","key":2},{"name":"竖向","key":3}],
            duplexType:[{"name":"单面","key":1},{"name":"双面","key":2}],
            packageType:[{"name":"店内打印","key":1},{"name":"远程自取","key":2},{"name":"商家配送","key":3}],
            visible: false,
            isSaveing: false,
            formData:{
                is_price:1
            },
            disabled:false,
            rules:{
                type: [
                    {required: true, message: '请选择'}
                ],
                paper_size: [
                    {required: true, message: '请选择'}
                ],
                color: [
                    {required: true, message: '请选择'}
                ],
                direction: [
                    {required: true, message: '请选择'}
                ],
                duplex: [
                    {required: true, message: '请选择'}
                ],
                package: [
                    {required: true, message: '请选择'}
                ],
                is_price: [
                    {required: true, message: '请选择'}
                ],
            }
        }
    },
    methods:{
        open(mode = 'add'){
            this.mode = mode;
            this.visible = true;
            return this
        },
        //表单注入数据
        setData(data){
            if (data.rule) {
                this.formData = data.rule;
            } else {
                this.formData = {}
            }
            this.formData.is_price = data.is_price;
            this.formData.id = data.id;
            this.titleMap.edit = "打印机【"+data.name + "】设置"
        },
        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.mPrint.save.post(submitData);
            this.isSaveing = false;
            if (resp.code !== 1) {
                return this.$message.error(resp.msg);
            }
            this.$message.success(resp.msg);
            this.visible = false;
            this.formData = {};
            this.submitState = false;
            this.$emit("success");
        }
    }
}
</script>
<style>
.mt20 {margin-top: 20px;}
.login-msg-yzm {display: flex;width: 100%;}
.login-msg-yzm .el-button {margin-left: 10px;--el-button-size:42px;}
</style>