<template>
	<page-meta root-font-size="18px"></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="scrollView" @scrolltoupper="upper"
			style="padding-bottom: 150px;" @scrolltolower="lower" @scroll="scroll">
			<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">{{holdItem.holdType}}</text>
						</view>
					</view>
				</view>
			</view>
			<view class="detail-list" v-for="(item,index) in itemList">
				<view class="detail-content">
					<uni-swipe-action>
						<uni-swipe-action-item :right-options="options" :auto-close="false"
							@click="removeClick($event,index)">
							<com-hold :dataContent="item" :editQty="false" :holdItem="holdItem"></com-hold>
						</uni-swipe-action-item>
					</uni-swipe-action>
				</view>
			</view>
		</scroll-view>
		<view class="new_bot_box" v-if="itemList.length>0">
			<uni-collapse @activeItem="openCollapse">
				<uni-collapse-item :show-animation="true" title="点击添加目标库位">
					<view class="content">
						<view class="tit_bot_box">目标库位</view>
						<uni-easyinput v-model="toLocationCode" placeholder="请扫描目标库位"
							style="padding: 10rpx 20rpx; width: 95%;" @confirm="getToLocation"> </uni-easyinput>

						<view class="tit_bot_box">备注</view>
						<uni-easyinput type="textarea" v-model="remark" placeholder="请输入备注"
							style="padding: 10rpx 20rpx; width: 95%;">
						</uni-easyinput>
					</view>
				</uni-collapse-item>
			</uni-collapse>
			<view class="new_btn_bot bot_pos uni-flex">
				<button class="new_clear_btn btn_double" @click="clear()"> 清空 </button>
				<button class="new_save_btn btn_double" @click="submit()">提交</button>
			</view>
		</view>
		<comMessage ref="comMessage"></comMessage>
		<win-scan-button @goScan='openScanPopup' v-if="itemList.length>0"></win-scan-button>
		<com-scan-hold ref="scanPopup" @holdTypeChange='holdTypeChange' @confirm='scanconfirm'></com-scan-hold>
	</view>
</template>

