<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 {printTempl} from '@/libs/printTools.js'
	import moment from 'moment'
	export default {
		components:{
			kkPrinter
		},
		data() {
			return {
				infoData: null,
				fromPage: '',
				printInfo: null,
				printNum: 0,
				isParinting: false
			}
		},
		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 nowDate = moment().format('YYYY-MM-DD HH:mm')
				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: nowDate,
						printUser: _this.$store.state.vuex_userData.userNameCN,
						barCode: `shelfSn=${_this.infoData.shelfSn}&productCode=${_this.infoData.productEntity&&_this.infoData.productEntity.code}`
						// 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: nowDate,
						printUser: _this.$store.state.vuex_userData.userNameCN,
						barCode: `shelfSn=${_this.infoData.shelfSn}&productCode=${_this.infoData.productCode}`
						// 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
				}
				if(this.isParinting){
					return
				}
				this.isParinting = true
				const command = printTempl(tsc,this.printInfo)
				blesdk.senBlData(opt.deviceId, opt.serviceId, opt.writeId, command.getData(), this.onPrintSuccess);
			},
			// 打印成功
			onPrintSuccess(){
				// uni.navigateBack()
				this.$refs.kkprinter.onPrintSuccess()
				this.isParinting = false
			}
		}
	}
</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>
