<template>
	<view>
		<uni-popup ref="popup" @maskClick="closeScanPopup()">
			<view class="">
				<view class="popup_box">
					<view class="pop_title">
						扫描箱码
						<text class="fr" @click="closeScanPopup()">关闭</text>
					</view>
					<view class="uni-flex uni-row" style="align-items: center;
						background-color: #fff; 
						margin-left: 20rpx;
						margin-right: 20rpx;
						border-radius: 8rpx;">
						<view class="uni-center" style="width: 25%; ">
							需求库位
						</view>
						<view class="" style="width: 75%;padding: 8rpx">
							<uni-combox ref="toLocationCombox" :candidates="requestLocationList" placeholder="请选择需求库位"
								v-model="requestLocationCode" @confirm="toLocationUpdate"></uni-combox>
						</view>
					</view>

					<view class="uni-flex uni-row" style="align-items: center;
						background-color: #fff; 
						margin-top: 10rpx;
						margin-left: 20rpx;
						margin-right: 20rpx;
						border-radius: 8rpx;">
						<view class="uni-center" style="width: 25%; ">
							发料库位
						</view>
						<view class="uni-flex uni-row u-col-center" style="width: 75%; padding: 8rpx;">
							<view class="">
								<uni-combox :candidates="fromLocationList" v-model="fromLocationCode"
									placeholder="请选择库位" @confirm="fromLocationUpdate"></uni-combox>
							</view>
							<!-- <view class="" v-if="locationCodeCountItem.recommendQty>0">
								{{locationCodeCountItem.handleQty}}/{{locationCodeCountItem.recommendQty}}({{locationCodeCountItem.uom}})
							</view> -->
						</view>
					</view>

					<view class="pop_tab">
						<view class="tab_info">
							<win-com-scan ref="comscan" placeholder="箱标签" @getResult="onScan" :clearResult="true">
							</win-com-scan>
						</view>
					</view>
					<!-- <view class="uni-flex uni-row" style="background-color: white">
						<button class="btn_edit_big_cancle" hover-class="btn_edit_big_after"
							@click="cancle()">取消</button>
						<button class="btn_edit_big_confirm" hover-class="btn_edit_big_after"
							@click="confirm()">确认</button>
					</view> -->
				</view>
			</view>
		</uni-popup>
		<com-message ref="comMessage"></com-message>
	</view>
</template>

