<template>
	<view class="printTag">
		<view class="print-nums flex flex_column align_center justify_center">
			<view>打印数量</view>
			<view class="u-ninput">
				<u-number-box :min="1" v-model="printNum" color="#000" bg-color="#fff" size="30" :input-height="70" :input-width="100"></u-number-box>
			</view>
		</view>
		<view class="shelf-info">
			<view class="shelf-info-title">
				<view>{{infoData&&infoData.shelfName || '--'}}</view>
				<view class="pno">{{infoData&&infoData.shelfPlaceCode}}</view>
			</view>
			<view>产品编码：{{fromPage == 'smdy'?infoData.productCode : infoData.productEntity&&infoData.productEntity.code}}</view>
			<view>产品名称：{{fromPage == 'smdy'?infoData.productName : infoData.productEntity&&infoData.productEntity.productName}}</view>
			<view>{{fromPage == 'smdy'?'实发':'库存'}}数量：{{fromPage == 'smdy'?infoData.confirmQty : infoData.qty}}</view>
		</view>
		<view class="buttons flex align_center justify_center">
			<view v-if="fromPage == 'bdtq'"><u-button @click="cansel" shape="circle" plain>返回列表</u-button></view>
			<view v-if="fromPage == 'smdy'"><u-button @click="cansel" shape="circle" plain>重新扫描</u-button></view>
			<view>
				<kk-printer ref="kkprinter" @startPrint="startPrint"></kk-printer>
			</view>
		</view>
	</view>
</template>

