<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>
			<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';
	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
			console.log(_this.infoData)
			var 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)
		},
		onUnload() {
			this.$refs.kkprinter.closeConnect()
		},
		methods: {
			cansel(){
				uni.$emit('refreshBL')
				uni.navigateBack()
			},
			textFormat(command,text,maxFontNums,left,top,lightHeight){
				const textLen = text.length
				console.log(textLen,maxFontNums)
				if(textLen > maxFontNums){
					const rows = Math.ceil(textLen/maxFontNums)
					for(let i=0;i<rows;i++){
						top = top+i*lightHeight
						console.log(top)
						// 最后一行
						if(i==rows-1){
							const ltxt = text.substring(i*maxFontNums)
							const ltlen = ltxt.length
							command.setText(left+((maxFontNums-ltlen)/2)*lightHeight, top, "TSS24.BF2", 1, 1, ltxt)
						}else{
							command.setText(left, top, "TSS24.BF2", 1, 1, text.substring(i*maxFontNums,maxFontNums))
						}
					}
				}else{
					command.setText(left+((maxFontNums-textLen)/2)*lightHeight, top, "TSS24.BF2", 1, 1, text)
				}
				return top
			},
			// 打印
			startPrint(opt,tsc,blesdk){
				console.log(opt,'opt')
				let top = 10 // 距离顶部10点像素
				const left = 10 // 距离左边10点
				const lightHeight = 24 // 行高3mm，1mm = 8点
				const pageW = 40 // 页签宽度mm
				const pageH = 30 // 页签高度mm
				const maxFontNums = Math.floor((pageW*8-left*2)/lightHeight) // 每行最多字体数
				// 初始化打印机
				const command = tsc.jpPrinter.createNew()
				command.init()
				command.setSize(pageW, pageH) // 标签纸张宽高，单位mm
				command.setGap(2) // 标签上下间距，单位mm
				command.setCls() // 清除缓冲区数据
				// 经销商文字高度，是否换行
				top = this.textFormat(command,this.printInfo.dealerName,maxFontNums,left,top,lightHeight)
				// 数字货架名称文字
				top = top+lightHeight
				top = this.textFormat(command,this.printInfo.shelfName,Math.floor(maxFontNums*0.6),left,top,lightHeight)
				// 产品编码
				top = top+lightHeight+15
				command.setText(left, top, "TSS24.BF2", 1, 1, this.printInfo.productCode)
				// 产品名称
				top = top+lightHeight
				this.textFormat(command,this.printInfo.productName,Math.floor(maxFontNums*0.6),left,top,lightHeight)
				
				// command.setQR(50, 50, "L", 5, "A", "https://www.baidu.com")
				// command.setBox(5, 5, 795, 595, 1)
				command.setPagePrint(1,1) // 打印分数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: 6rpx 20rpx;
				color: #033692;
				font-size: 24rpx;
			}
		}
	}
	.buttons{
		padding: 50rpx 0;
		> view{
			width: 36%;
			margin: 0 2%;
		}
	}
}
</style>
