<template>
	<uni-popup ref="popup">
		<view>
			<view class="popup_box">
				<view class="pop_title">
					扫描信息
					<text class="fr" @click="closeScanPopup()">关闭</text>
				</view>

				<view class="uni-steps">
					<com-steps-custom :options="list" :active="active"></com-steps-custom>
				</view>
				<view class="pop_tab" style="padding: 0 20rpx;">
					<view class="tab_info tab_notitle">
						<win-com-scan ref="scancom" @getScanResult="getScanResult" :placeholder="scanPlaceholder"
							:boxFocus="true" :clearResult="true"></win-com-scan>
					</view>
				</view>
				<scroll-view scroll-y="true" class="scroll-Y scan_scroll">
					<view class="uni-list popuni_list creattp_list">
						<view class="list_cell uni-flex uni-row space-between" v-if="fromLocation!=null">
							<view>库位</view>
							<text class="text_bold">{{fromLocation.code}}</text>
						</view>
					</view>
					<uni-collapse v-if="itemList.length>0">
						<view class="pop_list list_info" v-for="(label, index) in itemList" :key="label.id"
							v-if="itemList.length>0">
							<uni-collapse-item :open="true"
								:title="label.itemCode+'('+label.totalQty+'/'+label.scanQty+')'">
								<view v-for="(item, index) in label.labelList" :key="item.id">
									<uni-swipe-action>
										<uni-swipe-action-item :right-options="options" :auto-close="false"
											@click="swipeClick($event,label,index)">
											<view class="ljh_box" style="background-color: #ffffff;padding-left: 20px;">
												<view class="uni-flex space-between desc_card">
													<view class="ljh_left">
														<view class="tit_ljh">{{item.packingCode }}
														</view>
													</view>
													<view class="ljh_right">
														<text class="tnum">{{item.qty}}</text>
														<text class="tunit">{{item.uom}}</text>
													</view>
												</view>
											</view>

										</uni-swipe-action-item>
									</uni-swipe-action>
								</view>
							</uni-collapse-item>
						</view>
					</uni-collapse>

					<!-- 
					<view class="pop_list list_info" v-if="itemList.length>0">
						<view v-for="(label, index) in itemList" :key="label.id">
							<view class="uni-flex space-between" v-if="label.scanQty>0">
								<view>{{label.itemCode}}</view>
								<view>{{label.totalQty}}/{{label.scanQty}}</view>
								<view v-if="label.uom!=null">({{label.uom}})</view>
							</view>
							<view class="detail-content" v-for="(item, index) in label.labelList" :key="item.id">
								<uni-swipe-action>
									<uni-swipe-action-item :right-options="options" :auto-close="false"
										@click="swipeClick($event,label,index)">
										<com-base-item :dataContent='item' :displayBottomInfo="false"></com-base-item>
									</uni-swipe-action-item>
								</uni-swipe-action>
							</view>
						</view>
					</view> -->
				</scroll-view>
				<view class="pop_btn uni-flex uni-row space-between" style="padding-top: 10rpx;">
					<button class="cancel" @click="cancel">取消</button>
					<button class="save" @click="save">确定</button>
				</view>
			</view>
			<comMessage ref="comMessage"></comMessage>
		</view>
	</uni-popup>
</template>

