<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="toContainerCode">
							<view>托盘号</view>
							<text class="text_bold">{{toContainerCode}}</text>
						</view>
					</view>
					<view class="pop_list list_info">
						<view class="detail-content" v-for="(item, index) in itemList" :key="item.id">
							<uni-swipe-action>
								<uni-swipe-action-item :right-options="options" :auto-close="false"
									@click="swipeClick($event,index)">
									<com-base-info :dataContent='item'></com-base-info>
								</uni-swipe-action-item>
							</uni-swipe-action>
						</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 {
		getInventoryTypeStyle,
		getInventoryStatusDesc,
		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,
	} from '@/api/index.js';

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

	export default {
		name: 'comScanUnpick',
		components: {
			comStepsCustom,
			winComScan,
			comBaseInfo,
			comMessage
		},
		data() {
			return {
				options: [],
				active: 0,
				list: [{
					title: '扫描托标签',
					num: "1"
				}, {
					title: '扫描箱标签',
					num: "2"
				}],
				itemList: [],
				toContainerCode: '',
				defaultPlaceholder: '扫描托标签',
				scanPlaceholder: ''
			};
		},
		props: {

		},
		filters: {
			statusStyle: function(val) {
				return getInventoryTypeStyle(val);
			},
			statusColor: function(val) {
				return getInventoryStatusDesc(val);
			},
		},
		mounted() {
			this.scanPlaceholder = this.defaultPlaceholder;
			this.options = getRemoveOption();
		},
		onPullDownRefresh() {

		},
		onLoad() {

		},
		methods: {
			openScanPopup() {
				this.$refs.popup.open('bottom')
			},
			closeScanPopup() {
				this.$refs.popup.close()
			},
			//提示是否移除选择的行?
			swipeClick(e, index) {
				let {
					content
				} = e;
				if (content.text === '移除') {
					uni.showModal({
						title: '提示',
						content: '是否移除选择的行?',
						success: res => {
							if (res.confirm) {
								this.itemList.splice(index, 1);
							}
						}
					});
				}
			},
			getScanResult(result) {
				if (this.active == 0) //托标签
				{
					this.scanContainer(result);
				} else //ERP料号
				{
					this.scanPack(result);
				}
			},

			//扫描托标签
			scanContainer(result) {
				let that = this;
				if (result.data.scanType === 'qrcode') {
					if (result.data.isPack === false) {
						that.toContainerCode = result.data.containerCode;
					} else {
						that.showMessage('请扫描托标签');
					}
				} else if (result.data.scanType === 'barcode') {
					that.toContainerCode = result.data.code;
				}
				if (that.toContainerCode != '') {
					that.afterScanContainer();
				}
			},

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

			scanPack(result) {
				let that = this;
				if (result.data.isPack) {
					//判断是否重复扫描
					let datas = that.itemList.filter(r => {
						return r.packingCode == result.data.packingCode && r.itemCode === result.data.itemCode
					})
					if (datas.length > 0) {
						showConfirmMsg('零件已经存在,是否要重新扫描?', confirm => {
							if (confirm) {
								that.itemList.forEach((r, i) => {
									if (r.packingCode == result.data.packingCode && r.itemCode === result
										.data.itemCode) {
										that.itemList.splice(i, 1);
										return;
									}
								});
								that.getInventory(result);
							}
						});
					} else {
						that.getInventory(result);
					}

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

			getInventory(result) {
				uni.showLoading({
					title: '加载中...',
					mask: true
				})
				let params = {
					pageSize: 1000,
					pageIndex: 1,
					packingCode: result.data.packingCode,
					itemCode: result.data.itemCode
				};
				balances(params).then((res) => {
					try {
						if (res.totalCount === 0) {
							this.showMessage('未查找到库存信息');
						} else {
							res.items.forEach(r => {
								if (r.containerCode != '') {
									showConfirmMsg('零件[' + r.itemCode + ']已经存在托码,是否仍要新建托盘?', confirm => {
										this.addItem(r, result);
									})
								} else {
									this.addItem(r, result);
								}
							})
						}
					} catch (e) {
						this.showMessage(e.message)
					}
					uni.hideLoading();
				}).catch((err) => {
					uni.hideLoading();
					this.showMessage(err.message);
				})
			},

			addItem(item, result) {
				item.fromLot = item.lot;
				item.toLot = item.lot;

				item.fromPackingCode = item.packingCode;
				item.toPackingCode = item.packingCode;

				item.fromContainerCode = item.containerCode;
				item.toContainerCode = '';

				item.fromLocationCode = item.locationCode;
				item.toLocationCode = item.locationCode;

				item.fromWarehouseCode = item.warehouseCode;
				item.toWarehouseCode = item.warehouseCode;

				item.fromStatus = item.status;
				item.toStatus = item.status;

				item.FromBatch = {
					supplierBatch: item.supplierBatch,
					produceDate: getCurrDate + 'T00:00:00'
				};

				item.ToBatch = {
					supplierBatch: item.supplierBatch,
					produceDate: getCurrDate + 'T00:00:00'
				};

				//标签打印的属性			
				item.itemCode = result.data.itemCode;
				item.asnNumber = result.data.asn;
				item.supplierCode = result.data.supplierCode;
				item.packingCode = result.data.packingCode;
				item.containerCode = result.data.containerCode;
				item.qty = item.qty;

				this.itemList.unshift(item);
			},

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

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

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

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


<style>
</style>
