<template>
	<page-meta root-font-size="16px"></page-meta>
	<view class="">
		<win-blank-view @goScan='openScanPopup' v-if="itemList.length==0"></win-blank-view>
		<scroll-view :scroll-top="scrollTop" scroll-y="true" class="scroll-detail" @scrolltoupper="upper"
			@scrolltolower="lower" @scroll="scroll">
			<view class="detail-list" v-for="(item,index) in itemList">
				<view class="uni-flex uni-row">
					<view style="-webkit-flex: 1;flex: 1;">
						<view class="detail-content ">
							<uni-swipe-action>
								<uni-swipe-action-item :right-options="options" :auto-close="false"
									@click="swipeClick($event,index)">
									<com-base-info :dataContent='item'></com-base-info>
								</uni-swipe-action-item>
							</uni-swipe-action>
						</view>
					</view>
				</view>
			</view>
		</scroll-view>
		<view class="new_btn_bot" v-if="itemList.length>0">
			<button class="new_save_btn" @click="submit()">提交</button>
		</view>
		<comMessage ref="comMessage"></comMessage>
		<win-scan-button @goScan='openScanPopup' v-if="itemList.length>0"></win-scan-button>
		<win-scan-by-pack ref="scanPopup" @getScanResult='getScanResult'></win-scan-by-pack>
	</view>
</template>