<script>
	import {
		getRemoveOption
	} from '@/common/basic.js';

	import comStepsCustom from '@/mycomponents/common/comStepsCustom.vue'
	import winComScan from '@/mycomponents/wincom/winComScan.vue'
	import comBaseItem from '@/mycomponents/comItem/comBaseItem.vue'
	import comMessage from '@/mycomponents/common/comMessage.vue'
	import {
		balances,
		locations,
		getBalancesByLocationAndItem,
		getBalancesByLocationAndItemAsync,
	} from '@/api/index.js';

	import {
		compare,
		showConfirmMsg,
		getCurrDate
	} from '@/common/basic.js';

	export default {
		name: 'comScanSemiPutaway',
		components: {
			comStepsCustom,
			winComScan,
			comBaseItem,
			comMessage
		},
		data() {
			return {
				options: [],
				active: 0,
				list: [{
					title: '来源库位',
					num: "1"
				}, {
					title: '箱标签',
					num: "2"
				}],
				fromLocation: null,
				defaultPlaceholder: '来源库位',
				scanPlaceholder: '',
				itemList: [],
				// itemList: [{
				// 	itemCode: '',
				// 	totalQty: 0,
				// 	scanQty: 0,
				// 	uom: '',
				// 	labelList: []
				// }],
				currentLabel: null
			};
		},
		props: {

		},
		filters: {

		},
		mounted() {
			this.scanPlaceholder = this.defaultPlaceholder;
			this.options = getRemoveOption();
		},
		onPullDownRefresh() {

		},
		onLoad() {

		},
		methods: {
			openScanPopup(fromLocation, itemList) {
				if (itemList.length > 0) {
					this.fromLocation = fromLocation;
					this.itemList = itemList;
					this.active = 1;
					this.scanPlaceholder = '箱标签';
				}
				this.$refs.popup.open('bottom')
			},
			closeScanPopup() {
				this.$refs.popup.close()
			},
			//提示是否移除选择的行?
			swipeClick(e, label, index) {
				let {
					content
				} = e;
				if (content.text === '移除') {
					uni.showModal({
						title: '提示',
						content: '是否移除选择的行?',
						success: res => {
							if (res.confirm) {
								let item = label.labelList[index];
								label.scanQty = label.scanQty - item.qty;
								this.removeLabel(label, index)
								//label.labelList.splice(index, 1);
							}
						}
					});
				}
			},

			removeLabel(label, index) {
				label.labelList.splice(index, 1);
				if (label.labelList.length == 0) {
					for (var i = 0; i < this.itemList.length; i++) {
						let item = this.itemList[i];
						if (item.itemCode == label.itemCode) {
							this.itemList.splice(i, 1)
							return;
						}
					}
				}
			},

			getScanResult(result) {
				if (this.active == 0) //源库位
				{
					this.scanLocation(result);
				} else //标签
				{
					this.scanPack(result);
				}
			},

			//扫描源库位
			scanLocation(result) {
				let that = this;
				let code = result.data.code;
				if (code == '') return;
				uni.showLoading({
					title: '扫描中...',
					mask: true
				});
				locations(code).then(res => {
					if (res != null) {
						if (res.type != 5) {
							that.showMessage('请扫描线边库位');
						} else {
							that.fromLocation = res;
							that.active += 1;
							that.scanPlaceholder = '箱标签';
							// that.afterScanlocation();
						}
					} else {
						that.showMessage('未查询到库位' + code);
					}
					uni.hideLoading();
				}).catch(err => {
					uni.hideLoading();
					that.showMessage(err.message);
				});
			},

			//扫描完库位的下一步动作
			afterScanlocation() {
				this.active += 1;
				this.scanPlaceholder = '箱标签';
			},

			scanPack(result) {
				let that = this;
				let code = result.data.code;
				if (code) {
					let datas = [];
					//判断是否重复扫描
					that.itemList.forEach(l => {
						l.labelList.filter(r => {
							if (r.packingCode == code) {
								datas.push(r);
								return;
							}
						})
					})
					if (datas.length > 0) {
						showConfirmMsg('箱码已经存在,是否要重新扫描?', confirm => {
							if (confirm) {
								that.itemList.forEach(l => {
									l.labelList.forEach((r, i) => {
										if (r.packingCode == code) {
											l.labelList.splice(i, 1);
											return;
										}
									});
								})
								that.getLabel(result);
							}
						});
					} else {
						that.getLabel(result);
					}

				} else {
					this.showMessage('请先扫描箱标签');
					return;
				}
			},

			async getLabel(label) {
				let that = this;
				uni.showLoading({
					title: '扫描中...',
					mask: true
				})
				this.currentLabel = label;
				let itemInfo = that.itemList.find(r => {
					return r.itemCode == label.itemCode
				})
				if (itemInfo == undefined) {
					let params = {
						locationCode: that.fromLocation.code,
						itemCode: label.itemCode
					};

					let balanceRes = await getBalancesByLocationAndItemAsync(params);
					if (balanceRes.length == 0) {
						that.showMessage('按库位和零件号[' + that.fromLocation.code + ',' + label.itemCode + ']未查找到库存信息');
					} else {
						let balanceItem = balanceRes[0];
						let item = {
							itemCode: label.itemCode,
							totalQty: balanceItem.qty,
							uom: balanceItem.uom,
							scanQty: 0,
							labelList: []
						};
						that.itemList.push(item)
						this.addLabel(item);
					}
				} else {
					this.addLabel(itemInfo);
				}
				uni.hideLoading();
			},

			addLabel(item) {
				let scanQty = 0;
				item.labelList.forEach(r => {
					scanQty += Number(r.qty)
				})
				if (scanQty + this.currentLabel.qty > item.totalQty) {
					this.showMessage('已扫描数量[' + scanQty + ']加标签数量[' + this.currentLabel.qty + ']大于零件的库存数量[' + balanceItem
						.totalQty + '],不可以上架')
				} else {
					item.scanQty = scanQty + this.currentLabel.qty
					item.labelList.unshift(this.currentLabel);
				}
			},

			save() {
				let that = this;
				if (this.itemList.length == 0) {
					return;
				} else {
					this.closeScanPopup();
					this.$emit('confirm', this.fromLocation, this.itemList)
				}
			},

			//扫描完库位的下一步动作
			afterScanlocation() {
				this.active += 1;
				this.scanPlaceholder = this.defaultPlaceholder;
			},

			//取消
			cancel() {
				this.active = 0;
				this.itemList = [];
				this.fromLocation = null
				this.scanPlaceholder = this.defaultPlaceholder;
			},

			showMessage(message) {
				this.$refs.comMessage.showMessage(message);
			},
		}
	};
</script>


<style>
</style>
