<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 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>
		</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>
		<com-scan-create-tp ref='scanPopup' @goScan='openScanPopup' @confirm='afterScan'>
		</com-scan-create-tp>
	</view>
</template>

<script>
	import {
		balances,
		inventoryTransfer,
		getContainerAsync
	} from '@/api/index.js';
	import {
		printTBQLabel
	} from '@/common/print.js'

	import winBlankView from '@/mycomponents/wincom/winBlankView.vue'
	import winScanButton from '@/mycomponents/wincom/winScanButton.vue'
	import comScanCreateTp from '@/mycomponents/scan/comScanCreateTp.vue'
	import comBaseItem from '@/mycomponents/comItem/comBaseItem.vue'
	import comMessage from '@/mycomponents/common/comMessage.vue'
	import {
		getInventoryTypeStyle,
		getInventoryStatusDesc,
		showConfirmMsg,
		goHome,
		getCurrDate,
		getRemoveOption
	} from '@/common/basic.js';
	export default {
		name: 'transfer',
		components: {
			//winScanByCode,
			comMessage,
			winBlankView,
			winScanButton,
			comScanCreateTp,
			comBaseInfo
		},
		data() {
			return {
				options: [],
				itemList: [],
				scrollTop: 0,
				old: {
					scrollTop: 0
				},
				toContainerCode: '',
				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() {
				if (this.itemList.length > 0) {
					showConfirmMsg('已经有零件信息,是否要重新扫描?', confirm => {
						if (confirm) {
							this.itemList = [];
							this.$refs.scanPopup.openScanPopup();
						}
					})
				} else {
					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);
							}
						}
					});
				}
			},

			afterScan(items) {
				this.itemList = items;
			},

			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 status = that.itemList[0].status;
				let statusCount = that.itemList.filter(r => r.status === status).length;

				if (statusCount != that.itemList.length) {
					this.showMessage('必须状态相同,才可以新建托盘');
					return;
				}


				that.handleInventoryTransfer();
			},

			handleInventoryTransfer() {
				let that = this;
				uni.showLoading({
					title: "提交中...",
					mask: true
				});
				let item = {
					worker: localStorage.userName,
					warehouseCode: "",
					jobNumber: "",
					supplierCode: "",
					company: localStorage.company,
					number: "",
					details: []
				}

				that.itemList.forEach(r => {
					r.toContainerCode = 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('提交成功');
						that.clearInfo();
						that.hideLoading();
					})
					.catch(err => {
						this.showMessage(err.message);
						that.hideLoading();
					});
			},

			hideLoading() {
				uni.hideLoading();
			},

			clearInfo() {
				let that = this;
				that.itemList = [];
				that.$refs.itemBalances.getfocus();
				that.$refs.comScanTP.clear();
				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>
