<template>
	<view class="page-wraper">
		<view class="page-main">
			<scroll-view scroll-y="true" class="page-main-scroll">
				<view class=""
					style="padding-left: 20rpx;padding-right: 20rpx; padding-top: 10rpx;padding-bottom: 10rpx;">
					<view class="" style="font-size: 35rpx;">
						发货单号 :
					</view>
					<view class="" style="font-size: 35rpx;">
						<text>供应商 : </text>
					</view>
				</view>
				<u-line />
				<view class="detail-list" v-for="(item, index) in detailSource" :key="item.id">
					<view class="">
						<comRecordDetailCard :dataContent="item" :index="index" :settingParam="jobContent"
							@remove="updateData" @updateData="updateData" @openDetail="openDetail">
						</comRecordDetailCard>
					</view>
					<u-line />
				</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="">
				</view>
			</view>
		</view>

		<win-scan-pack ref="scanPopup" @getResult='getScanResult'></win-scan-pack>
		<recordDetailInfoPopup ref="recordDetailPopup"></recordDetailInfoPopup>
		<com-message ref="comMessage"></com-message>
	</view>
</template>

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

	import {
		goHome,
		getCurrDateTime,
		getPackingNumberAndBatch,
		getDirectoryItemArray,
	} from '@/common/basic.js';

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

	import {
		getDataSource,
		createRecordInfo,
		calcHandleQty
	} from '@/common/detail.js';


	import winScanPack from '@/mycomponents/scan/winScanPack.vue'
	import requiredLocation from '@/mycomponents/location/requiredLocation.vue'
	import comMessage from '@/mycomponents/common/comMessage.vue'
	import comRecordDetailCard from '@/pages/supplierDeliver/coms/comRecordDetailCard.vue'
	import recordDetailInfoPopup from '@/pages/supplierDeliver/coms/recordDetailInfoPopup.vue'

	export default {
		name: 'receipt_detail',
		components: {
			winScanPack,
			comRecordDetailCard,
			requiredLocation,
			comMessage,
			recordDetailInfoPopup
		},
		data() {
			return {
				id: '',
				receiptJob: {},
				received: false,

				scanCount: 0,
				jobContent: {}, //任务内容
				subList: [], //接口返回的任务subList
				detailSource: [], //绑定在页面上的数据源
				locationTypeList: [],
				toLocationCode: '',
				toLocationInfo: {},
				businessTypeInfo: {},
				managementList: []
			};
		},
		onLoad(option) {
			this.id = option.id;
			this.getDetail();
		},
		//返回首页
		onNavigationBarButtonTap(e) {
			if (e.index === 0) {
				goHome();
			}
		},
		//拦截返回按钮事件
		onBackPress(e) {},

		mounted() {

		},
		methods: {
			getDetail() {
				var that = this;
				uni.showLoading({
					title: "加载中....",
					mask: true
				});
				getSupplierDeliverRecordDetail(that.id).then(res => {
					uni.hideLoading();
					if (res.data == null) {
						that.showMessage('未获取到详情');
					} else {
						if (res.data.subList.length > 0) {
							that.jobContent = res.data;
							that.subList = res.data.subList;
							that.subList.forEach(res => {
								res.packingNumber = res.toPackingNumber
								res.batch = res.fromBatch
							})
							that.detailSource = getDataSource(that.subList)
						} else {
							that.showMessage('列表数据为0');
						}
					}
				}).catch(error => {
					uni.hideLoading()
					this.showErrorMessage(error)
				})
			},


			getScanResult(result) {
				try {
					var packingNumber = result.label.packingNumber;
					var batch = result.label.batch;
					var qty = result.label.qty;
					var itemCode = result.label.itemCode;
					var detail = this.detailSource.find(r => r.itemCode == itemCode);
					if (detail == undefined) {
						this.showMessage("物料号【" + itemCode + "】不在列表中")
					} else {
						var itemDetail = detail.subList.find(r => r.packingNumber == packingNumber && r.batch == batch);
						if (itemDetail == undefined) {
							this.showMessage("箱码【" + packingNumber + "】、批次【" + batch + "】不在列表中")
						} else {
							if (itemDetail.scaned) {
								this.showMessage("箱码【" + packingNumber + "】已经扫描")
							} else {
								itemDetail.scaned = true;
								itemDetail.record = this.createRecordInfo(itemDetail, result.label);
								calcHandleQty(this.detailSource);
							}
						}
					}

				} catch (e) {
					this.showErrorMessage(e.message)
				}
			},

			createRecordInfo(detail, label) {
				var record = {}
				detail.scaned = true;
				// let record = JSON.parse(JSON.stringify(detail));
				//克隆对象，深度克隆，防止双向绑定同一个变量
				Object.assign(record, detail)
				record.qty = Number(label.qty);
				record.toLocationCode = this.toLocationCode;
				return record;
			},

			getScanCount(closeScan) {
				let items = this.subList.filter(r => {
					if (r.scaned) {
						return r;
					}
				})
				let scanCount = items != null ? items.length : 0;
				return scanCount;
			},

			updateData() {
				calcHandleQty(this.detailSource);
			},

			scanLocationCode(location, code) {
				this.$refs.comMessage.showQuestionMessage("是否把所有的目标库位都变成默认库位[" + code + "]", res => {
					this.toLocationCode = code
					this.detailSource.forEach(item => {
						item.subList.forEach(detail => {
							detail.toLocationCode = code
						})
					})
				})
			},


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

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

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

			closeScanPopup() {
				this.$refs.scanPopup.closeScanPopup();
			},


			openDetail(item) {
				this.$refs.recordDetailPopup.openPopup(item)
			}
		}
	}
</script>

<style scoped lang="scss">
	page {
		width: 100%;
		height: 100%;
		background-color: #fff;
	}

	.page-wraper {
		display: flex;
		flex-direction: column;
		width: 100%;
		height: 100%;
	}


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

	.page-main-scroll {
		position: absolute;
		left: 0;
		right: 0;
		top: 0;
		bottom: 0;
	}

	.page-main-list {
		/*  height: 80rpx;
	    line-height: 80rpx; */
		text-align: center;
		background: #e0e0e0;

	}
</style>
