<template>
	<view class="printTag">
		<u-alert-tips :show="show" @close="show = false" type="warning" description="正在打印中,请勿息屏或退出应用!"></u-alert-tips>
		<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="print-radio" v-if="fromPage == 'bdtq'">
			<u-radio-group v-model="numType" @change="qtyChange">
				<u-radio :name="1">按当前库存打印</u-radio>
				<u-radio :name="2">按最大库容打印</u-radio>
			</u-radio-group>
		</view>
		<view class="shelf-info">
			<view class="shelf-info-title">
				<view class="shelf-name">{{infoData&&infoData.shelfName || '--'}}</view>
				<view class="pno">{{infoData&&infoData.shelfPlaceCode}}</view>
			</view>
			<view class="shelf-info-body">
				<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}}{{infoData.productEntity&&infoData.productEntity.unit}}</view>
				<view v-if="fromPage == 'bdtq'">最大库容：{{infoData.maxQty}}{{infoData.productEntity&&infoData.productEntity.unit}}</view>
			</view>
		</view>
		<view class="buttons flex align_center justify_center">
			<view v-if="fromPage == 'bdtq'"><u-button @click="cancel" shape="circle" plain>返回列表</u-button></view>
			<view v-if="fromPage == 'smdy'"><u-button @click="cancel" shape="circle" plain>重新扫描</u-button></view>
			<view>
				<kk-printer @closeConnect="closeConnect" ref="kkprinter" @startPrint="startPrint"></kk-printer>
			</view>
		</view>
	</view>
</template>

<script>
	import { clzConfirm, numberToFixed } from '@/libs/tools';
	import { shelfDetail } from '@/api/shelf'
	import kkPrinter from '@/components/kk-printer/index.vue';
	import {printTempl,printCpclTempl} from '@/libs/printTools.js'
	import moment from 'moment'
	export default {
		components:{
			kkPrinter
		},
		data() {
			return {
				show: false,
				infoData: null,
				fromPage: '',
				printInfo: null,
				printNum: 1,
				isParinting: false,
				numType: 2
			}
		},
		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.maxQty
			_this.printNum = _this.printNum||1
			console.log(_this.infoData)
			// 保持屏幕常亮
			uni.setKeepScreenOn({
				keepScreenOn: true
			});
		},
		onUnload() {
			// this.$refs.kkprinter.closeConnect()
			this.printNum = 1
			this.infoData = null
			this.printInfo = null
			this.fromPage = ''
			// 保持屏幕常亮
			uni.setKeepScreenOn({
				keepScreenOn: false
			});
		},
		// 监听页面返回，返回 event = {from:backbutton、 navigateBack} ，backbutton 表示来源是左上角返回按钮或 android 返回键；navigateBack表示来源是 uni.navigateBack
		onBackPress(event){
			if(event.from == 'backbutton'){
				if(this.isParinting){
					uni.showToast({
						icon:'none',
						title: '正在打印中...'
					})
				}else{
					this.cancel()
				}
				return true  // 阻止默认返回行为(会导致无限循环)
			}
		},
		methods: {
			qtyChange(v){
				this.printNum = v == '1' ? this.infoData.qty : this.infoData.maxQty
			},
			cancel(){
				if(this.fromPage == 'bdtq'){  // 补打贴签
					uni.navigateBack({delta: 1})
				}else if(this.fromPage == 'smdy'){  // 扫码打印
					uni.$emit('refreshBL')
					uni.navigateBack({delta: 1})
				}
			},
			// 获取打印内容
			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: `${_this.infoData.shelfSn}&${_this.infoData.productEntity&&_this.infoData.productEntity.code}&${_this.infoData.productEntity&&_this.infoData.productEntity.productSn}&${_this.infoData.shelfPlaceSn}`
						// 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: `${_this.infoData.shelfSn}&${_this.infoData.productCode}&${_this.infoData.productSn}&${_this.infoData.shelfPlaceSn}`
						// 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)
			},
			closeConnect(){
				this.isParinting = false
				uni.hideLoading()
			},
			// 打印
			startPrint(opt,cmd,blesdk){
				console.log(opt,cmd,'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
				this.show = true
				// tsc 指令
				if(cmd.type&&cmd.type=='tsc'){
					const command = printTempl(cmd,this.printInfo)
					blesdk.senBlData(opt.deviceId, opt.serviceId, opt.writeId, command.getData(), this.onPrintSuccess);
				}else{
					const resultData = printCpclTempl(cmd,this.printInfo)
					blesdk.sendDataToDevice({
										deviceId: opt.deviceId, 
										serviceId: opt.serviceId, 
										characteristicId: opt.writeId,
										value: resultData,
										lasterSuccess: this.onPrintSuccess,
										lasterError: this.onPrintError
									});
				}
			},
			// 打印成功
			onPrintSuccess(){
				this.$refs.kkprinter.onPrintSuccess()
				this.isParinting = false
				this.show = false
				clzConfirm({
				  title: '提示',
				  content: '打印已经结束，是否返回上页！',
				  success (ret) {
					if (ret.confirm || ret.index == 0) {
						uni.navigateBack()
					} 
				  }
				})
			},
			onPrintError(){
				this.$refs.kkprinter.onPrintFail()
				this.isParinting = false
				this.show = false
				uni.showToast({
					title: '打印失败，请重试！'
				})
			}
		}
	}
</script>

<style lang="less">
.printTag{
	background-color: #fff;
	width: 100vh;
	padding: 50rpx 30rpx;
	.print-nums{
		padding: 50rpx 30rpx 10rpx;
		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;
		}
	}
	.print-radio{
		padding: 20rpx 20rpx 50rpx;
		text-align: center;
	}
	.shelf-info{
		background-color: #f8f8f8;
		border-radius: 12rpx;
		color: #707070;
		.shelf-info-title{
			font-size: 30rpx;
			color: #222222;
			display: flex;
			justify-content: space-between;
			border-bottom: 1rpx solid #eee;
			padding: 15rpx 30rpx;
			font-weight: bold;
			.shelf-name{
				flex:1;
				margin-right: 10rpx;
			}
			.pno{
				background: rgba(3, 54, 146, 0.15);
				border-radius: 100rpx;
				padding: 0 20rpx;
				color: #033692;
				font-size: 26rpx;
				height: 40rpx;
				margin-top:5rpx;
			}
		}
		.shelf-info-body{
			padding: 15rpx 30rpx;
			> view{
				padding: 5rpx 0;
			}
		}
	}
	.buttons{
		padding: 50rpx 0;
		> view{
			width: 36%;
			margin: 0 2%;
		}
	}
}
</style>