<script>
	import { shelfDetail } from '@/api/shelf'
	import kkPrinter from '@/components/kk-printer/index.vue';
	import {textFormat} from '@/libs/printTools.js'
	export default {
		components:{
			kkPrinter
		},
		data() {
			return {
				infoData: null,
				fromPage: '',
				printInfo: null,
				printNum: 0
			}
		},
		onLoad(options) {
			const _this = this
			_this.fromPage = options.page
			_this.infoData = options.data ? JSON.parse(options.data) : {},
			_this.printNum = _this.fromPage == 'smdy' ? _this.infoData.confirmQty : _this.infoData.qty
			_this.printNum = _this.printNum||1
			console.log(_this.infoData)
		},
		onUnload() {
			// this.$refs.kkprinter.closeConnect()
			this.printNum = 1
			this.infoData = null
			this.printInfo = null
			this.fromPage = ''
		},
		methods: {
			cansel(){
				uni.$emit('refreshBL')
				uni.navigateBack()
			},
			// 获取打印内容
			getPrintContent(){
				const _this = this
				const myDate = new Date()
				if(_this.fromPage == 'bdtq'){
					_this.printInfo = {
						dealerName: _this.$store.state.vuex_userData.orgName,
						shelfName: _this.infoData.shelfName || '',
						productCode: _this.infoData.productEntity&&_this.infoData.productEntity.code || '',
						productName: _this.infoData.productEntity&&_this.infoData.productEntity.productName || '',
						shelfPlaceCode: _this.infoData.shelfPlaceCode || '',
						currentInven: _this.printNum,
						printDate: myDate.toLocaleDateString() +' '+myDate.getHours() +':'+myDate.getMinutes() +':'+myDate.getSeconds(),
						printUser: _this.$store.state.vuex_userData.username,
						barCode: `dealerSn=${_this.$store.state.vuex_userData.orgSn}&shelfSn=${_this.infoData.shelfSn}&productSn=${_this.infoData.productEntity&&_this.infoData.productEntity.productSn}&productCode=${_this.infoData.productEntity&&_this.infoData.productEntity.code}&shelfPlaceCode=${_this.infoData.shelfPlaceCode}&shelfPlaceSn=${_this.infoData.shelfPlaceSn}`
					}
				}else if(_this.fromPage == 'smdy'){
					_this.printInfo = {
						dealerName: _this.$store.state.vuex_userData.orgName,
						shelfName: _this.infoData.shelfName || '',
						productCode: _this.infoData.productCode || '',
						productName: _this.infoData.productName || '',
						shelfPlaceCode: _this.infoData.shelfPlaceCode || '',
						currentInven: _this.printNum,
						printDate: myDate.toLocaleDateString() +' '+myDate.getHours() +':'+myDate.getMinutes() +':'+myDate.getSeconds(),
						printUser: _this.$store.state.vuex_userData.username,
						barCode: `dealerSn=${_this.$store.state.vuex_userData.orgSn}&shelfSn=${_this.infoData.shelfSn}&productSn=${_this.infoData.productSn}&productCode=${_this.infoData.productCode}&shelfPlaceCode=${_this.infoData.shelfPlaceCode}&shelfPlaceSn=${_this.infoData.shelfPlaceSn}`
					}
				}
				console.log('printInfo-----------',_this.printInfo)
			},
			// 打印
			startPrint(opt,tsc,blesdk){
				console.log(opt,'opt')
				this.getPrintContent()
				if(this.printInfo.currentInven<=0){
					uni.showToast({
						icon:"none",
						title: "请选择打印数量"
					})
					this.$refs.kkprinter.onPrintFail()
					return
				}
				let top = 10 // 距离顶部10点像素
				const left = 8 // 距离左边
				const lightHeight = 24 // 行高3mm，1mm = 8点
				const pageW = 40 // 页签宽度mm
				const pageH = 30 // 页签高度mm
				const maxFontNums = Math.floor((pageW*8-left*2)/lightHeight) // 每行最多字体数
				let rightTop = 0
				let rightLeft = Math.floor(maxFontNums*0.6)*lightHeight
				// 初始化打印机
				const command = tsc.jpPrinter.createNew()
				command.init()
				command.setSize(pageW, pageH) // 标签纸张宽高，单位mm
				command.setGap(2) // 标签上下间距，单位mm
				command.setCls() // 清除缓冲区数据
				// 经销商文字高度，是否换行
				top = textFormat(command,this.printInfo.dealerName,maxFontNums,left,top,lightHeight,"center",1)
				// 数字货架名称文字
				top = top+10
				rightTop = top
				const lwidth = Math.floor(maxFontNums*0.6)
				top = textFormat(command,this.printInfo.shelfName,lwidth,left,top,lightHeight,"left",1)
				// 产品编码
				top = top+10
				top = textFormat(command,this.printInfo.productCode,lwidth,left,top,lightHeight,"left",1)
				// 产品名称
				top = top+5
				top = textFormat(command,this.printInfo.productName,lwidth,left,top,lightHeight,"left",1)
				// 货位号
				textFormat(command,this.printInfo.shelfPlaceCode,maxFontNums-lwidth,rightLeft,rightTop,lightHeight,"center",1)
				// 二维码
				const qrtop = rightTop +lightHeight+10
				command.setQR(rightLeft, qrtop, "L", 3, "A", this.printInfo.barCode)
				command.setPagePrint(1,this.printInfo.currentInven) // 打印分数1，每个标签重发打印2次
				console.log("开始打印了",command.getData())
				blesdk.senBlData(opt.deviceId, opt.serviceId, opt.writeId, command.getData(), this.onPrintSuccess);
			},
			// 打印成功
			onPrintSuccess(){
				// uni.navigateBack()
				this.$refs.kkprinter.onPrintSuccess()
			}
		}
	}
</script>

<style lang="less">
.printTag{
	background-color: #fff;
	width: 100vh;
	padding: 50rpx 30rpx;
	.print-nums{
		padding: 50rpx 30rpx;
		color: #999;
		font-size: 32rpx;
		> view{
			padding: 20rpx 0;
		}
		.u-ninput{
			border: 2rpx solid #eee;
			border-radius: 50rpx;
			padding: 0 12rpx;
		}
		/deep/ .u-icon-disabled{
			background: #fff!important;
		}
		/deep/ .u-number-input{
			margin: 0 2rpx;
			border: 2rpx solid #eee;
			border-top: 0;
			border-bottom: 0;
		}
		/deep/ .u-icon-plus, /deep/ .u-icon-minus{
			border-radius: 50rpx;
		}
	}
	.shelf-info{
		background-color: #f8f8f8;
		padding: 30rpx;
		border-radius: 12rpx;
		color: #707070;
		.shelf-info-title{
			font-size: 32rpx;
			color: #222222;
			display: flex;
			justify-content: space-between;
			margin-bottom: 10rpx;
			.pno{
				background: rgba(3, 54, 146, 0.15);
				border-radius: 100rpx;
				padding: 4rpx 20rpx;
				color: #033692;
				font-size: 26rpx;
			}
		}
	}
	.buttons{
		padding: 50rpx 0;
		> view{
			width: 36%;
			margin: 0 2%;
		}
	}
}
</style>
