<template>
    <div>
        <el-dialog append-to-body
            title="打印物料标签"
            v-model="visible"
            destroy-on-close
            width="800px"
        >
        <el-form>
            <el-form-item label="容器容量">
                <el-input-number 
                v-model="counts" 
                min="1" 
                precision="0"
                style="width: 100%;"
                ></el-input-number >
            </el-form-item>
            <el-form-item label="打印份数">
                <el-input-number  
                v-model="print_count"
                min="1"
                precision="0"
                style="width: 100%;"
                ></el-input-number >
            </el-form-item>
        </el-form>
        <el-footer>
            <el-button type="primary" @click="print">打印</el-button>
        </el-footer>
        </el-dialog>
    </div>
</template>
<script>
export default {
    props:{
        apiObj:{
            type:String,
            default:''
        },
        mId:{
            type:String,
            default:''
        },
        mtype:{
            type:Number,
            default:0
        },
        supplier_name:{
            type:String,
            default:''
        },
        create_time:{
            type:String,
            default:''
        },
        effective_time:{
            type:String,
            default:''
        },
    },
	data(){
        return{
            counts:100,
            print_count:1,
            count_printed:0,
            visible:false,
            printInterval:null,
            printer_name:localStorage.getItem("printer_name")
        }
    },
    mounted(){
        
    },
    methods:{
        open(){
            this.visible = true;
        },
        print(){
            let that = this;
            that.apiObj.req({tid:that.mId}).then((res) => {
				let code = res.code_label;
				let str = [
					"SIZE 40 mm,70 mm",
					"GAP 7 mm,7 mm",
					"CLS",
					"REFERENCE 0,0",
					'QRCODE 30,400,H,5,A,0,"' +code +'"',
					"WINTEXT 30,380,28,90,0,0,Simhei,容器容量:" + that.counts,
					"WINTEXT 200,550,28,90,0,0,Simhei," +"批次号："+ res.material_name,
					"WINTEXT 240,550,28,90,0,0,Simhei," +"数量："  + res.batch,
					]; 
                    //供应商（仅原料、辅料）
                    if(that.mtype==40||that.mtype==30){
                        str.push("WINTEXT 70,380,28,90,0,0,Simhei,供应商:" + that.supplier_name,);
                    }
                    //生产日期/有效日期（仅成品销售发货单中，默认为入成品库日期）
                    if(that.mtype==10){
                        str.push("WINTEXT 70,380,28,90,0,0,Simhei,生产日期:" + that.create_time,);
					    str.push("WINTEXT 110,380,28,90,0,0,Simhei,有效日期:" + that.effective_time,);
                    }
				if(res.notok_sign!==null){
					str.push("WINTEXT 280,550,28,90,0,0,Simhei," +res.notok_sign_name,)
				}
				str.push("PRINT 1",)
				let obj = {};
				obj.printer_commands = str;
				obj.printer_name = that.printer_name;
                if(that.print_count>1){
                    let printerInterval = that.$TOOL.data.get('printerInterval');
                    if(printerInterval==null){
                        let intervalID  = setInterval(() => {
                            that.printLabel(obj)
                        }, 2000);
                        that.visible = false;
                        that.$TOOL.data.set('printerInterval',intervalID );

                    }else{
                        that.$message.warning("请等待当前打印任务完成");
                    }
                }else{
                    let printer_ip=localStorage.getItem("printer_ip")||'127.0.0.1';
that.$API.wpm.prints.req(printer_ip, obj).then((response) => {});
                }
                
			})
        },
        closed(){
            this.visible = false;
        },
        printLabel(obj){
            let that = this;
            let printer_ip=localStorage.getItem("printer_ip")||'127.0.0.1';
that.$API.wpm.prints.req(printer_ip, obj).then((response) => {
                that.count_printed ++
                if (that.count_printed >= that.print_count){
                    let printerInterval = that.$TOOL.data.get('printerInterval');
                    clearInterval(printerInterval);
                    that.$TOOL.data.set('printerInterval',null);
                }
            });
        }
    },
    beforeUnmount() {
        // 清除定时器
        clearInterval(this.$INTERVAL);
    },
}
</script>