<template>
	<page-meta root-font-size="16px"></page-meta>
	<view class="">
		<win-blank-view @goScan='openScanPopup' v-if="itemList.length==0"></win-blank-view>
		<view class="top_wrap" v-if="itemList.length>0">
			<view class="top_card">
				<view class="uni-flex space-between top_lines_info">
					<view class="font_sm">
						目标库位:
						<text class="text_bold">{{locationCode}}</text>
					</view>
				</view>
			</view>
		</view>

		<scroll-view scroll-y="true" class="scroll-detail margin_top">
			<view class="detail-list count_shadow" v-for="(item, index) in itemList" :key="item.id"
				style="margin-top: 4rpx;">
				<view class="detail-content">
					<uni-swipe-action>
						<uni-swipe-action-item :right-options="options" :auto-close="false"
							@click="swipeClick($event,index)">
							<com-deliver-tf :dataContent="item" :editQty="false"></com-deliver-tf>
						</uni-swipe-action-item>
					</uni-swipe-action>
				</view>
			</view>
		</scroll-view>
		<view class="new_btn_bot" v-if="itemList.length>0">
			<button :disabled="submitting" type="primary" 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-deliver-tf ref='scanPopup' @confirm='scanConfirm'></com-scan-deliver-tf>
	</view>
</template>

<script>
	import {
		getBalancesByFilter,
		warehouseTransfer
	} from '@/api/index.js';
	import {
		showConfirmMsg,
		goHome,
		getISODateTime,
		getRemoveOption
	} from '@/common/basic.js';

	import winScanButton from '@/mycomponents/wincom/winScanButton.vue'
	import winBlankView from '@/mycomponents/wincom/winBlankView.vue'
	import comScanDeliverTf from '@/mycomponents/scan/comScanDeliverTf.vue'
	import comDeliverTf from '@/mycomponents/coms/store/comDeliverTf.vue'
	import comMessage from '@/mycomponents/common/comMessage.vue'

	export default {
		name: 'receiptbyjob',
		components: {
			winScanButton,
			winBlankView,
			comScanDeliverTf,
			comDeliverTf,
			comMessage,
		},
		data() {
			return {
				//滑动移除
				options: [],
				locationCode: '',
				itemList: [],
				pageSize: 100,
				pageIndex: 1,
				submitting: false,
				scanData: {},
				location: {}
			};
		},
		props: {
			datacontent: {
				type: Object,
				value: null
			}
		},
		mounted() {
			this.options = getRemoveOption();
		},
		onShow() {

		},
		//返回首页
		onNavigationBarButtonTap(e) {
			if (e.index === 0) {
				goHome();
			}
		},
		methods: {
			openScanPopup() {
				this.$refs.scanPopup.openScanPopup(this.locationCode, this.itemList);
			},

			//提示是否移除选择的行?
			swipeClick(e, index) {
				let {
					content
				} = e;
				if (content.text === '移除') {
					uni.showModal({
						title: '提示',
						content: '是否移除选择的行?',
						success: res => {
							if (res.confirm) {
								this.itemList.splice(index, 1);
							}
						}
					});
				}
			},

			scanConfirm(locationCode, itemList) {
				this.locationCode = locationCode;
				this.itemList = itemList;
			},

			submit() {
				let that = this;
				if (that.itemList.length === 0) {
					this.showMessage('请扫描产品标签');
					return;
				}
				that.finsh();
			},

			finsh() {
				let that = this;
				that.submitting = true;
				uni.showLoading({
					title: "提交中..."
				});

				let item = {
					worker: localStorage.userName,
					warehouseCode: localStorage.warehouseCode,
					jobNumber: "",
					supplierCode: "",
					company: localStorage.company,
					number: "",
					details: []
				}

				this.itemList.forEach(r => {
					r.fromLocationCode = r.locationCode;
					r.toLocationCode = that.location.code;
					r.fromStatus = r.status;
					r.toStatus = r.status;
					r.fromLot = r.lot;
					r.toLot = r.lot;
					r.fromPackingCode = r.packingCode;
					r.toPackingCode = r.packingCode;
					r.fromContainerCode = r.containerCode;
					r.toContainerCode = r.containerCode;
					r.fromWarehouseCode = r.warehouseCode;
					r.toWarehouseCode = that.location.warehouseCode;
					r.worker = localStorage.userName;
					item.details.push(r);
				})

				let params = JSON.stringify(item);
				console.log('params', params);
				warehouseTransfer(params)
					.then(res => {
						that.showCommitSuccess();
						that.afterSubmit();
						that.hideLoading();
					})
					.catch(err => {
						that.showMessage(err.message);
						that.hideLoading();
					});
			},

			afterSubmit() {
				this.itemList = [];
				this.$refs.comscanLocation.clear();
				this.location = {};
			},

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

			showMessage(message) {
				this.$refs.comMessage.showMessage(message);
			},

			showCommitSuccess() {
				this.$refs.comMessage.showCommitSuccess();
			},
			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">
	@import '../../common/pdabasic.css';
</style>
