<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">
		<!-- 	<view class="page-header">
				<view class="header_item">
					来源库位 : {{fromLocationCode}}
				</view>
				<u-line />
			</view> -->
			<view class="page-main">
				<scroll-view scroll-y="true" class="page-main-scroll">
					<view class="detail-list" v-for="(item, index) in detailSource" :key="item.id">
						<view class="">
							<record-com-detail-card :dataContent="item" :index="index" :settingParam="dataContent"
								:isShowFromLocation="false" @removeItem="removeItem(index,item)"
								@updateData="updateData" @removePack="removePack">
							</record-com-detail-card>
						</view>

					</view>
				</scroll-view>
			</view>

			<view class="page-footer">
				<view class="uni-flex u-col-center space-between padding_10"
					style="background-color:ghostwhite; width: 100%; ">
					<view class="">
						<!-- <requiredLocation v-if="showToLoaction" title="目标库位" :locationCode="toLocationCode"
							@getLocation='getToLocationCode' :locationTypeList="tolocationTypeList"></requiredLocation> -->
					</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-package ref="scanPopup" @getResult='getScanResult' :allowModifyLocation='false'>
		</win-scan-package>
		<comMessage ref="comMessage"></comMessage>
	<!-- 	<win-scan-location ref="scanLocationCode" title="来源库位" @getLocation='getLocation'
			:locationAreaTypeList="fromLocationAreaTypeList"></win-scan-location> -->
	</view>
</template>

