<template>
	<el-dialog append-to-body
		title="标签信息"
		v-model="visible"
		:size="1000"
		destroy-on-close
		@closed="$emit('closed')"
	>
		<el-form
			ref="dialogForm"
			:model="form"
			:rules="rules"
			label-width="130px"
			v-if="type=='in'"
		>
			<el-form-item label="类型">
				<el-radio-group v-model="packtype">
					<el-radio label="国内">国内</el-radio>
					<el-radio label="国外">国外</el-radio>
				</el-radio-group>
			</el-form-item>
			<template v-if="packtype == '国外'">
				<el-form-item label="Drawing Number" prop="drawingNumber">
					<el-input v-model="form.drawingNumber" placeholder="图纸编号" />
				</el-form-item>
				<el-form-item label="Serial Number" prop="serialNumber">
					<el-input v-model="form.serialNumber" placeholder="序列号" />
				</el-form-item>
			</template>
			<template v-if="packtype == '国内'">
				<el-form-item label="名称">
					<el-input v-model="form.name" placeholder="名称" />
				</el-form-item>
				<el-form-item label="型号">
					<el-input v-model="form.model" placeholder="型号" />
				</el-form-item>
				<el-form-item label="批号">
					<el-input v-model="form.batch" placeholder="批号" />
				</el-form-item>
				<el-form-item label="数量">
					<el-input v-model="form.count" placeholder="数量" />
				</el-form-item>
			</template>
		</el-form>
		<el-form
			ref="dialogForm"
			:model="form"
			:rules="rules"
			label-width="130px"
			v-if="type=='out'"
		>
			<el-form-item label="类型">
				<el-radio-group v-model="packtype">
					<el-radio label="国内">国内</el-radio>
					<el-radio label="国外">国外</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="日期">
				<el-date-picker
					v-model="form.date"
					type="date"
					placeholder="日期"
					value-format="YYYY-MM-DD"
					style="width: 100%"
				></el-date-picker>
			</el-form-item>
			<template v-if="packtype == '国外'">
				<el-form-item label="Item" prop="name">
					<el-input v-model="form.name" placeholder="名称" />
				</el-form-item>
				<el-form-item label="Drawing Number" prop="drawingNumber">
					<el-input v-model="form.drawingNumber" placeholder="图纸编号" />
				</el-form-item>
				<el-form-item label="Serial Number" prop="serialNumber">
					<el-input v-model="form.serialNumber" placeholder="序列号" />
				</el-form-item>
			</template>
			<template v-if="packtype == '国内'">
				<el-form-item label="名称">
					<el-input v-model="form.name" placeholder="名称" />
				</el-form-item>
				<el-form-item label="型号">
					<el-input v-model="form.model" placeholder="型号" />
				</el-form-item>
				<el-form-item label="数量">
					<el-input v-model="form.count" placeholder="数量" />
				</el-form-item>
				<el-form-item label="批号">
					<el-input v-model="form.batch" placeholder="批号" />
				</el-form-item>
				<el-form-item label="本次发货共">
					<el-input-number v-model="form.packNum" placeholder="数量" />箱
					<el-input v-model="form.pack" placeholder="数量" style="width: 60%;"/>
				</el-form-item>
			</template>
		</el-form>
		<template #footer>
			<el-button type="primary" :loading="isSaveing" @click="submit">确定</el-button>
			<el-button @click="visible = false">取消</el-button>
		</template>
	</el-dialog>
