<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-Y">
			<view class="detail-list margin_top" v-for="(item, index) in itemList">
				<uni-swipe-action>
					<uni-swipe-action-item :right-options="options2" :auto-close="false"
						@click="swipeClick($event,index)">
						<com-base-item :dataContent='item' :displayBottomInfo="false"></com-base-item>
					</uni-swipe-action-item>
				</uni-swipe-action>
			</view>
		</scroll-view>

		<div class="new_bot_box" v-show="itemList.length>0">
			<win-collapse-location @getLocationCode='getToLocation' @clear='clear'></win-collapse-location>
			<view class="new_btn_bot bot_pos uni-flex">
				<button class="new_clear_btn btn_double" @click="cancel()">清空</button>
				<button class="new_save_btn btn_double" @click="submit()">提交</button>
			</view>
		</div>

		<view class="uni-flex uni-row new_btn_bot" v-if="itemList.length>0">
			<button class="new_clear_btn btn_double" @click="cancel()">取消</button>
			<button class="new_save_btn btn_double" @click="submit()">确定</button>
		</view>

		<win-scan-button @goScan='openScanPopup' v-if="itemList.length>0"></win-scan-button>
		<win-scan-pack ref="scanPopup" @getScanResult='getScanResult'></win-scan-pack><comMessage ref="comMessage" @afterCloseScanMessage='closeScanMessage' @afterRescanMessage='afterRescan'
			@afterCloseCommitMessage='closeCommitMessage'></comMessage>
	</view>
</template>