<script>
	import {
		balances,
		inventoryTransfer,
		getContainerCode
	} from '@/api/index.js';
	import {
		printTBQLabel
	} from '@/common/print.js'
	import {
		showConfirmMsg,
		goHome,
		getCurrDate,
		getRemoveOption
	} from '@/common/basic.js';

	import winBlankView from '@/mycomponents/wincom/winBlankView.vue'
	import winScanButton from '@/mycomponents/wincom/winScanButton.vue'
	import winScanByPack from '@/mycomponents/wincom/winScanByPack.vue'
	import comBaseItem from '@/mycomponents/comItem/comBaseItem.vue'
	import comMessage from '@/mycomponents/common/comMessage.vue'

	export default {
		name: 'transfer',
		components: {
			winBlankView,
			winScanButton,
			winScanByPack,
			comBaseInfo,
			comMessage
		},
		data() {
			return {
				//滑动移除
				options: [],
				itemList: [],
				scrollTop: 0,
				old: {
					scrollTop: 0
				},
				toContainerCode: '',
				submitting: false,
				locationCode: ''
			};
		},
		mounted() {
			this.options = getRemoveOption();
		},
		onLoad() {},
		onNavigationBarButtonTap(e) {
			if (e.index === 0) {
				goHome();
			}else if(e.index === 1){
				window.location.reload();
			}
		},
		filters: {
			statusStyle: function(val) {
				return getInventoryTypeStyle(val);
			},
			statusColor: function(val) {
				return getInventoryStatusDesc(val);
			},
		},
		methods: {
			openScanPopup() {
				this.$refs.scanPopup.openScanPopup();
			},
			//提示是否移除选择的行?
			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 (!result.data.isPack) {
					this.showMessage('请扫描箱标签');
					return;
				}
				if (result.data.itemCode === null) {
					this.showMessage('请包含零件号');
					return;
				}
				//判断是否重复扫描
				let datas = that.itemList.filter(r => {
					return r.packingCode == result.data.packingCode && r.itemCode === result.data.itemCode
				})
				if (datas.length > 0) {
					showConfirmMsg('零件已经存在,是否要重新扫描?', confirm => {
						if (confirm) {
							that.itemList.forEach((r, i) => {
								if (r.packingCode == result.data.packingCode && r.itemCode === result.data
									.itemCode) {
									that.itemList.splice(i, 1);
									return;
								}
							});
							that.getInventory(result);
						}
					});
				} else {
					that.getInventory(result);
				}
			},

			getInventory(result) {
				uni.showLoading({
					title: '加载中...',
					mask: true
				})
				let params = {
					pageSize: 1000,
					pageIndex: 1,
					packingCode: result.data.packingCode,
					itemCode: result.data.itemCode
				};
				balances(params).then((res) => {
					try {
						if (res.totalCount === 0) {
							this.showMessage('未查找到库存信息');
						} else {
							res.items.forEach(r => {
								this.addItem(r, result);
							})
						}
					} catch (e) {
						this.showMessage(e.message)
					}
					uni.hideLoading();
				}).catch((err) => {
					uni.hideLoading();
					this.showMessage(err.message);
				})
			},

			addItem(item, result) {
				item.fromLot = item.lot;
				item.toLot = item.lot;

				item.fromPackingCode = item.packingCode;
				item.toPackingCode = item.packingCode;

				item.fromContainerCode = item.containerCode;
				item.toContainerCode = '';

				item.fromLocationCode = item.locationCode;
				item.toLocationCode = item.locationCode;

				item.fromWarehouseCode = item.warehouseCode;
				item.toWarehouseCode = item.warehouseCode;

				item.fromStatus = item.status;
				item.toStatus = item.status;

				item.FromBatch = {
					supplierBatch: item.supplierBatch,
					produceDate: getCurrDate() + 'T00:00:00'
				};

				item.ToBatch = {
					supplierBatch: item.supplierBatch,
					produceDate: getCurrDate() + 'T00:00:00'
				};

				//标签打印的属性			
				item.itemCode = result.data.itemCode;
				item.asnNumber = result.data.asn;
				item.supplierCode = result.data.supplierCode;
				item.packingCode = result.data.packingCode;
				// item.containerCode = result.data.containerCode;
				item.qty = item.qty;
				this.itemList.push(item);
			},

			submit() {
				let that = this;
				if (that.itemList.length === 0) {
					this.showMessage('请扫描箱标签');
					return;
				}
				//判断是否有不同的库位
				let locationCode = that.itemList[0].locationCode;
				let count = that.itemList.filter(r => r.locationCode === locationCode).length;
				if (count != that.itemList.length) {
					this.showMessage('必须是相同库位,才可以合托');
					return;
				}

				let itemCodes = '';
				that.itemList.forEach(r => {
					if (r.containerCode != '' && r.containerCode != null) {
						itemCodes += r.itemCode + ',';
					}
				})
				if (itemCodes != '') {
					showConfirmMsg('零件[' + itemCodes + ']已经存在托码,是否仍要合托?', confirm => {
						if (confirm) {
							this.submiting();
						}
					})
				} else {
					this.submiting();
				}
			},

			submiting() {
				let that = this;
				that.submitting = true;
				uni.showLoading({
					title: "提交中..."
				});
				//返回一个托标签
				let qty = 1
				getContainerCode(qty).then((res) => {
					if (res.codes.length === 0) {
						this.showMessage('未返回箱标签');
						that.hideLoading();
						return;
					} else {
						that.toContainerCode = res.codes[0];
						that.handleInventoryTransfer();
					}
				}).catch((err) => {
					this.showMessage(err.message);
					that.hideLoading();
				})
			},

			handleInventoryTransfer() {
				let that = this;
				let item = {
					worker: localStorage.userName,
					warehouseCode: "",
					jobNumber: "",
					supplierCode: "",
					company: localStorage.company,
					number: "",
					details: []
				}

				that.itemList.forEach(r => {
					r.toContainerCode = that.toContainerCode;
					r.containerCode = that.toContainerCode;
					r.worker = localStorage.userName
					item.details.push(r);
				})
				let params = JSON.stringify(item);
				console.log('params', params);
				inventoryTransfer(params)
					.then(res => {
						that.showMessage('提交成功');
						showConfirmMsg('是否打印托标签?',
							confirm => {
								if (confirm) {
									printTBQLabel(item.details);
								}
							});
						that.clearInfo();
						that.hideLoading();
					})
					.catch(err => {
						this.showMessage(err.message);
						that.hideLoading();
					});
			},

			hideLoading() {
				this.submitting = false;
				uni.hideLoading();
			},

			clearInfo() {
				let that = this;
				that.itemList = [];
				that.toContainerCode = '';
			},
			showMessage(message) {
				this.$refs.comMessage.showMessage(message);
			},
			upper: function(e) {
				// console.log(e)
			},
			lower: function(e) {
				// console.log(e)
			},
			scroll: function(e) {
				// console.log(e)
				this.old.scrollTop = e.detail.scrollTop;
			},
		}
	};
</script>

<style scoped lang="scss">
</style>
