<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">{{customerAddress.warehouseCode}}</text>
					</view>
					<view class="font_sm">
						库位:
						<text class="text_bold">{{customerAddress.locationCode}}</text>
					</view>
				</view>
			</view>
		</view>

		<scroll-view :scroll-top="scrollTop" scroll-y="true" class="scroll-detail" @scrolltoupper="upper"
			@scrolltolower="lower" @scroll="scroll">
			<view class="detail-list margin_top" 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="false"></com-deliver-fg>
						</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-fg ref='scanPopup' @confirm='scanConfirm'></com-scan-deliver-fg>
	</view>
</template>

<script>
	import {
		getCustomerAddressByCode,
		getBalancesByFilter,
		finshDeliver
	} 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 comScanDeliverFg from '@/mycomponents/scan/comScanDeliverFg.vue'
	import comDeliverFg from '@/mycomponents/coms/store/comDeliverFg.vue'
	import comMessage from '@/mycomponents/common/comMessage.vue'
	import moment from 'moment'

	export default {
		name: 'receiptbyjob',
		components: {
			winScanButton,
			winBlankView,
			comScanDeliverFg,
			comDeliverFg,
			comMessage
		},
		data() {
			return {
				//滑动移除
				options: [],
				type: '',
				itemList: [],
				currentItem: {},
				scrollTop: 0,
				old: {
					scrollTop: 0
				},

				pageSize: 100,
				pageIndex: 1,
				submitting: false,
				customerAddress: {},
				scanData: {}
			};
		},
		props: {
			datacontent: {
				type: Object,
				value: null
			}
		},
		mounted() {
			this.options = getRemoveOption();
		},
		//返回首页
		onNavigationBarButtonTap(e) {
			if (e.index === 0) {
				goHome();
			}else if(e.index === 1){
				window.location.reload();
			}
		},
		methods: {
			openScanPopup() {
				this.$refs.scanPopup.openScanPopup(this.prodLine, 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(customerAddress, itemList) {
				this.customerAddress = customerAddress;
				this.itemList = itemList;
			},

			openedit(item) {
				this.currentItem = item;
				this.$refs['comqtyedit'].openPopup3(item.qty.qty, item.defaultQty.qty, false);
			},


			deleteItem() {
				let that = this;
				if (that.itemList.length === 0) {
					this.showMessage('没有要删除的数据')
				} else {
					let checkItems = that.itemList.filter(r => {
						return r.checked;
					})
					if (checkItems.length === 0) {
						this.showMessage('请选择要删除的数据')
					} else {
						showConfirmMsg('是否要将选择的行删除?', confirm => {
							if (confirm) {
								for (var i = that.itemList.length - 1; i >= 0; i--) {
									let item = that.itemList[i];
									if (item.checked) {
										that.itemList.splice(i, 1);
									}
								}
							}
						})
					}
				}
			},

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

			finsh() {
				let that = this;
				that.submitting = true;
				if (this.customerAddress.customerCode === undefined) {
					this.showMessage('请扫描客户地址')
					return;
				}
				uni.showLoading({
					title: "提交中..."
				});
				let deliverItem = {
					worker: localStorage.userName_CN ==""?localStorage.userName:localStorage.userName_CN,
					warehouseCode: localStorage.warehouseCode,
					Company: localStorage.company,
					company: localStorage.company,
					jobNumber: "FE2E4416-E5DC-B869-38DA-3A05028BE27D",
					customer: this.customerAddress.customerCode,
					customerAddressCode: this.customerAddress.code,
					number: "aaa",
					details: [] //子表
				};

				let cpde = this.customerAddress.locationCode;
				for (var i = 0; i < that.itemList.length; i++) {
					let data = that.itemList[i];
					let detail = {
						itemCode: data.itemCode,
						item: data.item,
						fromLot: data.lot,
						toLot: data.lot,
						batch: data.batch,
						fromPackingCode: data.packingCode,
						toPackingCode: data.packingCode,
						qty: data.qty,

						fromLocationCode: data.locationCode,
						toLocationCode: this.customerAddress.locationCode,

						fromWarehouseCode: localStorage.warehouseCode,
						toWarehouseCode: this.customerAddress.warehouseCode,

						fromStatus: data.status,
						toStatus: data.status,
						worker: localStorage.userName_CN ==""?localStorage.userName:localStorage.userName_CN,
					};

					deliverItem.details[i] = detail;
				}
				let params = JSON.stringify(deliverItem);
				console.log('param', params);
				finshDeliver(params)
					.then(res => {
						this.showMessage('提交成功')
						that.afterSubmit();
						that.hideLoading();
					})
					.catch(err => {
						this.showMessage(err.message)
						that.hideLoading();
					});
			},

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

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

			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">
	@import '../../common/pdabasic.css';
</style>