<script>
	import {
		putawayRecordSubmit,
		validateItemAndLocation,
		getPutawayRecommendLocation
	} from '@/api/request2.js';

	import {
		goHome,
		updateTitle,
		getCurrDateTime,
		getPackingNumberAndBatchByList,
		deepCopyData
	} from '@/common/basic.js';

	import {
		calc
	} from '@/common/calc.js';

	import {
		getInventoryStatusDesc,
		getDirectoryItemArray
	} from '@/common/directory.js';

	import {
		getBusinessType,
		createItemInfo,
		createDetailInfo,
		calcHandleQty
	} from '@/common/record.js';

	import {
		getManagementPrecisions,
		getPrecisionStrategyList,
		getPrecisionStrategyParams
	} from '@/common/balance.js';

	import winScanButton from '@/mycomponents/scan/winScanButton.vue'
	import winScanPackage from '@/mycomponents/scan/winScanPackage.vue'
	// import requiredLocation from '@/mycomponents/location/requiredLocation.vue'
	import comBlankView from '@/mycomponents/common/comBlankView.vue'
	import winScanLocation from "@/mycomponents/scan/winScanLocation.vue"
	import recordComDetailCard from '@/mycomponents/record/recordComDetailCard.vue'

	export default {
		components: {
			winScanButton,
			winScanPackage,
			// requiredLocation,
			comBlankView,
			winScanLocation,
			recordComDetailCard
		},
		data() {
			return {
				id: '',
				dataContent: {}, //内容
				detailSource: [], //绑定在页面上的数据源
				fromLocationInfo: {},
				fromLocationCode: "",
				fromLocationAreaTypeList: [],
				toLocationAreaTypeList: [],
				inInventoryStatus: "", //目标入库库存状态
				outInventoryStatus: "", //来源出库库存状态
				businessType: {},
				showToLoaction: true,
				recommendLocationList: [], //推荐库位列表
				fromWarehouseCode: '', //来源仓库
				businessTypeCode: "Relegate"
			};
		},
		onLoad(option) {
			this.clear();
			getBusinessType(this.businessTypeCode, res => {
				if (res.success) {
					this.businessType = res.businessType;
					this.fromLocationAreaTypeList = res.fromLocationAreaTypeList;
					this.toLocationAreaTypeList = res.toLocationAreaTypeList;
					this.openScanPopup();
				} else {
					this.showErrorMessage(res.message)
				}
			});
		},
		//返回首页
		onNavigationBarButtonTap(e) {
			if (e.index === 0) {
				goHome();
			}
		},
		//拦截返回按钮事件
		onBackPress(e) {},

		onPullDownRefresh() {},

		mounted() {},

		methods: {
			getScanResult(result) {
				let balance = result.balance;
				let label = result.label;
				let pack = result.package;
				let poLine =label.poLine;
				let poNumber =label.poNumber;
				let supplierCode=label.supplierCode;
			 
			},

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


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

			removeItem(index, item) {
				this.detailSource.splice(index, 1)
				this.updateData();
			},
 

			openScanPopup() {
				this.$refs.scanPopup.openScanPopupByBusinessType(this.businessType);
			},

			showFromLocationPopup() {
				this.$nextTick(() => {
					this.$refs.scanLocationCode.openScanPopup();
				})
			},

			closeScanPopup() {
				if (this.$refs.scanPopup != undefined) {
					this.$refs.scanPopup.closeScanPopup();
				}
			},

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

			// scanLocationCode(location, code) {
			// 	this.toLocationCode = code
			// 	this.toLocationCode = location;

			// },

			commit() {
				uni.showLoading({
					title: "提交中....",
					mask: true
				});

				//记录有目标库位，需要查询管理模式
				let precisionStrategyParams = getPrecisionStrategyParams(this.detailSource);
				//2:获取管理模式，封装参数
				getPrecisionStrategyList(precisionStrategyParams, res => {
					if (res.success) {
						this.managementList = res.list;
						var params = this.setRecordParams()
						console.log("提交参数", JSON.stringify(params));

						putawayRecordSubmit(params).then(res => {
							uni.hideLoading()
							if (res.data) {
								this.showCommitSuccessMessage("提交成功<br>生成采购上架记录<br>" + res.data, )
							} else {
								this.showErrorMessage("提交失败[" + res.msg + "]")
							}
						}).catch(error => {
							uni.hideLoading()
							this.showErrorMessage(error)
						})
					} else {
						uni.hideLoading();
						this.showErrorMessage(res.message);
					}
				});
			},

 
			setRecordParams() {
				var subList = []
				var creator = this.$store.state.user.id
				this.detailSource.forEach(item => {
					item.subList.forEach(detail => {
						if (detail.scaned) {
							var info = getPackingNumberAndBatchByList(this.managementList, detail.itemCode,
								detail.packingNumber, detail.toLocationCode, detail.batch);
							var submitItem = deepCopyData(detail)
							submitItem.toPackingNumber = info.packingNumber;
							submitItem.toBatch = info.batch;
							submitItem.toContainerNumber = detail.containerNumber;

							submitItem.fromPackingNumber = info.packingNumber;
							submitItem.fromBatch = info.batch;
							submitItem.fromContainerNumber = detail.containerNumber;

							submitItem.fromLocationCode = detail.locationCode;
							submitItem.toLocationCode = detail.toLocationCode;

							// detail.toInventoryStatus = detail.inventoryStatus
							// detail.toLocationCode = detail.toLocationCode
							submitItem.qty = detail.handleQty;
							submitItem.package = "";
							subList.push(submitItem)
						}
					})
				})
				if (subList.length > 0) {
					this.dataContent.toWarehouseCode = subList[0].toWarehouseCode;
				}
				this.dataContent.subList = subList
				this.dataContent.creator = creator;
				this.dataContent.fromWarehouseCode = this.fromWarehouseCode;
				return this.dataContent;
			},

			showMessage(message) {
				this.$refs.comMessage.showMessage(message, res => {
					if (res) {}
				});
			},

			showScanMessage(message) {
				this.$refs.comMessage.showScanMessage(message);
			},

			afterCloseMessage() {
				this.scanPopupGetFocus();
			},

			closeScanMessage() {
				this.scanPopupGetFocus();
			},
			getLocation(location, code) {
				this.getFromLocationCode(location, code)
			},
		 
			showCommitSuccessMessage(hint) {
				this.$refs.comMessage.showSuccessMessage(hint, res => {
					this.clear();
				})
			},

			clear() {
				this.fromLocationInfo = {};
				this.fromLocationCode = '';
				this.fromWarehouseCode = '';
				this.toWarehouseCode = '';
				this.detailSource = [];
			}
		}
	}
</script>

<style scoped lang="scss">

</style>