<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="tab_info tab_notitle">
					<win-com-scan ref="scanLocation" v-if="active==0" @getScanResult="getLocationResult"
						:placeholder="scanPlaceholder" :boxFocus="true" :clearResult="true"></win-com-scan>
					<win-com-scan-product v-else="active==1" ref="scanProduct" @getScanResult="getProductResult"
						:placeholder="scanPlaceholder" :boxFocus="true" :clearResult="true"></win-com-scan-product>
				</view>
				<scroll-view scroll-y="true" class="scroll-Y scan_scroll">
					<!-- 回显库位 -->
					<view class="top_wrap" style="padding: 20rpx 0;" v-if="locationCode!=''">
						<view class="top_card">
							<view class="uni-flex space-between top_lines_info">
								<view class="font_sm">
									库位:
									<text class="text_bold">{{locationCode}}</text>
								</view>
							</view>
						</view>
					</view>
					<view class="detail-list count_shadow" v-for="(item, index) in itemList" :key="item.id"
						style="margin:0 0 20rpx 0;">
						<view class="detail-content">
							<uni-swipe-action>
								<uni-swipe-action-item :right-options="options" :auto-close="false"
									@click="swipeClick($event,index)">
									<com-deliver-tf :dataContent="item" :editQty="true" @qtyInput='qtyInput'>
									</com-deliver-tf>
								</uni-swipe-action-item>
							</uni-swipe-action>
						</view>
					</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>
		<com-balance ref="balanceItems" @selectedItem='getSelectedBalance'></com-balance>
		<comMessage ref="comMessage"></comMessage>
	</uni-popup>
</template>

<script>
	import comStepsCustom from '@/mycomponents/common/comStepsCustom.vue'
	import winComScan from '@/mycomponents/wincom/winComScan.vue'
	import winComScanProduct from '@/mycomponents/wincom/winComScanProduct.vue'
	import comScanDeliverTf from '@/mycomponents/scan/comScanDeliverTf.vue'
	import comDeliverTf from '@/mycomponents/coms/store/comDeliverTf.vue'
	import comBalance from '@/mycomponents/common/comBalance.vue'
	import comMessage from '@/mycomponents/common/comMessage.vue'
	import {
		locations,
		getBalancesByFilter,
	} from '@/api/index.js';

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

	export default {
		name: 'comScanReplen',
		components: {
			comStepsCustom,
			winComScan,
			winComScanProduct,
			comScanDeliverTf,
			comDeliverTf,
			comBalance,
			comMessage
		},
		data() {
			return {
				active: 0,
				list: [{
					title: '扫描目标库位',
					num: "1"
				}, {
					title: '扫描产品标签',
					num: "2"
				}],
				//滑动移除
				options: [],
				itemList: [],
				prodLine: {},
				locationCode: '',
				scanPlaceholder: '扫描目标库位'
			};
		},
		props: {

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

		},
		methods: {
			openScanPopup(locationCode, itemList) {
				if (itemList.length > 0) {
					this.locationCode = locationCode;
					this.itemList = itemList;
					this.active = 1
				}
				this.$refs.popup.open('bottom')
			},
			closeScanPopup() {
				this.$refs.popup.close()
			},

			//获取地址
			getLocationResult(result) {
				uni.showLoading({
					title: '正在扫描',
					mask: true
				})
				locations(result.data.code).then(res => {
					if (res == null) {
						this.showMessage('目标库位不存在')
					} else {
						this.locationCode = result.data.code;
						this.active += 1;
						this.scanPlaceholder = '扫描产品标签';
					}
					uni.hideLoading();
				}).catch(err => {
					this.showMessage(err.message)
					uni.hideLoading();
				})
			},

			//获取产品信息
			getProductResult(result) {
				let that = this;
				if (result.data != null) {
					that.scanData = result.data;
					//按照ERP料号+箱码+批次去重
					let datas = that.itemList.filter(r => {
						return r.itemCode === result.data.itemCode &&
							r.lot === result.data.lot &&
							r.packingCode === result.data.packingCode
					});

					if (datas.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.getDeliverItem(result.data);
							}
						});
					} else {
						that.getDeliverItem(result.data);
					}
				} else {
					this.showMessage('标签格式不正确,请重新扫描');
				}
			},

			getDeliverItem(data) {
				let that = this;
				that.getBalance(data, item => {
					if (item != null) {
						if (item.items.length == 0) {
							this.showMessage('在成品库未查找到该零件的库存');
						} else if (item.items.length == 1) {
							let deliverItem = that.createDeliverItem(data, item.items[0]);
							that.itemList.unshift(deliverItem);
						} else {
							this.$refs.balanceItems.openPopup(item.items);
						}
					} else {
						this.showMessage('在成品库未查找到该零件的库存');
					}
				});
			},

			//创建收货item
			createDeliverItem(data, balanceItem) {
				let deliveritem = {
					itemCode: balanceItem.itemCode,
					item: {
						id: balanceItem.id,
						name: balanceItem.itemName,
						desc1: balanceItem.item.desc1,
						desc2: balanceItem.item.desc2
					},
					lot: balanceItem.lot,
					batch: balanceItem.batch,
					packingCode: balanceItem.packingCode,
					qty: {
						uom: balanceItem.qty.uom,
						qty: balanceItem.qty.qty
					},
					locationCode: balanceItem.locationCode,
					warehouseCode: localStorage.warehouseCode,
					balanceQty: balanceItem.qty.qty,
					status: balanceItem.status
				};
				return deliveritem;
			},

			//获取零件库存
			getBalance(data, callback) {
				uni.showLoading({
					title: "加载中....",
					mask: true
				});
				let params = {
					pageSize: 100,
					pageIndex: 1,
					itemCode: data.itemCode,
					// locationTypes: [4], //成品库 4 deliverToCustomer
					// locationTypes: [2, 3, 4], //成品库 4
					inventoryStatus: [2], //库存状态为合格
					packingCode: data.packingCode
				};
				getBalancesByFilter(params)
					.then(res => {
						if (res === null) {
							this.showMessage('未查找到零件库存,不可以发货');
						} else {
							callback(res);
						}
						uni.hideLoading();
					})
					.catch(err => {
						this.showMessage(err.message);
						uni.hideLoading();
					});
			},

			getSelectedBalance(balanceItem) {
				let that = this;
				let deliverItem = that.createDeliverItem(that.scanData, balanceItem);
				that.itemList.unshift(deliverItem);
			},

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

			//提示是否移除选择的行?
			swipeClick(e, index) {
				let {
					content
				} = e;
				if (content.text === '移除') {
					uni.showModal({
						title: '提示',
						content: '是否移除选择的行?',
						success: res => {
							if (res.confirm) {
								this.itemList.splice(index, 1);
							}
						}
					});
				}
			},

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

			showMessage(message) {
				this.$refs.comMessage.showMessage(message);
			},
			//取消
			cancel() {
				this.active = 0;
				this.prodLine = {};
				this.locationCode = ''
				this.itemList = [];
				this.scanPlaceholder = '扫描目标库位';
			},

			//关闭
			close() {
				this.$emit("close");
			},

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

<style>
</style>
