<template>
	<uni-popup ref="popup">
		<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 nopad">
				<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" v-if="locationCode!=''">
				<view class="uni-list popuni_list">
					<view class="list_cell uni-flex uni-row space-between">
						<view>目标库位</view>
						<text>{{locationCode}}</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' :displayLocation="false" :displayStatus="false">
								</com-base-info>
							</uni-swipe-action-item>
						</uni-swipe-action>
					</view>
				</view>
			</scroll-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>
	</uni-popup>
</template>

<script>
	import {
		locations,
		getitems
	} from '@/api/index.js';
	import {
		getCurrDate,
		getRemoveOption,
		showConfirmMsg
	} 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'
	export default {
		name: 'winScanReceipt',
		components: {
			comStepsCustom,
			winComScan,
			comBaseInfo,
			comMessage
		},
		props: {
			title: {
				type: String,
				default: ''
			},
		},
		data() {
			return {
				options: [],
				active: 0,
				list: [{
					title: '扫描目标库位',
					num: "1"
				}, {
					title: '扫描箱标签',
					num: "2"
				}],
				locationCode: '',
				itemList: [],
				defaultPlaceholder: '目标库位',
				scanPlaceholder: ''
			}
		},
		created() {
			this.options = getRemoveOption();
		},
		mounted() {
			this.scanPlaceholder = this.defaultPlaceholder;
		},
		methods: {
			openScanPopup(locationCode, itemList) {
				if (itemList.length > 0) {
					this.locationCode = locationCode;
					this.itemList = itemList;
					this.active = 1;
					this.scanPlaceholder = '请扫描箱标签'
				}
				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) {
				let that = this;
				if (that.active == 0) //目标库位
				{
					if (result.data.code == '')
						return;
					that.getLocationResult(result);
				} else //目标库位
				{
					that.getPackResult(result)
				}
			},

			//获取地址
			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();
				})
			},

			getPackResult(result) {
				let that = this;
				//判断是否重复扫描
				let datas = [];
				if (!result.data.isPack) {
					this.showMessage('请扫描箱标签');
					return;
				} else {
					datas = that.itemList.filter(r => {
						return r.itemCode == result.data.itemCode &&
							r.packingCode == result.data.packingCode &&
							r.lot == result.data.lot
					})
				}
				if (datas.length > 0) {
					showConfirmMsg('零件已经存在,是否要重新扫描?', confirm => {
						if (confirm) {
							that.itemList.forEach((r, i) => {
								if (r.itemCode == result.data.itemCode &&
									r.packingCode == result.data.packingCode &&
									r.lot == result.data.lot) {
									that.itemList.splice(i, 1);
									return;
								}
							});
							that.addItemList(result.data);
						}
					});
				} else {
					that.addItemList(result.data);
				}
				that.scanResult = result.data;
			},

			addItemList(item) {
				uni.showLoading({
					title: '加载中...',
					mask: true
				})
				let that = this;
				getitems(item.itemCode).then((res) => {
					try {
						if (res != null) {
							item.uom = res.basicUom; //basicUom
							item.item = {
								id: res.id,
								name: res.name,
								desc1: res.desc1,
								desc2: res.desc2
							};
							let produceData = getCurrDate() + 'T00:00:00'
							item.batch = {
								supplierBatch: item.supplierBatch,
								produceDate: produceData //生产日期默认当天
							};
							item.status = res.status;
							if (item.qty == null) {
								item.qty = 0;
							}
							that.itemList.unshift(item);
						} else {
							this.showMessage('未查找到零件信息')
						}
					} catch (e) {
						this.showMessage(e.message)
					}
					uni.hideLoading();
				}).catch((err) => {
					this.showMessage('未查找到零件信息')
					uni.hideLoading();
				})
			},

			save() {
				if (this.locationCode == '')
					return;
				if (this.itemList.length == 0)
					return;
				this.$emit('confirm', this.locationCode, this.itemList)
				this.closeScanPopup();
			},
			clear() {
				showConfirmMsg('是否要清空所有数据?', confirm => {
					if (confirm) {
						this.itemList = [];
						this.locationCode = '';
						this.active = 0;
						this.scanPlaceholder = '请扫描目标库位'
					}
				})
			},
			showMessage(message) {
				this.$refs.comMessage.showMessage(message);
			},
		}
	}
</script>

<style>
</style>
