<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="locationCode!=''">
						<view class="list_cell uni-flex uni-row space-between">
							<view>目标库位</view>
							<text>{{locationCode==''?'暂未扫描':locationCode}}</text>
						</view>
					</view>
					<view class="list_card">
						<view class="ljh_box" v-for="(item, index) in itemList">
							<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>
				<view class="pop_btn uni-flex uni-row space-between">
					<button class="cancel" @click="cancel">取消</button>
					<button class="save" @click="save">确定</button>
				</view>
				<com-message ref="comMessage"></com-message>
			</view>
		</scroll-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 {
		balances,
	} 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: '',
				placeholder: '扫描目标库位'
			};
		},
		props: {

		},
		mounted() {

		},
		onPullDownRefresh() {

		},
		methods: {
			openScanPopup(locationCode, itemList) {
				if (locationCode != '') {
					this.active = 1;
					this.locationCode = locationCode;
					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 //零件号
				{
					this.scanItem(result.data.code);
				}
			},

			//扫描库位
			scanLocation(code) {
				if (code == '') return;
				this.locationCode = code;
				this.afterScanlocation();
				return;
				let locationCode = code;
				if (locationCode != '') {
					locations(locationCode)
						.then(res => {
							if (res != null) {
								if (res.type != 5) {
									this.showMessage('请扫描线边库位');
								} else {
									this.locationCode = res.code;
									this.afterScanlocation();
								}
							} else {
								this.showMessage('未查询到生产线' + locationCode);
							}
						})
						.catch(err => {
							this.showMessage(err.message);
						});
				}
			},

			//扫描箱码
			scanItem(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(code);
						}
					});
				} else {
					that.getItem(code);
				}
			},


			getItem(code) {
				let that = this;
				uni.showLoading({
					title: "加载中....",
					mask: true
				});
				let params = {
					pageSize: 1000,
					pageIndex: 1,
					packingCode: code,
				};
				balances(params).then(res => {
					if (res.totalCount == 0) {
						that.showMessage('未查找到库存记录,不可以退货');
					} else if (res.totalCount == 1) {
						that.itemList.push(res.items[0])
					} else {
						that.showMessage('查询到多条库存记录,请在列表中移除不需要的记录');
						res.items.forEach(r => {
							that.itemList.push(r)
						})
					}
					// that.$refs.scanPopup.getfocus()
					uni.hideLoading();
				}).catch(ex => {
					uni.hideLoading();
					that.showMessage(ex.message);
				});
			},

			async getItemInfo(itemCode) {
				//按零件号查询零件
				let that = this;
				let itemInfo = await getItemAsync(itemCode);
				if (itemInfo != null) {} else {
					that.showMessage('未查询到零件' + itemCoe);
				}
			},

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

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

					this.itemList.forEach(r => {
						r.itemCode = r.code;
						r.item = {
							id: r.id,
							name: r.name,
							desc1: r.desc1,
							desc2: r.desc2,
						};
						r.qty.uom = r.basicUom,
							r.toLocationCode = that.locationCode;
						r.worker = localStorage.userName
					})
					this.$emit('confirm', this.itemList, this.locationCode)
				}
			},

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

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

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

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

<style>
</style>
