<template>
	<view class="page-wraper">
		<view class="">
			<com-blank-view @goScan='openScanPopup' v-if="detailSource.length==0"></com-blank-view>
		</view>
		<view class="page-wraper" v-if="detailSource.length>0">
			<u-line />
			<view class="page-main">
				<uni-data-picker v-if="detailSource.length>0" style="padding: 20rpx; background-color:#fff;"
					class='uni-data-picker' placeholder="请选择入库原因" popup-title="入库原因" :localdata="reasonList"
					v-model="reason">
				</uni-data-picker>

				<scroll-view scroll-y="true" class="page-main-scroll">
					<view class="detail-list" v-for="(item, index) in detailSource" :key="item.id">
						<view class="">
							<com-receipt-record :dataContent="item" @removeItem="removeItem(index,item)"
								@updateData="updateData" :isShowLocation="false" @removePack="removePack">
							</com-receipt-record>
						</view>
						<u-line />
					</view>
				</scroll-view>
			</view>
			<requiredLocation title="目标库位" :locationCode="toLocationCode" @getLocation='getToLocationCode'
				:locationTypeList="tolocationTypeList"></requiredLocation>
			<view class="page-footer">
				<view class="uni-flex u-col-center space-between padding_10"
					style="background-color:ghostwhite; width: 100%; ">
					<view class="">
					</view>
					<view class=" uni-flex uni-row">
						<button class="btn_single_commit" hover-class="btn_commit_after" @click="commit">提交</button>
					</view>
				</view>
			</view>
			<win-scan-button @goScan='openScanPopup'></win-scan-button>
		</view>
		<win-scan-pack ref="scanPopup" @getResult='getScanResult' :title="'箱码'"></win-scan-pack>
		<com-message ref="comMessage"></com-message>
	</view>
</template>

<script>
	import comEmptyView from '@/mycomponents/common/comEmptyView.vue'
	import winScanButton from '@/mycomponents/scan/winScanButton.vue'
	import requiredLocation from '@/mycomponents/location/requiredLocation.vue'
	import comReceiptRecord from '@/pages/unPlanned/coms/comReceiptRecord.vue'
	import comBlankView from '@/mycomponents/common/comBlankView.vue'
	import winScanLocation from "@/mycomponents/scan/winScanLocation.vue"
	import winScanPack from "@/mycomponents/scan/winScanPack.vue"
	import comMessage from '@/mycomponents/common/comMessage.vue'

	import {
		getBalanceByUniquecode,
	} from '@/api/request.js';

	import {
		getDirectoryItemArray,
		getUnPlannedReceiptReasonList
	} from '@/common/directory.js';
	
	import {
		getBusinessType,
	} from '@/common/record.js';

	export default {
		components: {
			comEmptyView,
			winScanButton,
			requiredLocation,
			comReceiptRecord,
			comBlankView,
			winScanLocation,
			winScanPack,
			comMessage
		},
		props: {

		},
		data() {
			return {
				toLocationCode: "",
				toLocationInfo: {},
				toInventoryStatus: [],
				businessType: {}, //业务类型
				detailSource: [], //绑定在页面上的数据源
				businessTypeCode: "UnplannedReceipt",
				reason: "",
				reasonList: [],
				fromType:""
			}
		},

		mounted() {
			this.reasonList = getUnPlannedReceiptReasonList();
			getBusinessType(this.businessTypeCode, res => {
				if (res.success) {
					this.businessType = res.businessType;
					this.fromlocationTypeList = res.fromlocationTypeList;
					this.tolocationTypeList = res.tolocationTypeList;
					this.openScanPopup();
				} else {
					this.$refs.comMessage.showBreakMessage(res.message );
				}
			});
		},

		onLoad(option) {
		this.fromType = option.fromType
		},

		methods: {
			openScanPopup() {
				this.$refs.scanPopup.openScanPopup();
			},
			getToLocationCode(location, code) {
				this.toLocationCodeInfo = location;
				this.toLocationCode = code;
			},
			getScanResult(result) {
				let label = result.label;
				let pack = result.package;
				var item = this.detailSource.find(res => {
					if (res.itemCode == label.itemCode) {
						return res
					}
				})
				if (item == undefined) {
					var itemp = this.createItemInfo(label, pack);
					let newDetail = this.createDetailInfo(label, pack);
					itemp.details.push(newDetail);
					this.detailSource.push(itemp)
				} else {
					var detail = item.details.find(r => {
						if (r.packingNumber == result.number &&
							r.batch == result.batch) {
							return r;
						}
					})

					if (detail == undefined) {
						let newDetail = this.createDetailInfo(label, pack);
						item.details.push(newDetail);
					} else {
						if (detail.scaned) {
							this.showErrorMessage("箱码[" + result.Number + "]批次[" + result.batch + "]已经扫描")
						}

					}
				}
				this.calcHandleQty();

			},

			createItemInfo(label, pack) {
				let item = {
					itemCode: pack.itemCode,
					itemName: pack.itemName,
					stdPackQty: pack.stdPackQty,
					stdPackUnit: pack.stdPackUnit,
					qty: label.qty,
					handleQty: 0,
					uom: label.uom,
					details: []
				}
				return item;
			},

			createDetailInfo(label, pack) {
				let detail = {};
				detail.scaned = true;
				// Object.assign(detail, label)
				detail.qty = Number(label.qty)
				detail.inventoryStatus = "kcztOK"
				detail.stdPackQty = pack.stdPackQty;
				detail.stdPackUnit = pack.stdPackUnit;
				label.inventoryStatus="kcztOK";
				detail.package = pack;
				detail.label = label;
				return detail;
			},

			calcHandleQty() {
				for (let item of this.detailSource) {
					item.qty = 0;
					for (let detail of item.details) {
						if (detail != undefined) {
							item.qty += Number(detail.qty)
						}
					}
				}
				this.$forceUpdate();
			},

			showErrorMessage(message) {
				this.$refs.comMessage.showErrorMessage(message, res => {
					if (res) {

					}
				});
			},
			updateData() {
				this.calcHandleQty();
			},
			removePack() {
				for (var i = 0; i < this.detailSource.length; i++) {
					var item = this.detailSource[i];
					if (item.details.length == 0) {
						this.detailSource.splice(i, 1)
					}
				}
				this.updateData();
			},

			removeItem(index, item) {
				this.detailSource.splice(index, 1)
			},
			commit() {
				if (this.toLocationCode == '') {
					this.showErrorMessage("请扫描目标库位")
					return;
				}
				if (this.reason == '') {
					this.showErrorMessage("请选择入库原因")
					return;
				}

				console.log("提交", this.reason)
			}
		}
	}
</script>

<style>
	.page-main {
		flex: 1;
		position: relative;
	}

	.page-main-scroll {
		position: relative;
		margin-top: 2px;
	}
</style>
