<template>
	<uni-popup ref="popup">
		<view class="popup_box">
			<view class="pop_title">
				扫描信息
				<text class="fr" @click="closeScanPopup()">关闭</text>
			</view>
			<view class="pop_tab">
				<view class="uni-steps">
					<com-steps-custom :options="list" :active="active"></com-steps-custom>
				</view>
				<view class="popuni_list" style="margin-bottom: 20rpx;">
					<view class="list_cell" v-if="firstScan">
						<uni-data-picker class='uni-data-picker' popup-title="隔离库存处理" :localdata="holdStatusArray"
							v-model="holdStatus" @change="holdStatusChanged" style="padding: 10rpx 20rpx;">
						</uni-data-picker>
					</view>
					<view v-else class="uni-list popuni_list">
						<view class="list_cell uni-flex uni-row space-between">
							<view>{{holdItem.holdType}}</view>
						</view>
					</view>
				</view>
				<scroll-view class="scan_scroll scroll-Y" scroll-y="true" style="max-height: 56vh;">
					<view class="tab_info next_info" style="width: 98%;margin:1%;">
						<win-com-scan :placeholder="scanPlaceholder" :clearResult='true' @getScanResult='getScanResult'>
						</win-com-scan>
					</view>

					<view class="popinpop" v-for="(item,index) in itemList">
						<uni-swipe-action>
							<uni-swipe-action-item :right-options="options" :auto-close="false"
								@click="removeClick($event,index)">
								<com-hold :dataContent="item" :editQty="true" :holdItem="holdItem" @qtyInput='qtyInput'>
								</com-hold>
							</uni-swipe-action-item>
						</uni-swipe-action>
					</view>
				</scroll-view>
			</view>
			<view class="pop_btn uni-flex uni-row space-between">
				<button class="cancel" @click="cancel">取消</button>
				<button class="save" @click="save">确定</button>
			</view>
		</view>
		<comMessage ref="comMessage"></comMessage>
		<com-balance ref="balanceItems" @selectedItem='selectedBalanceItem'></com-balance>
	</uni-popup>
</template>

