<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" style="padding-bottom: 150px" @scrolltoupper="upper"
			@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">{{toLocationCode}}</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="swipeClick($event,index)">
							<com-base-info :dataContent='item' :displayLocation="false" :displayStatus="false">
							</com-base-info>
						</uni-swipe-action-item>
					</uni-swipe-action>
				</view>
			</view>
		</scroll-view>

		<view class="new_bot_box" v-if="itemList.length>0">
			<uni-collapse>
				<uni-collapse-item :show-animation="true" title="点击添加其他原因">
					<view class="content">
						<view class="tit_bot_box">原因</view>
						<uni-data-picker class='uni-data-picker' placeholder="请选择原因" popup-title="出库原因"
							:localdata="receiptReasonArray" v-model="receiptReason" style="padding: 10rpx 20rpx;">
						</uni-data-picker>
						<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">
				<button :disabled="submitting" class="new_save_btn" @click="beforSubmit()">提交</button>
			</view>
		</view>
		<comMessage ref="comMessage"></comMessage>

		<win-scan-button @goScan='openScanPopup' v-if="itemList.length>0"></win-scan-button>
		<com-scan-unreceipt ref="scanPopup" @confirm='scanConfirm'></com-scan-unreceipt>
	</view>
</template>

<script>
	import {
		unplannedReceipt,
		getitems,
		balances
	} from '@/api/index.js';
	import {
		getReceiptReasonArray
	} from '@/common/array.js';

	import winBlankView from '@/mycomponents/wincom/winBlankView.vue'
	import winScanButton from '@/mycomponents/wincom/winScanButton.vue'
	import comBaseItem from '@/mycomponents/comItem/comBaseItem.vue'
	import comScanUnreceipt from '@/mycomponents/scan/comScanUnReceipt.vue'
	import comMessage from '@/mycomponents/common/comMessage.vue'
	import {
		getInventoryTypeStyle,
		getInventoryStatusDesc,
		showConfirmMsg,
		goHome,
		getRemoveOption
	} from '@/common/basic.js';

	export default {
		name: 'unreceipt',
		components: {
			winBlankView,
			winScanButton,
			comBaseInfo,
			comScanUnreceipt,
			comMessage
		},
		data() {
			return {
				options: [],
				itemList: [],
				fromLocationCode: '',
				toLocationCode: '',
				scrollTop: 0,
				old: {
					scrollTop: 0
				},
				receiptReason: '',
				receiptReasonArray: [],
				remark: '',
				submitting: false,
				scanResult: {}
			};
		},
		created() {
			this.options = getRemoveOption();
			this.receiptReasonArray = getReceiptReasonArray();
			this.receiptReason = this.receiptReasonArray[0].value;
		},
		onLoad() {},
		onNavigationBarButtonTap(e) {
			if (e.index === 0) {
				goHome();
			}
		},
		filters: {
			statusStyle: function(val) {
				return getInventoryTypeStyle(val);
			},
			statusColor: function(val) {
				return getInventoryStatusDesc(val);
			},
		},
		methods: {
			openScanPopup() {
				this.$refs.scanPopup.openScanPopup(this.toLocationCode, this.itemList)
			},

			closeScanPopup() {
				this.$refs.popup.close()
			},
			//提示是否移除选择的行?
			swipeClick(e, index) {
				let {
					content
				} = e;
				if (content.text === '移除') {
					uni.showModal({
						title: '提示',
						content: '是否移除选择的行?',
						success: res => {
							if (res.confirm) {
								this.itemList.splice(index, 1);
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				}
			},

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

			//检查库存余额
			beforSubmit() {
				let that = this;
				if (that.itemList.length === 0) {
					this.showMessage('请先扫描箱标签');
					return;
				}
				if (that.toLocationCode === '') {
					this.showMessage('请扫描目标库位');
					return;
				}

				if (that.receiptReason === '') {
					this.showMessage('请先选择库存原因');
					return;
				}

				let zeroCount = that.itemList.filter(r => r.qty == 0).length;
				if (zeroCount > 0) {
					this.showMessage('入库数量不能为0');
					return;
				}

				let params = {
					pageSize: 100,
					pageIndex: 1,
					itemCode: that.scanResult.itemCode,
					packingCode: that.scanResult.packingCode,
					locationCode: that.toLocationCode
				};

				balances(params).then(res => {
					if (res.totalCount > 0) {
						that.showMessage('到目标库位已经有库存记录,不可以再入库');
					} else {
						that.submit();
					}
				}).catch(ex => {
					that.showMessage(ex.message);
				});
			},

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

				let item = {
					worker: localStorage.userName,
					warehouseCode: localStorage.warehouseCode,
					jobNumber: "",
					supplierCode: "",
					company: localStorage.company,
					number: "",
					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: Number(r.qty),
							uom: r.uom,
						},
						status: 2,
						locationCode: that.toLocationCode,
						reasonCode: that.receiptReason,
						inventoryStatus: 2,
						worker: localStorage.userName
					}
					item.details.push(detail);
				})
				let params = JSON.stringify(item);
				console.log('params', params);
				unplannedReceipt(params)
					.then(res => {
						this.showMessage('提交成功');
						that.clearInfo();
						that.hideLoading();
					})
					.catch(err => {
						this.showMessage(err.message);
						that.hideLoading();
					});
			},

			hideLoading() {
				this.submitting = false;
				uni.hideLoading();
			},
			clearInfo() {
				let that = this;
				that.itemList = [];
				that.toLocationCode = '';
				that.receiptReason = "";
				that.$refs.comscan.getfocus();
				that.$refs.toLocation.clear();
			},
			showMessage(message) {
				this.$refs.comMessage.showMessage(message);
			}
		}
	};
</script>

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