<template>
	<view class="batchPrint-manualPrint-wrap">
		<view class="batchPrint-manualPrint-body">
			<u-alert-tips :show="show" @close="show = false" type="warning" description="正在打印中,请勿息屏或退出应用!"></u-alert-tips>
			<view class="part-list">
				<!-- 产品 -->
				<productList :list="partList" title="产品列表" ref="productList" noDataText="暂无产品" @allChecked="allCheckedCallback"></productList>
			</view>
		</view>
		<view class="batchPrint-manualPrint-footer">
			<view>
				<u-checkbox size="40" @change="allCheckeChange" v-model="allChecked" shape="circle">{{allChecked?'取消全选':'全选'}}</u-checkbox>
			</view>
			<view>
				<kk-printer ref="kkprinter" defaultText="开始打印" @startPrint="startPrint"></kk-printer>
			</view>
		</view>
	</view>
</template>

<script>
	import kkPrinter from '@/components/kk-printer/index.vue';
	import productList from './productList.vue'
	import {printTempl} from '@/libs/printTools.js'
	export default {
		components: { productList, kkPrinter },
		data() {
			return {
				show: false,
				basicInfoData:null,
				partList: [],
				allChecked: false,
				printIndex: 0,
				isParinting: false
			}
		},
		onReady() {
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: this.$config('primaryColor')
			})
			this.$refs.productList.setData(this.partList)
		},
		onLoad(option) {
			this.basicInfoData = JSON.parse(decodeURIComponent(option.data));
			this.partList = this.basicInfoData.list
			// 保持屏幕常亮
			uni.setKeepScreenOn({
				keepScreenOn: true
			});
		},
		onUnload() {
			// this.$refs.kkprinter.closeConnect()
			this.partList = []
			// 保持屏幕常亮
			uni.setKeepScreenOn({
				keepScreenOn: false
			});
		},
		onBackPress(event){
			if(event.from == 'backbutton'){
				if(this.isParinting){
					uni.showToast({
						icon:'none',
						title: '正在打印中...'
					})
				}else{
					uni.navigateBack({delta: 1})
				}
				return true  // 阻止默认返回行为(会导致无限循环)
			}
		},
		methods: {
			// 全选
			allCheckeChange(e){
				this.$refs.productList.allSelect(e.value)
			},
			allCheckedCallback(val){
				this.allChecked = val
			},
			printOnce(opt,tsc,blesdk,data){
				const _this = this
				const dealer = this.$store.state.vuex_userData
				// 60*40 打印模板
				const command = printTempl(tsc,{
					dealerName: dealer.orgName,
					shelfName: data.shelfName || '',
					productCode: data.productCode || '',
					productName: data.productName || '',
					shelfPlaceCode: data.shelfPlaceCode || '',
					currentInven: data.printQty,
					printDate: this.$u.timeFormat(this.timestamp, 'yyyy-mm-dd hh:MM'),
					printUser: dealer.userNameCN,
					barCode: `${data.shelfSn}&${data.productCode}&${data.productSn}&${data.shelfPlaceSn}`
				})
				// 开始批量打印 
				blesdk.senBlData(
				opt.deviceId, 
				opt.serviceId, 
				opt.writeId, 
				command.getData(), 
				function(){
					const result =_this.$refs.productList.getAllChecked()
					_this.printIndex = _this.printIndex + 1
					if(_this.printIndex < result.length){
						_this.printOnce(opt,tsc,blesdk,result[_this.printIndex])
					}else{
						_this.printIndex = 0
						_this.$refs.kkprinter.onPrintSuccess()
						_this.isParinting = false
						_this.show = false
					}
				});
			},
			// 批量打印 
			startPrint(opt,tsc,blesdk){
				const result =this.$refs.productList.getAllChecked()
				console.log(result,result.length)
				if(result.length){
					 if(this.isParinting){
					 	return
					 }
					 this.isParinting = true
					 this.show = true
					 this.printOnce(opt,tsc,blesdk,result[this.printIndex])
				}else{
					this.toashMsg("请选择产品！")
					this.$refs.kkprinter.onPrintFail()
				}
			},
		}
	}
</script>

<style lang="less">
.batchPrint-manualPrint-wrap{
	position: relative;
	width: 100%;
	height: 100%;
	overflow: hidden;
	padding-bottom: 102upx;
	.p-title{
		padding: 20rpx;
		display: flex;
		align-items: center;
		color: #222;
		font-size: 32rpx;
		.shelfName{
			width: 80%;
			flex-grow: 1;
			font-weight: bold;
			text{
				display: block;
				height: 30rpx;
				width: 6rpx;
				background: #0485F6;
				margin-right: 10rpx;
				border-radius: 10rpx;
			}
		}
		.btns{
			width:100rpx;
			text-align: right;
			font-size: 28rpx;
		}
	}
	.batchPrint-manualPrint-body{
		.part-list{
			padding: 10rpx 25rpx;
			background-color: #fff;
			margin-bottom: 20rpx;
			border-radius: 25rpx;
			box-shadow: 2rpx 3rpx 5rpx #eee;
		}
	}
	.batchPrint-manualPrint-footer{
		padding: 10upx 32upx 12upx;
		background-color: #fff;
		position: fixed;
		left: 0;
		bottom: 0;
		width: 100%;
		box-shadow: 3px 1px 7px #eee;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}
}
</style>