<script>
	import {
		locations,
		getBalancesByFilter,
		unplannedIssue,
		inventoryTransfer
	} from '@/api/index.js';

	import winBlankView from '@/mycomponents/wincom/winBlankView.vue'
	import winComScan from '@/mycomponents/wincom/winComScan.vue'
	import winScanButton from '@/mycomponents/wincom/winScanButton.vue'
	import comScanHold from '@/mycomponents/scan/comScanHold.vue'
	import comHold from '@/mycomponents/coms/inventory/comHold.vue'
	import comBalance from '@/mycomponents/common/comBalance.vue'
	import comMessage from '@/mycomponents/common/comMessage.vue'

	import {
		showConfirmMsg,
		goHome,
		getRemoveOption
	} from '@/common/basic.js';
	export default {
		name: 'unreceipt',
		components: {
			winBlankView,
			winComScan,
			winScanButton,
			comScanHold,
			comHold,
			comBalance,
			comMessage,
		},
		data() {
			return {
				//滑动移除
				options: [],
				holdItem: {
					holdStatus: 0,
					holdType: ''
				},
				itemList: [],
				toLocationCode: '',
				scrollTop: 0,
				old: {
					scrollTop: 0
				},
				remark: '',
				currentItem: {},
				scanResult: {},
			};
		},
		onLoad() {

		},
		mounted() {
			this.options = getRemoveOption();
		},
		onNavigationBarButtonTap(e) {
			if (e.index === 0) {
				goHome();
			}else if(e.index === 1){
				window.location.reload();
			}
		},
		filters: {},
		methods: {
			openScanPopup() {
				this.$refs.scanPopup.openScanPopup(this.holdItem)
			},

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

			scanconfirm(holdItem, itemList) {
				this.holdItem = holdItem;
				this.itemList = itemList;
				this.initLocation();
			},

			holdTypeChange(holdItem) {
				this.holdItem = holdItem;
			},

			initLocation() {
				if (this.toLocationCode == '') {
					if (this.holdItem.holdStatus === 1) //隔离转合格
					{
						this.toLocationCode = 'INSPECT';
					} else if (this.holdItem.holdStatus === 2) //隔离出库
					{
						this.toLocationCode = '';
					} else if (this.holdItem.holdStatus === 3) //合格隔离
					{
						this.toLocationCode = 'HOLD';
					}
				}
			},

			getToLocation() {
				uni.showLoading({
					title: '正在扫描',
					mask: true
				})
				let that = this;
				locations(that.toLocationCode).then(res => {
					if (res == null) {
						this.showMessage('目标库位不存在')
					} else {
						that.location = res;
						that.toLocationCode = res.code;
					}
					uni.hideLoading();
				}).catch(err => {
					this.showMessage(err.message)
					uni.hideLoading();
				})
			},

			async submit() {
				let that = this;
				if (that.itemList.length === 0) {
					this.showMessage('请先扫描标签');
					return;
				}
				if (that.holdItem.holdStatus === 1) { //隔离转合格
					that.transferHoldToGood();
				} else if (that.holdItem.holdStatus === 2) { //隔离出库
					that.unIssue(); //计划外出库
				} else if (that.holdItem.holdStatus === 3) //合格转隔离
				{
					that.transferGoodToHold();
				}
			},

			//隔离转合格
			transferHoldToGood() {
				uni.showLoading({
					title: '验证中...',
					mask: true
				})
				let params = {
					pageSize: 100,
					pageIndex: 1,
					itemCode: this.scanResult.data.itemCode,
					packingCode: this.scanResult.data.packingCode,
					lot: this.scanResult.data.lot,
					locationCode: that.toLocationCode,
					inventoryStatuses: [2],
					locationTypes: [1, 2, 3, 4],
					sortBy: "PackingCode",
				};
				getBalancesByFilter(params).then((res) => {
					try {
						uni.hideLoading();
						if (res.totalCount == 0) {
							this.transfer();
						} else if (res.totalCount >= 1) {
							this.showMessage('已经有库存余额,不能将隔离转合格');
						}
					} catch (e) {
						this.showMessage(e.message);
						uni.hideLoading();
					}
				}).catch((err) => {
					this.showMessage(err.message);
					uni.hideLoading();
				})
			},

			//合格转隔离
			transferGoodToHold() {
				this.transfer();
			},

			//计划外出库
			unIssue() {
				let that = this;
				uni.showLoading({
					title: "提交中...",
					mask: true
				});
				let item = {
					worker: localStorage.userName_CN ==""?localStorage.userName:localStorage.userName_CN,
					warehouseCode: localStorage.warehouseCode,
					jobNumber: "",
					supplierCode: "",
					company: localStorage.company,
					number: "",
					remark: that.remark,
					details: []
				}

				that.itemList.forEach(r => {
					let detail = {
						itemCode: r.itemCode,
						item: r.item,
						lot: r.lot,
						batch: r.batch,
						packingCode: r.packingCode,
						containerCode: r.containerCode,
						warehouseCode: localStorage.warehouseCode,
						qty: {
							qty: r.qty.qty,
							uom: r.qty.uom
						},
						status: 2,
						locationCode: that.toLocationCode,
						reasonCode: '0',
						worker: localStorage.userName_CN ==""?localStorage.userName:localStorage.userName_CN,
					}
					item.details.push(detail);
				})

				let params = JSON.stringify(item);
				console.log('params', params);
				unplannedIssue(params)
					.then(res => {
						that.showMessage('提交成功');
						that.clearInfo();
						that.hideLoading();
					})
					.catch(err => {
						this.showMessage(err.message);
						that.hideLoading();
					});
			},

			transfer() {
				let that = this;
				uni.showLoading({
					title: "提交中....",
					mask: true
				});

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

				that.itemList.forEach(r => {
					if (that.holdItem.holdStatus === 1) { //隔离转合格
						r.toStatus = 2;
					} else if (that.holdItem.holdStatus === 3) //合格转隔离
					{
						r.toStatus = 4;
					}

					r.toLocationCode = that.toLocationCode;
					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();
			},

			clear() {
				showConfirmMsg('是否要清空扫描的内容?', confirm => {
					if (confirm) {
						let that = this;
						that.itemList = [];
						that.toLocationCode = ''
						that.holdItem = {
							holdStatus: 0,
							holdType: ''
						};
					}
				})
			},

			showMessage(message) {
				this.$refs.comMessage.showMessage(message);
			}
		}
	};
</script>

<style scoped lang="scss">
	/deep/ .input-value {
		font-size: 16px;
	}
</style>
