<template>
	<uni-popup ref="popup">
		<scroll-view scroll-y="true" class="scroll-Y">
			<view class="popup_box">
				<view class="pop_title">
					扫描信息
					<text class="fr" @click="close()">关闭</text>
				</view>
				<view class="uni-steps">
					<com-steps-custom :options="list" :active="active"></com-steps-custom>
				</view>
				<view class="pop_tab">
					<view class="tab_info tab_notitle">
						<win-com-scan @getScanResult="getScanResult" :placeholder="placeholder"></win-com-scan>
					</view>

					<view class="uni-list popuni_list" v-if="location!=null">
						<view class="list_cell uni-flex uni-row space-between">
							<view>目标库位</view>
							<text>{{location.code==''?'暂未扫描':location.code}}</text>
						</view>
					</view>
					<view class="list_card" v-for="(item, index) in itemList">
						<view class="ljh_box nopad">
							<uni-swipe-action>
								<uni-swipe-action-item :right-options="options2" :auto-close="false"
									@click="swipeClick($event,index)">
									<dy-item-info :dataContent='item'></dy-item-info>
								</uni-swipe-action-item>
							</uni-swipe-action>
						</view>
					</view>
				</view>

				<com-message ref="comMessage"></com-message>
			</view>
		</scroll-view>
		<view class="pop_btn uni-flex uni-row space-between" style="background-color: #fff;">
			<button class="cancel" @click="cancel">取消</button>
			<button class="save" @click="save">确定</button>
		</view>
	</uni-popup>
</template>

<script>
	import comStepsCustom from '@/mycomponents/common/comStepsCustom.vue'
	import winComScan from '@/mycomponents/wincom/winComScan.vue'
	import dyItemInfo from '@/mycomponents/dycom/dyItemInfo.vue'
	import comMessage from '@/mycomponents/common/comMessage.vue'
	import {
		locations
	} from '@/api/index.js';

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

	export default {
		name: 'comScanPutaway',
		components: {
			comStepsCustom,
			winComScan,
			dyItemInfo,
			comMessage
		},
		data() {
			return {
				active: 0,
				list: [{
					title: '目标库位',
					num: "1"
				}, {
					title: '箱标签',
					num: "2"
				}],
				options2: [{
					text: '移除',
					style: {
						backgroundColor: '#F56C6C'
					}
				}],
				itemList: [],
				locationCode: '',
				location: null,
				placeholder: '目标库位'
			};
		},
		props: {

		},
		mounted() {

		},
		onPullDownRefresh() {

		},
		methods: {
			openScanPopup(location, itemList) {
				if (location != null) {
					this.active = 1;
				} else {
					this.active = 0;
				}
				this.location = location;
				this.itemList = itemList;
				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.scanLocation(result.data.code);
				} else //ERP料号
				{
					this.scanItem(result);
				}
			},

			//扫描库位
			scanLocation(code) {
				if (code == '') return;
				let locationCode = code;
				if (locationCode != '') {
					uni.showLoading({
						title: '扫描中...',
						mask: true
					})
					locations(locationCode)
						.then(res => {
							if (res != null) {
								if (res.type != 2) {
									this.showMessage('目标库位必须是原材料库')
								} else {
									location
									this.location = res;
									this.afterScanlocation();
								}
							} else {
								this.showMessage('未查询到库位' + locationCode);
							}
							uni.hideLoading();
						})
						.catch(err => {
							this.showMessage(err.message);
							uni.hideLoading();
						});
				}
			},

			//扫描箱码
			scanItem(result) {
				let code = result.data.code;
				if (code == '') return;
				let that = this;
				let items = that.itemList.filter(r => {
					return r.packingCode === code
				});
				if (items.length > 0) {
					showConfirmMsg('零件已经存在,是否要请重新扫描?', confirm => {
						if (confirm) {
							that.deleteItemCode(code);
							that.getItem(result);
						}
					});
				} else {
					that.getItem(result);
				}
			},

			//删除已经扫描的零件
			deleteItemCode(code) {
				let that = this;
				that.itemList.forEach((r, i) => {
					if (r.packingCode == code) {
						that.itemList.splice(i, 1);
						return;
					}
				});
				this.$forceUpdate();
			},

			getItem(result) {
				let data = this.createItem(result);
				this.itemList.unshift(data);
			},

			createItem(item) {
				let data = {
					packingCode: item.code,
					itemCode: item.itemCode,
					itemName: item.itemName,
					itemDesc1: item.itemDesc1,
					itemDesc2: item.itemDesc2,
					lot: item.lot,
					expireDate: item.expireDate,
					stdPackUom: item.stdPackUom,
					qty: item.qty,
					uom: item.uom,
					remark: item.remark,
					arriveDate: item.arriveDate,
					stdPackQty: item.stdPackQty,
					supplierBatch: item.supplierBatch,
					status: 2, //合格		 
					reasonCode: '',

					locationErpCode: this.location.erpLocationCode,
					locationCode: this.location.code,

					//标签中没有的属性
					locationArea: item.locationArea,
					locationGroup: item.locationGroup,
					produceDate: item.produceDate,
					containerCode: '',
					warehouseCode: localStorage.warehouseCode,
				}
				return data;
			},

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

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

			//取消
			cancel() {
				this.active = 0;
				this.location = null;
				this.itemList = [];
				this.placeholder = '目标库位';
			},

			//关闭
			close() {
				this.closeScanPopup();
			},

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

<style>
</style>
