<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">
				<view class="tab_info tab_notitle">
					<win-com-scan ref="scancom" v-if="active==0" @getScanResult="getCustomerResult"
						:placeholder="scanPlaceholder" :boxFocus="true" :clearResult="true"></win-com-scan>
					<win-com-scan-product v-else="active==1" ref="scanProduct" @getScanResult="getProductResult"
						:placeholder="scanPlaceholder" :boxFocus="true" :clearResult="true"></win-com-scan-product>
				</view>
			</view>

			<scroll-view scroll-y="true" class="scroll-Y scan_scroll normal_scroll">
				<!-- 回显客户地址 -->
				<view class="top_wrap" style="margin-top: -10px;" v-if="customerAddress.warehouseCode!=undefined">
					<view class="top_card">
						<view class="uni-flex space-between top_lines_info">
							<view class="font_sm">
								仓库:
								<text class="text_bold">{{customerAddress.warehouseCode}}</text>
							</view>
							<view class="font_sm">
								库位:
								<text class="text_bold">{{customerAddress.locationCode}}</text>
							</view>
						</view>
					</view>
				</view>
				<!-- 回显产品标签 -->
				<view class="detail-list pop_list" v-for="(item, index) in itemList" :key="item.id">
					<view class="detail-content">
						<uni-swipe-action>
							<uni-swipe-action-item :right-options="options" :auto-close="false"
								@click="swipeClick($event,index)">
								<com-deliver-fg :dataContent="item" :editQty="true" @qtyInput='qtyInput'>
								</com-deliver-fg>
							</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>
		<com-balance ref="balanceItems" @selectedItem='getSelectedBalance'></com-balance>
		
		<!-- com-message必须放在最下层 -->
		<com-message ref="comMessage"></com-message>
	</uni-popup>
</template>

<script>
	import {
		getCustomerAddressByCode,
		getBalancesByFilter,
	} from '@/api/index.js';

	import comStepsCustom from '@/mycomponents/common/comStepsCustom.vue'
	import winComScan from '@/mycomponents/wincom/winComScan.vue'
	import winComScanProduct from '@/mycomponents/wincom/winComScanProduct.vue'
	import comDeliverFg from '@/mycomponents/coms/store/comDeliverFg.vue'
	import comBalance from '@/mycomponents/common/comBalance.vue'
	import comMessage from '@/mycomponents/common/comMessage.vue'

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

	export default {
		name: 'comScanDeliverFg',
		components: {
			comStepsCustom,
			winComScan,
			winComScanProduct,
			comDeliverFg,
			comBalance,
			comMessage
		},
		data() {
			return {
				//滑动移除
				options: [],
				active: 0,
				list: [{
					title: '扫描客户地址',
					num: "1"
				}, {
					title: '扫描产品标签',
					num: "2"
				}],
				customerAddress: {},
				itemList: [],
				scanData: {},
				locationCode: '',
				scanPlaceholder: '扫描客户地址'
			};
		},
		props: {

		},

		mounted() {
			this.options = getRemoveOption();
		},
		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()
			},

			//获取客户地址信息
			getCustomerResult(result) {
				let that = this;
				let customerCode = result.data.code;
				if (customerCode === '') {
					that.showMessage('未解析到客户代码地址');
					return;
				} else {
					getCustomerAddressByCode(customerCode).then(res => {
						if (res != null) {
							that.customerAddress = res;
							that.afterScanCustomer();
						} else {
							that.showMessage('未查询到客户地址');
						}
					}).catch((err => {
						that.showMessage(err.message);
						uni.hideLoading();
					}));
				}
			},

			//产品标签
			getProductResult(result) {
				let that = this;
				if (result.data != null) {
					that.scanData = result.data;
					//按照零件号+箱码+批次去重
					let datas = that.itemList.filter(function(r) {
						if (r.itemCode === result.data.itemCode &&
							r.lot === result.data.lot &&
							r.packingCode === result.data.packingCode) {
							return r;
						}
					});

					if (datas.length > 0) {
						//列表中已经存在
						showConfirmMsg('零件已经存在,是否要重新扫描?', confirm => {
							if (confirm) {
								that.itemList.forEach((r, i) => {
									if (r.itemCode === result.data.itemCode &&
										r.lot === result.data.lot &&
										r.packingCode === result.data.packingCode) {
										that.itemList.splice(i, 1);
										return;
									}
								});
							}
							that.getDeliverItem(result.data);
						});
					} else {
						that.getDeliverItem(result.data);
					}
				} else {
					this.showMessage('标签格式不正确,请重新扫描');
				}
			},

			getDeliverItem(data) {
				let that = this;
				that.getBalance(data, item => {
					if (item.items.length == 0) {
						this.showMessage('在成品库未查找到该零件的库存');
					} else if (item.items.length == 1) {
						let deliverItem = that.createDeliverItem(data, item.items[0]);
						that.itemList.unshift(deliverItem);
					} else {
						this.$refs.balanceItems.openPopup(item.items);
					}
				});
			},

			//获取零件库存
			getBalance(data, callback) {
				uni.showLoading({
					title: "加载中....",
					mask: true
				});

				let params = {
					pageSize: 100,
					pageIndex: 1,
					itemCode: data.itemCode,
					// locationTypes: [2, 3, 4], //成品库 4 deliverToCustomer
					// inventoryStatus: [2], //库存状态为合格
					packingCode: data.packingCode
				};
				getBalancesByFilter(params)
					.then(res => {
						if (res === null) {
							this.showMessage('未查找到零件库存,不可以发货');
						} else {
							callback(res);
						}
						uni.hideLoading();
					})
					.catch(err => {
						this.showMessage(err.message);
						uni.hideLoading();
					});

			},

			//创建收货item
			createDeliverItem(data, balanceItem) {
				let deliveritem = {
					itemCode: balanceItem.itemCode,
					item: {
						id: balanceItem.id,
						name: balanceItem.itemName,
						desc1: balanceItem.item.desc1,
						desc2: balanceItem.item.desc2
					},
					lot: balanceItem.lot,
					batch: balanceItem.batch,
					packingCode: balanceItem.packingCode,
					qty: {
						uom: balanceItem.qty.uom,
						qty: balanceItem.qty.qty
					},
					locationCode: balanceItem.locationCode,
					warehouseCode: localStorage.warehouseCode,
					balanceQty: balanceItem.qty.qty,
					status: balanceItem.status
				};
				return deliveritem;
			},

			getSelectedBalance(balanceItem) {
				let deliverItem = this.createDeliverItem(this.scanData, balanceItem);
				this.itemList.unshift(deliverItem);
				// this.$forceUpdate();
			},

			//扫描完客户地址的下一步动作
			afterScanCustomer() {
				this.active += 1;
				this.scanPlaceholder = '扫描产品标签';
			},

			qtyInput(value, item) {
				let qty = Number(value);
				if (qty > item.balanceQty) {
					this.showMessage('发货数量不能大于[' + item.balanceQty + ']')
					setTimeout(() => {
							item.qty.qty = item.balanceQty
						},
						100)
				}
			},

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

			//取消
			cancel() {
				this.active = 0;
				this.customerAddress = {};
				this.itemList = [];
				this.scanPlaceholder = '扫描客户地址';
			},

			//关闭
			close() {
				this.$emit("close");
			},

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

<style>
</style>
