<template>
	<view class="batchPrint-manualPrint-wrap">
		<view class="batchPrint-manualPrint-body">
			<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>
				共 {{partList.length}}产品
			</view>
			<view>
				<kk-printer ref="kkprinter" defaultText="开始打印" @closeConnect="closeConnect" @startPrint="startPrint"></kk-printer>
			</view>
		</view>
	</view>
</template>

<script>
	import { clzConfirm, numberToFixed } from '@/libs/tools';
	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 {
				basicInfoData:null,
				partList: [],
				allChecked: false,
				printIndex: 0,
				isParinting: false
			}
		},
		onReady() {
			uni.showLoading({
				title: '正在加载...'
			})
			this.$refs.productList.setData(this.partList)
			setTimeout(()=>{
				uni.hideLoading()
				// 默认全选
				if(this.partList.length){
					this.allCheckeChange({value: true})
					this.allChecked = true
				}
			},3000)
		},
		onLoad(option) {
			this.basicInfoData = JSON.parse(decodeURIComponent(option.data));
			this.partList = this.$store.state.vuex_tempPrintList
			// 保持屏幕常亮
			uni.setKeepScreenOn({
				keepScreenOn: true
			});
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: this.$config('primaryColor')
			})
		},
		onUnload() {
			// this.$refs.kkprinter.closeConnect()
			this.partList = []
			// 保持屏幕常亮
			uni.setKeepScreenOn({
				keepScreenOn: false
			});
			this.$store.state.vuex_tempPrintList = []
		},
		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
						uni.hideLoading()
						clzConfirm({
						  title: '提示',
						  content: '打印已经结束,是否返回上页!',
						  success (ret) {
							if (ret.confirm || ret.index == 0) {
								uni.navigateBack()
							} 
						  }
						})
					}
				});
			},
			closeConnect(){
				this.isParinting = false
				uni.hideLoading()
			},
			// 批量打印 
			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
					 uni.showLoading({
					 	mask: true,
						title: '正在打印中,请勿息屏或退出应用!'
					 })
					 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>