<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="closeScanPopup()">关闭</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 ref="scancom" @getScanResult="getScanResult" :placeholder="scanPlaceholder"
							:boxFocus="true" :clearResult="true"></win-com-scan>
					</view>

					<view class="uni-list popuni_list" v-if="prodLine!=null">
						<view class="list_cell uni-flex uni-row space-between">
							<view>生产线</view>
							<text>{{prodLine.code}}</text>
						</view>
						<view class="list_cell uni-flex uni-row space-between">
							<view>需求库位</view>
							<text>{{prodLine.rawLocation}}</text>
						</view>
						<view class="list_cell uni-flex uni-row space-between">
							<view>需求数量</view>
							<uni-easyinput ref='qtyInput' type="number" placeholder="请填写"
								placeholderStyle="font-size:0.875rem" @input="allQtyInput" @confirm="qtyConfirm">
							</uni-easyinput>
						</view>
					</view>
					<view class="list_card">
						<view class="ljh_box" v-for="(item, index) in itemList">
							<com-replen :dataContent="item" :editQty="true"></com-replen>
						</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必须放在最下层 -->
				<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 comReplen from '@/mycomponents/coms/task/comReplen.vue'
	import comMessage from '@/mycomponents/common/comMessage.vue'
	import {
		getItemAsync,
		getprodlineitem,
		checkIssueJobExistAsync
	} from '@/api/index.js';

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

	export default {
		name: 'comScanReplen',
		components: {
			comStepsCustom,
			winComScan,
			comReplen,
			comMessage
		},
		data() {
			return {
				options2: [{
					text: '移除',
					style: {
						backgroundColor: '#F56C6C'
					}
				}],
				active: 0,
				list: [{
					title: '扫描生产线',
					num: "1"
				}, {
					title: '扫描零件',
					num: "2"
				}],
				itemList: [],
				prodLine: {},
				locationCode: '',
				scanPlaceholder: '扫描生产线'
			};
		},
		props: {

		},
		mounted() {

		},
		onPullDownRefresh() {

		},
		methods: {
			openScanPopup(prodLine, itemList) {
				if (itemList.length > 0) {
					this.prodLine = prodLine;
					this.locationCode = prodLine.rawLocation;
					this.itemList = itemList;
					this.active = 1
				}
				this.$refs.popup.open('bottom')
			},
			closeScanPopup() {
				this.$refs.popup.close()
			},
			getScanResult(result) {
				if (this.active == 0) //库位
				{
					this.scanProdLine(result);
				} else //零件号
				{
					this.scanItem(result);
				}
			},

			//扫描库位
			scanProdLine(result) {
				uni.showLoading({
					title: '正在扫描',
					mask: true
				})

				let prodCode = result.data.code;
				if (prodCode != '') {
					getprodlineitem(prodCode)
						.then(res => {
							if (res != null) {
								this.prodLine = res;
								this.locationCode = res.rawLocation;
								//rawLocation
								this.afterProdLine();
							} else {
								this.showMessage('未查询到生产线' + locationCode);
							}
							uni.hideLoading();
						})
						.catch(err => {
							this.showMessage(err.message);
							uni.hideLoading();
						});
				}
			},

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

			//扫描零件号
			scanItem(result) {
				let itemCode = '';
				if (result.data.scanType === 'qrcode') {
					itemCode = result.data.itemCode;
				} else if (result.data.scanType === 'barcode') {
					itemCode = result.data.code;
				}

				if (itemCode === null || itemCode === '') {
					this.showMessage('未解析到零件号');
					return;
				} else {
					let that = this;
					let items = that.itemList.filter(r => {
						return r.code === itemCode
					});
					if (items.length > 0) {
						showConfirmMsg('零件已经存在,是否要请重新扫描?', confirm => {
							if (confirm) {
								that.deleteItemCode(itemCode);
								that.getItemInfo(itemCode);
							}
						});
					} else {
						that.getItemInfo(itemCode);
					}
				}
			},

			async getItemInfo(itemCode) {
				//按零件号查询零件
				let that = this;
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				let itemInfo = await getItemAsync(itemCode);
				if (itemInfo != null) {
					let params = {
						itemCode: itemCode,
						locationCode: that.locationCode
					};
					let issueInfo = await checkIssueJobExistAsync(params);
					if (issueInfo.length > 0) {
						showConfirmMsg('[' + itemCode + ']到库位[' + that.locationCode + ']已存在发料任务,是否继续补料',
							confirm => {
								if (confirm) {
									that.addItemInfo(itemInfo);
								}
							})
					} else {
						that.addItemInfo(itemInfo);
					}
				} else {
					that.showMessage('未查询到零件' + itemCoe);
				}
				uni.hideLoading();
			},

			addItemInfo(item) {
				item.qty = {
					qty: 1,
					uom: ''
				};
				this.itemList.unshift(item)
			},

			//需求数量
			allQtyInput(e) {
				let that = this;
				that.itemList.forEach(r => {
					r.qty.qty = Number(e)
				})
			},

			//删除已经扫描的零件
			deleteItemCode(itemCode) {
				let that = this;
				that.itemList.forEach((r, i) => {
					if (r.code === itemCode) {
						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.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.closeScanPopup();
					this.$emit('confirm', this.prodLine, this.itemList)
					this.cancel();
				}
			},

			//取消
			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>