<script>
	import {
		getHoldStatusArray
	} from '@/common/array.js';

	import {
		getBalancesByFilter,
	} from '@/api/index.js';

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

	import comStepsCustom from '@/mycomponents/common/comStepsCustom.vue'
	import winComScan from '@/mycomponents/wincom/winComScan.vue'
	import comHold from '@/mycomponents/coms/inventory/comHold.vue'
	import comBalance from '@/mycomponents/common/comBalance.vue'
	import comMessage from '@/mycomponents/common/comMessage.vue'
	export default {
		name: 'comScanHold',
		components: {
			comStepsCustom,
			comHold,
			winComScan,
			comBalance,
			comMessage,
		},
		data() {
			return {
				options: [],
				active: 1,
				list: [{
					title: '选择状态',
					num: "1"
				}, {
					title: '扫描标签',
					num: "2"
				}],
				scanPlaceholder: '箱标签',
				holdItem: {
					holdStatus: 1,
					holdType: '隔离转合格'
				},
				itemList: [],
				scanResult: {},
				holdStatus: 1,
				holdStatusArray: [],
				firstScan: true,
			};
		},
		props: {

		},
		created() {
			this.options = getRemoveOption();
			this.holdStatusArray = getHoldStatusArray();
		},
		mounted() {

		},
		onPullDownRefresh() {

		},
		methods: {
			openScanPopup(holdItem) {
				if (holdItem.holdType == '') {
					this.firstScan = true;
				} else {
					this.firstScan = false;
				}
				this.$refs.popup.open('bottom')
			},

			closeScanPopup() {
				this.$refs.popup.close()
			},

			holdStatusChanged(e) {
				this.holdItem = {
						holdStatus: e.detail.value[0].value,
						holdType: e.detail.value[0].text
					},
					// this.holdType = e.detail.value[0].text;
					this.$emit('holdTypeChange', this.holdItem)
			},

			getScanResult(result) {
				let that = this;
				if (!result.data.isPack) {
					this.showMessage('请扫描箱标签');
					return;
				};

				if (result.data.itemCode == null || result.data.itemCode == undefined) {
					this.showMessage('未解析到ERP料号信息');
					return;
				}
				that.scanResult = result;
				let items = that.itemList.filter(r => {
					return r.itemCode === result.data.itemCode &&
						r.packingCode === result.data.packingCode &&
						r.lot === result.data.lot
				})

				if (items.length > 0) {
					showConfirmMsg('零件已经存在,是否要重新扫描?', confirm => {
						if (confirm) {
							that.itemList.forEach((r, i) => {
								if (r.itemCode === result.data.itemCode &&
									r.lot === result.data.lot &&
									r.packingCode === result.data.packingCode) {
									that.itemList.splice(i, 1);
									return;
								}
							})
							that.getBalance(result);
						}
					})
				} else {
					that.getBalance(result);
				}
			},

			getBalance(result) {
				uni.showLoading({
					title: '正在查询库存余额',
					mask: true
				})

				let params = {
					pageSize: 100,
					pageIndex: 1,
					itemCode: result.data.itemCode,
					packingCode: result.data.packingCode,
					sortBy: "PackingCode"
				};
				// if (this.holdItem.holdStatus === 1 || this.holdItem.holdStatus === 2) //隔离转合格,隔离出库
				// {
				// 	params.locationTypes = [6] //隔离库
				// } else if (this.holdItem.holdStatus === 3) //合格转隔离
				// {
				// 	params.locationTypes = [1, 2, 3, 4, 5, 9, 10] //去掉6隔离库，7报废库，8不合格品库
				// }
				getBalancesByFilter(params).then((res) => {
					try {
						if (res.totalCount == 0) {
							this.showMessage('未查找到库存信息');
						} else if (res.totalCount == 1) {
							let data = res.items[0];
							this.setScanInfo(data, result);
							this.itemList.unshift(data);
							this.firstScan = false;
						} else {
							if (res.items != null) {
								this.$refs.balanceItems.openPopup(res.items);
							}
						}
					} catch (e) {
						this.showMessage(e.message);
					}
					uni.hideLoading();
				}).catch((err) => {
					this.showMessage(err.message);
					uni.hideLoading();
				})
			},

			selectedBalanceItem(balanceItem) {
				this.setScanInfo(balanceItem, this.scanResult);
				this.itemList.unshift(balanceItem);
				this.firstScan = false;
				this.$forceUpdate();
			},

			setScanInfo(data, result) {
				let qty = 0;
				if (Number(result.data.qty) > data.qty.qty) {
					qty = data.qty.qty;
					this.showMessage('收获数量[' + result.data.qty + ']大于库存数量,默认改为库存数量[' + data.qty.qty + ']')
				} else {
					qty = Number(result.data.qty);
				}

				data.fromQty = data.qty;
				data.qty = {
					qty: qty,
					uom: data.qty.uom
				}

				data.fromPackingCode = data.packingCode;
				data.toPackingCode = result.data.packingCode;

				data.fromContainerCode = data.containerCode;
				data.toContainerCode = result.data.containerCode;

				data.fromLocationCode = data.locationCode;

				data.fromWarehouseCode = localStorage.warehouseCode;
				data.toWarehouseCode = localStorage.warehouseCode;

				data.fromLot = data.lot;
				data.toLot = data.lot;
				// data.toLot = result.data.lot;

				data.fromStatus = data.status;
				// data.toStatus = 2;
			},

			qtyInput(value, item) {
				let qty = Number(value);
				if (qty > item.fromQty.qty) {
					this.showMessage('实际数量不能大于[' + qty + ']')
					setTimeout(() => {
						item.qty.qty = item.fromQty.qty;
					}, 100)
				}
			},

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

			//取消
			cancel() {
				this.active = 0;
				this.holdItem = {
					holdStatus: 1,
					holdType: '隔离转合格'
				};
				this.itemList = [];
			},

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

<style>
</style>