<script>
	import comBaseItem from '@/mycomponents/comItem/comBaseItem.vue'
	import winScanButton from '@/mycomponents/wincom/scanCom/winScanButton.vue'
	import winScanPack from '@/mycomponents/wincom/scanCom/winScanPack.vue'
	import winCollapseLocation from '@/mycomponents/wincom/winCollapseLocation.vue'
	import winBlankView from '@/mycomponents/wincom/winBlankView.vue'
	import comMessage from '@/mycomponents/common/comMessage.vue'
	import {
		recycledMaterialReceipt,
		locations,
		getItemAsync,
		getBalancesByFilterAsync,
	} from '@/api/index.js';

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

	export default {
		name: 'directPutaway',
		components: {
			winBlankView,
			winScanPack,
			winScanButton,
			comBaseItem,
			winCollapseLocation,
			comMessage,
		},
		data() {
			return {
				options2: [],
				itemList: [],
				location: null,
				scrollTop: 0,
				old: {
					scrollTop: 0
				},
				currentLabel: null,
			};
		},
		props: {
			datacontent: {
				type: Object,
				value: null
			},
			type: {
				type: String,
				value: 'SEMI' //SEMI半成品 
			}
		},
		//返回首页
		onNavigationBarButtonTap(e) {
			if (e.index === 0) {
				goHome();
			}
		},
		mounted: function() {
			this.options = getRemoveOption();
		},
		methods: {
			openScanPopup() {
				this.$refs.scanPopup.openScanPopup(this.location, 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);
							}
						}
					});
				}
			},

			//扫描箱码
			async getScanResult(result) {
				let code = result.data.code;
				if (code == '') return;
				let that = this;
				that.currentLabel = result.data;
				uni.showLoading({
					title: '扫描中...',
					mask: true
				})
				let item = await getItemAsync(result.data.itemCode);
				if (!item.isRecycled) {
					this.showScanMessage('零件【' + result.data.itemCode + '】不是回收料,请扫描回收料');
				} else {
					let balanceParams = {
						pageSize: 100,
						pageIndex: 1,
						packingCode: code
					};
					let res = await getBalancesByFilterAsync(balanceParams);
					if (res.totalCount > 0) {
						let locationCode = res.items[0].locationCode;
						this.showScanMessage('箱码【' + code + '】在【' + locationCode +
							'】库位已经有库存信息,请重新扫描箱码');
					} else {
						let items = that.itemList.filter(r => {
							return r.packingCode === code
						});
						if (items.length > 0) {
							this.showRescanMessage('箱码【' + code + '】已经存在,是否要重新扫描?');
							// showConfirmMsg('箱码【' + code + '】已经存在,是否要请重新扫描?', confirm => {
							// 	if (confirm) {
							// 		that.deleteItemCode();
							// 		that.getItem(result);
							// 	}
							// });
						} else {
							that.getItem();
						}
					}
				}
				uni.hideLoading();
			},

			//重新扫描
			afterRescan(confirm) {
				let that = this;
				if (confirm) {
					that.deleteItemCode();
					that.getItem();
				}
			},

			//删除已经扫描的零件
			deleteItemCode() {
				let that = this;
				that.itemList.forEach((r, i) => {
					if (r.packingCode == that.currentLabel.packingCode) {
						that.itemList.splice(i, 1);
						return;
					}
				});
				this.$forceUpdate();
			},

			getItem() {
				let that = this;
				let data = that.createItem();
				this.itemList.unshift(data);
			},

			createItem() {
				let item = this.currentLabel;
				item.status = 2;
				item.reasonCode = '';
				item.containerCode = '';
				item.warehouseCode = localStorage.warehouseCode;
				return item;
				// let data = {
				// packingCode: item.data.code,
				// itemCode: item.data.itemCode,
				// itemName: item.data.itemName,
				// itemDesc1: item.data.itemDesc1,
				// itemDesc2: item.data.itemDesc2,
				// lot: item.data.lot,
				// expireDate: item.data.expireDate,
				// stdPackUom: item.data.stdPackUom,
				// qty: item.data.qty,
				// uom: item.data.uom,
				// remark: item.data.remark,
				// arriveDate: item.data.arriveDate,
				// stdPackQty: item.data.stdPackQty,
				// supplierBatch: item.data.supplierBatch,
				// status: 2, //合格		 
				// reasonCode: '',

				// //标签中没有的属性
				// locationArea: item.data.locationArea,
				// locationGroup: item.data.locationGroup,
				// produceDate: item.data.produceDate,
				// 	containerCode: '',
				// 	warehouseCode: localStorage.warehouseCode,
				// }

			},

			//扫描库位
			getToLocation(code) {
				if (code == '') return;
				let locationCode = code;
				if (locationCode != '') {
					uni.showLoading({
						title: '扫描中...',
						mask: true
					})
					locations(locationCode)
						.then(res => {
							if (res != null) {
								if (res.type != 2) {
									this.showMessage('目标库位必须是【原材料库】')
								} else {
									this.location = res;
								}
							} else {
								this.showMessage('未查询到库位【' + locationCode + '】');
							}
							uni.hideLoading();
						})
						.catch(err => {
							this.showMessage(err.message);
							uni.hideLoading();
						});
				}
			},

			clear() {
				this.location = null;
			},

			submit() {
				let that = this;
				if (that.itemList.length == 0) {
					return;
				}
				if (that.location == null) {
					this.showMessage('请扫描目标库位')
					return;
				}

				uni.showLoading({
					title: '提交中',
					mask: true
				})
				let item = {
					worker: localStorage.userName,
					company: localStorage.company,
					warehouseCode: localStorage.warehouseCode,
					// details: that.itemList
				};
				that.itemList.forEach(r => {
					r.locationErpCode = that.location.erpLocationCode;
					r.locationCode = that.location.code;
				})
				item.details = that.itemList;
				let params = JSON.stringify(item);
				console.log(params);
				recycledMaterialReceipt(params)
					.then(res => {
						that.showCommitSuccess();
						that.itemList = [];
						uni.hideLoading();
					})
					.catch(err => {
						this.showMessage(err.message);
						uni.hideLoading();
					});
			},

			cancel() {
				let that = this;
				showConfirmMsg('是否要清空已扫描的目标库位和零件信息?', confirm => {
					if (confirm) {
						that.location = null;
						that.itemList = [];
					}
				})
			},

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

			showCommitSuccess() {
				this.$refs.comMessage.showCommitSuccess();
			},

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

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

			closeCommitMessage() {
				this.openScanPopup();
			},

			closeScanMessage() {
				this.$refs.scanPopup.getfocus();
			},

			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>
</style>
