<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>
		<view class="sendOutGoods-footer">
			<u-button @click="scanCode" type="success" :custom-style="customStyle" hover-class="none" shape="circle">
				<u-icon name="scan" size="34"></u-icon>
				点击扫码
			</u-button>
		</view>
		<!-- 确认弹框 -->
		<common-modal v-if="confirmModal" :openModal="confirmModal" :title="contTitle?'扫码成功':'扫码失败'" :content="contModal" confirmText="继续扫码" @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: [],
				contTitle: '',
				contModal: '',
				confirmModal: false,
				mpaasScanModule: null,
				customStyle: {
					borderRadius:'100rpx',
					fontSize:'30rpx',
					background: this.$config('primaryColor')
				},
			}
		},
		onReady() {
			// 初始化摄像头
			// this.init()
		},
		onLoad(options) {
			this.replenishBillSn = options.sn
			this.getPartList()
			// 监听整改完成后刷新事件
			uni.$on('refreshBL', this.modalConfirm)
		},
		onUnload() {
			uni.$off('refreshBL')
		},
		methods: {
			// 扫码
			scanCode(){
				// this.mpaasScanModule = uni.requireNativePlugin("Mpaas-Scan-Module")
				// this.mpaasScanModule.mpaasScan({
				// 	// 扫码识别类型，参数可多选，qrCode、barCode，不设置，默认识别所有
				// 	'scanType':  ['qrCode','barCode'],
				// 	// 是否隐藏相册，默认false不隐藏
				// 	'hideAlbum': false
				// },
				// (ret) => {
				// 	// 返回值中，resp_code 表示返回结果值，10：用户取消，11：其他错误，1000：成功
				// 	// 返回值中，resp_message 表示返回结果信息
				// 	// 返回值中，resp_result 表示扫码结果，只有成功才会有返回
				// 	if(ret.resp_code == '1000'){
				// 		this.scanResult(ret.resp_result)
				// 	}
				// })
				this.mpaasScanModule = uni.requireNativePlugin("wss-scan")
				this.mpaasScanModule.scan(
					{
						"scanType":["CODABAR","CODE93","CODE39","CODE128","EAN13","EAN8","ITF14","UPCCODE_A","UPCCODE_E"],
						"scanMode":"Customized",
						"scanStyle":{
							"scanFrameSizePlus":{"width":250,"height":120},
							"scanFrameSize":200,
							"scanLight":"visible",
							"scanText":"对准物品条形码",
							"scanTitle":"扫码打印",
						}
					},
					(result) => {
						console.log(result)
						if(result.scanStatus == 1){
							this.scanResult(result.scanValue)
						}
					})
			},
			// 查询列表
			getPartList(){
				const _this = this
				shelfReplenishDetailList({ replenishBillSn: this.replenishBillSn }).then(res => {
					if(res.status == 200 && res.data){
						res.data.map(item =>{
							if(item.billState == 'FINISH'){
								item.confirmQty = item.putQty ? Number(item.putQty) : 0
							}else{
								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.contTitle = 0
						this.contModal = "抱歉，该产品不是此补货单的产品请更换产品重新扫描？"
						this.confirmModal = true
					}
					uni.hideLoading()
				})
			},
			modalConfirm(){
				this.confirmModal = false
				this.scanCode()
			},
			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) {
					console.log(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%;
		display: flex;
		flex-direction: column;
		.barCode{
			height: 28%;
		}
		.info-body{
			flex-grow: 1;
			overflow: auto;
			padding-bottom: 120rpx;
		}
		.info{
			background-color: #FFFFFF;
			padding: 10rpx 30upx;
			font-size: 32rpx;
			margin-top: 20rpx;
		}
		.sendOutGoods-footer{
			padding: 26upx 32upx 30upx;
			background-color: #fff;
			position: fixed;
			left: 0;
			bottom: 0;
			width: 100%;
			box-shadow: 3px 1px 7px #eee;
		}
	}
</style>