<script>
	import comMessage from '@/mycomponents/common/comMessage.vue'
	import winComScan from '@/mycomponents/scan/winComScan.vue'

	export default {
		name: 'winScanPack',
		components: {
			comMessage,
			winComScan
		},
		props: {
			title: {
				type: String,
				default: ''
			},

		},
		data() {
			return {
				dataContent: {},
				jobContent: {},
				expendIcon: 'arrow-down',
				show: false,
				scanList: [],       
				requestLocationList: [],
				requestLocation: null,
				requestLocationCode: '',
				fromLocationList: [],
				fromLocationCode: '',
				fromLocation: null,
			}
		},
		created() {
			 
		},
		watch: {},
		mounted() {},
		methods: {
			openScanPopup(content, jobcontent) {
				this.dataContent = content;
				this.jobContent = jobcontent;
				this.initData();
				this.$refs.popup.open('bottom');
			},

			closeScanPopup() {
				this.$refs.popup.close();
				this.$emit("closeScan")
				//清除数据,恢复默认值
				// Object.assign(this.$data, this.$options.data());
			},

			initData() {
				let that = this;
				that.requestLocationList = [];
				that.fromLocationList = [];
				if (that.dataContent != null) {
					that.requestLocation = that.dataContent[0];
					that.requestLocationCode = that.dataContent[0].toLocationCode;
					that.dataContent.forEach(res => {
						that.requestLocationList.push(res.toLocationCode)
					})
					that.fromLocationList = that.getFromLocationList();
				}
			},

			getFromLocationList() {
				let list = [];
				let location = this.dataContent.find(r => r.toLocationCode == this.requestLocation.toLocationCode);
				if (location != undefined) {
					location.Items.forEach(item => {
						item.Locations.forEach(f => {
							list.push(f.fromLocationCode)
						})
						this.fromLocationCode = item.Locations[0].fromLocationCode;
					})
					return list;
				} else {
					this.$refs.comMessage.showErrorMessages('需求库位【' + this.toLocationCode + '】不存在', res => {
						this.toLocationCode = '';
					});
				}
			},

			fromLocationUpdate(fromlocation) {
				let location = this.fromLocationList.find(r => r == fromlocation)
				if (location == undefined) {
					this.fromLocationCode = ''
					this.showErrorMessage('发料库位【' + fromlocation + '】不存在')
				}
			},

			toLocationUpdate(tolocation) {
				let toLocation = this.requestLocationList.find(r => r == tolocation);
				if (toLocation == undefined) {
					this.requestLocation = null;
					this.requestLocationCode = '';
					this.showErrorMessage('需求库位【' + tolocation + '】不存在')
				} else {
					this.getFromLocationList();
				}
			},

			qtyChanged(value, item) {
				if (value == 0) {
					uni.showToast({
						title: '数量必须大于0'
					})
					return;
				}
				item.qty = value;
				this.calcCount();
			},

			onScan(result) {
				let that = this;
				if (that.requestLocation == null) {
					that.showErrorMessage('请选择需求库位', res => {
						that.$refs.toLocationCombox.onFocus();
					});
					return;
				}
				if (that.fromLocationCode == '') {
					that.showErrorMessage('请选择发料库位', res => {
						that.$refs.toLocationCombox.onFocus();
					});
					return;
				}

				let itemCode = result.label.itemCode;
				let packingCode = result.label.packingNumber;
				let lot = result.label.batch;

				let item = that.requestLocation.Items.find(r => r.itemCode == itemCode);
				if (item == undefined) {
					that.showErrorMessage('未查找到零件【' + itemCode + '】的发料明细',
						res => {
							that.getfocus();
						}
					)
					return;
				} else {
					let fromLocation = item.Locations.find(l => l.fromLocationCode == that.fromLocationCode);
					if (fromLocation != undefined) {
						let batch = fromLocation.Batchs.find(r => r.batch == lot);
						if (batch != undefined) {
							if (batch.Records == undefined) {
								batch.Records = [];
							}

							let record = batch.Records.find(r => r.packingNumber == packingCode);
							if (record == undefined) {
								let record = that.creatRecord(result.label);
								batch.Records.push(record);
								that.calcBatchHandleQty(batch);
							} else {
								that.showErrorMessage('箱码【' + packingCode + '】已经扫描,请继续扫描下一个箱',
									res => {
										that.getfocus();
									}
								)
							}
						} else {
							if (this.jobContent.AllowModifyBatch == null) {
								this.showQuestionMessage('未查找到批次【' + lot + '】的发料明细,是否要继续发料?', res => {
									if (res) {
										let batch = that.createBatchInfo(result.data);
										fromLocation.Batchs.unshift(batch);
									}
								})
							} else {
								that.showErrorMessage('未查找到批次【' + lot + '】的发料明细',
									res => {
										that.getfocus();
									});
							}
						}
					} else {
						that.showErrorMessage('未查找到推荐库位【' + that.fromLocationCode + '】的发料明细',
							res => {
								that.getfocus();
							}
						)
					}
				}
			},

			createBatchInfo(data) {
				let batch = {
					batch: data.lot,
					qty: 0,
					uom: data.uom,
					handleQty: Number(data.qty),
					Records: []
				}
				let record = this.creatRecord(data);
				batch.Records.push(record);
				return batch;
			},

			creatRecord(data) {
				let record = {
					packingNumber: data.packingNumber,
					qty: Number(data.qty),
					inventoryStatus: 'kcztOK'
				}
				return record;
			},

			calcBatchHandleQty(batch) {
				let handleQty = 0;
				batch.Records.forEach(res => {
					handleQty += res.qty
				})
				batch.handleQty = handleQty;
			},

			calcCount() {
				let handleQty = 0;
				this.scanList.forEach(res => {
					handleQty += res.qty
				})
				this.scanCount = handleQty;
				this.locationCodeCountItem.handleQty = this.scanCount;
			},

			getfocus() {
				if (this.$refs.comscan != undefined) {
					this.$refs.comscan.getfocus();
				}
			},

			losefocus() {
				if (this.$refs.comscan != undefined) {
					this.$refs.comscan.losefocus();
				}
			},

			showMessage(message, callback) {
				this.$refs.comMessage.showMessage(message,callback);
			},
			showErrorMessage(message, callback) {
				this.$refs.comMessage.showErrorMessage(message,callback);
			},
			showQuestionMessage(message, callback) {
				this.$refs.comMessage.showQuestionMessage(message, callback);
			},
			confirm() {
				this.closeScanPopup();
				this.$emit("onConfirm", this.dataContent);
			},
			cancle() {
				this.closeScanPopup()
			}
		}
	}
</script>

<style lang="scss">
	button {
		border: none;
	}

	button::after {
		border: none
	}

	.scroll-view {
		overflow-y: scroll;
		height: auto;
		max-height: 300rpx;
		padding: 10rpx;
	}
</style>