<template>
	<view class="scanCodePrint-wrap">
		<view class="barCode" id="barcode"></view>
		<view class="info-body">
			<view class="info partList">
				<!-- 补货产品 -->
				<partList :list="partList" title="补货产品" model="view" fromPage="scanCodePrint" ref="partList" noDataText="暂无产品"></partList>
			</view>
		</view>
		<!-- 确认弹框 -->
		<common-modal v-if="confirmModal" :openModal="confirmModal" title="扫码失败" content="抱歉，该产品不是此补货单的产品请更换产品重新扫描？" confirmText="好的" :isCancel="false" @confirm="modalConfirm" @close="confirmModal=false" />
	</view>
</template>

<script>
	import partList from '@/pages/common/partList.vue'
	import commonModal from '@/pages/common/commonModal.vue'
	import { shelfReplenishDetailList, shelfReplenishQrCode, shelfReplenishDetailOutScan } from '@/api/shelfReplenish'
	export default {
		components: { partList, commonModal },
		data() {
			return {
				barcode:null,
				replenishBillSn: null,
				partList: [],
				confirmModal: false
			}
		},
		onReady() {
			// 初始化摄像头
			this.init()
		},
		onLoad(options) {
			this.replenishBillSn = options.sn
			this.getPartList()
			// 监听整改完成后刷新事件
			uni.$on('refreshBL', this.modalConfirm)
		},
		onUnload() {
			uni.$off('refreshBL')
		},
		methods: {
			// 查询列表
			getPartList(){
				const _this = this
				shelfReplenishDetailList({ replenishBillSn: this.replenishBillSn }).then(res => {
					if(res.status == 200 && res.data){
						res.data.map(item =>{
							item.confirmQty = item.confirmQty ? Number(item.confirmQty) : 0
						})
						this.partList = res.data || []
					}else{
						this.partList = []
					}
				})
			},
			// 扫码结果
			scanResult(qrCode){
				const _this = this
				uni.showLoading({
					title: "正在查询产品信息"
				})
				console.log({
					productQrCode: qrCode,
					replenishBillSn: this.replenishBillSn
				})
				shelfReplenishQrCode({
					productQrCode: qrCode,
					replenishBillSn: this.replenishBillSn
				}).then(res => {
					console.log(res)
					if(res.status == 200&&res.data.length){
						let params = {
							shelfName: res.data&&res.data[0]&&res.data[0].shelf&&res.data[0].shelf.shelfName,
							productName: res.data&&res.data[0]&&res.data[0].product&&res.data[0].product.name,
							productCode: res.data&&res.data[0]&&res.data[0].product&&res.data[0].product.code,
							productSn: res.data&&res.data[0]&&res.data[0].product&&res.data[0].product.productSn,
							confirmQty: res.data&&res.data[0]&&res.data[0].confirmQty,
							shelfSn: res.data&&res.data[0]&&res.data[0].shelfSn,
							replenishBillDetailSn: res.data&&res.data[0]&&res.data[0].replenishBillDetailSn,
							shelfPlaceCode: res.data&&res.data[0]&&res.data[0].shelfPlaceCode,
							shelfPlaceSn: res.data&&res.data[0]&&res.data[0].shelfPlaceSn
						}
						uni.navigateTo({ url: "/pages/common/printTag/printTag?page=smdy&data="+JSON.stringify(params) })
					}else{
						this.confirmModal = true
					}
					uni.hideLoading()
				})
			},
			modalConfirm(){
				this.confirmModal = false
				this.barcode.start()
			},
			init(){
				const _this = this
				// 初始化
				this.barcode = plus.barcode.create('barcode', [], {
					top:'0px',
					left:'0px',
					width: '100%',
					height: '28%',
					position: 'static',
					frameColor: '#00aaff',
					scanbarColor: '#00aaff'
				})
				// 设置高度
				const query = uni.createSelectorQuery().in(this);
				query.select('#barcode').boundingClientRect(data => {
					this.barcode.setStyle({
						height: data.height	+ 'px'	// 调整扫码控件的位置
					})
				}).exec()
				 
				// 扫码成功后
				this.barcode.onmarked = function(type, result) {
					_this.scanResult(result)
				}
				// 扫码识别出错
				this.barcode.onerror = function(error){
					console.log(error)
					_this.toashMsg("条形码错误！")
					_this.barcode.start()
				}
				
				 const currentWebview = this.$mp.page.$getAppWebview()
				 currentWebview.append(this.barcode)
				 this.barcode.start()
			}
		}
	}
</script>

<style lang="less">
	page{
		height: 100vh;
	}
	.scanCodePrint-wrap{
		width: 100%;
		height: 100%;
		.barCode{
			height: 28%;
		}
		.info-body{
			flex-flow: 1;
			overflow: auto;
			height: 72%;
		}
		.info{
			background-color: #FFFFFF;
			padding: 10rpx 30upx;
			font-size: 32rpx;
			margin-top: 20rpx;
		}
	}
</style>
