<template>
	<view class="sendOutGoods-wrap">
		<view class="sendOutGoods-con">
			<view class="barCode" id="barcode"></view>
			<view class="info-body">
				<view class="info partList">
					<!-- 补货产品 -->
					<partList :list="partList" title="补货产品" model="view" fromPage="sendOutGoods" ref="partList" noDataText="暂无产品"></partList>
				</view>
			</view>
		</view>
		<view class="sendOutGoods-footer">
			<u-button @click="handleOutGoods" type="success" :custom-style="customStyle" hover-class="none" shape="circle">立即出库</u-button>
		</view>
		<!-- 确认弹框 -->
		<common-modal v-if="confirmModal" :openModal="confirmModal" title="扫码失败" :content="contModal" confirmText="好的" :isCancel="false" @confirm="modalConfirm" @close="confirmModal=false" />
		<!-- 出库确认弹框 -->
		<common-modal v-if="confirmOutModal" :openModal="confirmOutModal" title="出库" content="此补货单产品已经全部扫描完成,确认出库吗？" confirmText="确认出库" @confirm="modalOutConfirm" @close="confirmOutModal=false" />
		<!-- 选择配送方式弹框 -->
		<choose-type-modal v-if="chooseModal" :openModal="chooseModal" @confirm="modalChooseType" @close="chooseModal=false" />
	</view>
</template>

<script>
	import partList from '@/pages/common/partList.vue'
	import chooseTypeModal from './chooseTypeModal.vue'
	import commonModal from '@/pages/common/commonModal.vue'
	import { getQueryString } from '@/libs/tools'
	import { shelfReplenishDetailList, shelfReplenishDetailOutScan, shelfReplenishOutStock } from '@/api/shelfReplenish'
	export default {
		components: { partList, commonModal, chooseTypeModal },
		data() {
			return {
				barcode:null,
				replenishBillSn: null,
				shelfSn: null,
				partList: [],
				confirmModal: false,
				customStyle: {
					borderRadius:'100rpx',
					fontSize:'30rpx',
					background: this.$config('primaryColor')
				},
				isAll: false,
				contModal: '',
				confirmOutModal: false,
				chooseModal: false
			}
		},
		onReady() {
			// 初始化摄像头
			this.init()
		},
		onLoad(options) {
			this.replenishBillSn = options.sn
			this.shelfSn = options.shelfSn
			this.getPartList()
		},
		methods: {
			// 立即出库
			handleOutGoods(){
				if(this.isAll){
					this.confirmOutModal = true
				}else{
					this.toashMsg("请全部扫描完成后，再进行出库操作")
				}
			},
			// 选择配送方式
			modalChooseType(data){
				const _this = this
				const arr = []
				_this.partList.map((item, index) => {
				  arr.push({
				    productSn: item.productSn,
				    confirmQty: item.confirmQty,
				    replenishBillDetailSn: item.replenishBillDetailSn
				  })
				})
				const params = {
				  replenishBillSn: _this.replenishBillSn,
				  detailList: arr,
				  dispatchType: data.dispatchType,
				  remarks: data.remarks,
				  shelfSn: _this.shelfSn
				}
				console.log(params)
				shelfReplenishOutStock(params).then(res => {
					console.log(res)
				  if (res.status == 200) {
				    _this.toashMsg(res.message)
					_this.chooseModal = false
					uni.navigateTo({ url: '/pages/replenishmentManage/replenishmentList?billState=WAIT_OUT_STOCK' })
				  }else{
					  _this.toashMsg(res.message)
				  }
				})
			},
			// 确认出库
			modalOutConfirm(){
				this.chooseModal = true
			},
			// 查询列表
			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
				shelfReplenishDetailOutScan({
					replenishBillSn: _this.replenishBillSn,
					shelfSn: getQueryString('&'+qrCode, 'shelfSn'),
					shelfPlaceSn: getQueryString('&'+qrCode, 'shelfPlaceSn'),
					productSn: getQueryString('&'+qrCode, 'productSn')
				}).then(res => {
					console.log(res)
					if(res.status == 200){
						_this.toashMsg("扫码成功！")
						_this.getPartList()
						if(res.data && res.data.totalConfirmQty && res.data.totalScanQty && res.data.totalConfirmQty == res.data.totalScanQty){
							_this.isAll = true
							_this.handleOutGoods()
						}
					}else{
						this.contModal = res.message
						this.confirmModal = true
					}
				})
			},
			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;
	}
	.sendOutGoods-wrap{
		position: relative;
		width: 100%;
		height: 100%;
		overflow: hidden;
		padding-bottom: 136upx;
		.sendOutGoods-con{
			width: 100%;
			height: 100%;
			overflow: auto;
			.barCode{
				height: 30%;
			}
			.info-body{
				flex-flow: 1;
				overflow: auto;
				height: 70%;
			}
			.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>