</template>
<script>
export default {
	emits: ["success", "closed"],
	props: {
		type: { type: String, default: "" },
		mioObj: { type: Object },
	},
	data() {
		return {
			packtype: "国内",
			loading: false,
			mode: "add",
			form: {
				drawingNumber: "",
				serialNumber: "",
				name: "",
				model: "",
				batch: "",
				count:"",
			},
			rules: {
				number: [
					{
						required: true,
						message: "请填写编号",
						trigger: "blur",
					},
				],
			},
			visible: false,
			isSaveing: false,
			options: [],
			printer_name:localStorage.getItem("printer_name"),
		};
	},
	mounted() {
		let that = this;
		that.$nextTick(() => {
		    if(that.type=="in"){//里面的小箱子
				that.form.name = that.mioObj.material_?that.mioObj.material_.name:'';
				that.form.model = that.mioObj.material_?that.mioObj.material_.model:'';
				that.form.batch = that.mioObj.batch;
				that.form.count = that.mioObj.count;
			}else{//外面的大箱子
				let items = that.mioObj.items;
				that.form.name = items[0].material_.name;
				that.form.model = items[0].material_.model;
				that.form.batch = items[0].batch;
			}
		})
	},
	methods: {
		//显示
		open(index,total) {
			let that = this;
			let date = new Date();
			that.form.date = that.$TOOL.dateFormat2(date);
			if(index&&total){
				that.form.packNum = total;
				that.form.pack = index+'/'+total;
				that.$nextTick(() => {
					let totalCount = 0;
					let items = that.mioObj.items;
					that.form.name = items[0].material_.name;
					that.form.model = items[0].material_.model;
					that.form.batch = items[0].batch;
					items.forEach(item=>{
						if(item.pack_index==index){
							totalCount += Number(item.count);
						}
					})
					that.form.count = totalCount;
				})
			}
			this.visible = true;
			return this;
		},
		//提交
		submit() {
			let that = this;
			that.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					let commands = [];
					if(that.type == "in"){
						if(that.packtype == "国内"){
							commands = [
								"SIZE 70 mm,100 mm",
								"GAP 7 mm,7 mm",
								"CLS",
								"REFERENCE 0,0",
								"WINTEXT 20,750,36,90,0,0,Simhei,中国建筑材料科学研究总院-玻纤院",
								"WINTEXT 80,750,36,90,0,0,Simhei,名称",
								"WINTEXT 80,600,36,90,0,0,Simhei," + this.form.name,
								"WINTEXT 130,750,36,90,0,0,Simhei,批号",
								"WINTEXT 130,600,36,90,0,0,Simhei," + this.form.batch,
								"WINTEXT 180,750,36,90,0,0,Simhei,型号" ,
								"WINTEXT 180,600,36,90,0,0,Simhei," + this.form.model,
								"WINTEXT 230,750,36,90,0,0,Simhei,数量" ,
								"WINTEXT 230,600,36,90,0,0,Simhei," + this.form.count,
								"PRINT 1",
							];
						}else{
							commands = [
								"SIZE 70 mm,100 mm",
								"GAP 7 mm,7 mm",
								"CLS",
								"REFERENCE 0,0",
								"WINTEXT 50,760,36,90,0,0,Simhei,Drawing Number",
								"WINTEXT 50,500,36,90,0,0,Simhei," + this.form.drawingNumber,
								"WINTEXT 110,760,36,90,0,0,Simhei,Serial Number",
								"WINTEXT 110,500,36,90,0,0,Simhei," + this.form.serialNumber,
								"PRINT 1",
							];
						}
					}else if(that.type == "out"){
						if(that.packtype == "国内"){
							commands = [
								"SIZE 70 mm,100 mm",
								"GAP 7 mm,7 mm",
								"CLS",
								"REFERENCE 0,0",
								"WINTEXT 20,750,36,90,0,0,Simhei,装箱单（"+this.form.date+"）",
								"WINTEXT 80,750,36,90,0,0,Simhei,名称",
								"WINTEXT 80,600,36,90,0,0,Simhei," + this.form.name,
								"WINTEXT 130,750,36,90,0,0,Simhei,批号",
								"WINTEXT 130,600,36,90,0,0,Simhei," + this.form.model,
								"WINTEXT 180,750,36,90,0,0,Simhei,数量" ,
								"WINTEXT 180,600,36,90,0,0,Simhei," + this.form.count,
								"WINTEXT 230,750,36,90,0,0,Simhei,型号" ,
								"WINTEXT 230,600,36,90,0,0,Simhei," + this.form.batch,
								"WINTEXT 280,750,36,90,0,0,Simhei,本次发货",
								"WINTEXT 320,750,36,90,0,0,Simhei,共"+this.form.packNum+"箱" ,
								"WINTEXT 280,600,36,90,0,0,Simhei," + this.form.pack,
								"WINTEXT 330,750,36,90,0,0,Simhei,中国建筑材料科学研究总院--玻纤院",
								"PRINT 1",
							];
						}else{
							commands = [
								"SIZE 70 mm,100 mm",
								"GAP 7 mm,7 mm",
								"CLS",
								"REFERENCE 0,0",
								"WINTEXT 20,750,36,90,0,0,Simhei,Packing List（"+this.form.date+"）",
								"WINTEXT 80,760,36,90,0,0,Simhei,Item",
								"WINTEXT 80,500,36,90,0,0,Simhei," + this.form.name,
								"WINTEXT 130,760,36,90,0,0,Simhei,Drawing Number",
								"WINTEXT 130,500,36,90,0,0,Simhei," + this.form.drawingNumber,
								"WINTEXT 180,760,36,90,0,0,Simhei,Serial Number",
								"WINTEXT 180,500,36,90,0,0,Simhei," + this.form.serialNumber,
								"PRINT 1",
							];
						}
					}
					console.log('commands',commands);
					let obj = {};
					obj.printer_commands = commands;
					obj.printer_name = that.printer_name;
					let printer_ip=localStorage.getItem("printer_ip")||'127.0.0.1';
that.$API.wpm.prints.req(printer_ip, obj).then((response) => {
						that.$message.success("打印成功");
						that.isSaveing = false;
						that.$emit("success");
						that.visible = false;
					});
				}
			});
		},
		//表单注入数据
		setData(data) {
			Object.assign(this.form, data);
		},
		//设置过滤项
		setFilters(filters) {
			this.selectionFilters = filters;
			this.setFiltersVisible = true;
		},
	},
};
</script>
<style></style>
