<template>
	<page-meta root-font-size="18px"></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="device-detail">
					<view class="list-style nopad">
						<view class="ljh_box nopad">
							<view class="tit_ljh uni-flex">
								<text class="font_xl text_black text_bold">位置码 : {{dataContent.code}}</text>
							</view>

						</view>
					</view>
				</view>

			</view>
		</view>

		<scroll-view scroll-y="true" @scroll="scroll" class="scroll-detail">

			<view class="detail-list margin_top" v-for="(item, index) in itemList" :key="item.id">


				<view class="detail-content">
					<view class="choose_main">
						<view class="ljh_box">
							<view class="tit_ljh">{{ item.partCode }}</view>
							<view class="ljh_left desc_ljh">
								<view class="font_xs text_lightblue">{{ item.partName }}</view>
								<view class="font_xs text_lightblue">{{ item.partDesc }}</view>
							</view>
						</view>

						<view class="list_form">
							<view class="uni-container">
								<uni-table style="overflow-x: hidden;">
									<uni-tr>
										<uni-th width="50">库位</uni-th>
										<uni-th width="240" align="center">
											<view class="text_packingCode">
												{{ item.locationCode }}({{item.locationName}})
											</view>
										</uni-th>
									</uni-tr>
									<uni-tr>
										<uni-th width="50">数量</text></uni-th>
										<uni-th width="240" align="center">
											<view
												style="display: flex;flex-direction: row;justify-content:center;align-items: center;">
												<com-number-box :ref="'comNumberBox_'+index" v-model="item.qty"
													:max="999999" :min="0" style='width: 100px;'
													@change="qtyChanged($event,item,index)">
												</com-number-box>
											</view>
										</uni-th>
									</uni-tr>
									<uni-tr>
										<uni-th width="50">单位</uni-th>
										<uni-th width="240" align="center">
											<view class="text_packingCode">
												{{ item.basicUom }}
											</view>
										</uni-th>
									</uni-tr>
									<uni-tr>
										<uni-th width="50">标包数</uni-th>
										<uni-th width="240" align="center">
											<view class="text_black">{{item.stdPackQty }}</view>
										</uni-th>
									</uni-tr>
								</uni-table>

							</view>
						</view>
					</view>
					<view class="choose_marked" v-if="item.scaned">
						<image src="@/static/image_marked.svg"></image>
					</view>
				</view>
			</view>
		</scroll-view>

		<div class="new_bot_box" v-show="itemList.length>0">
			<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>
		</div>

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

<script>
	import {
		kittingIssueRequest
	} from '@/api/index.js';

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

	import winBlankView from '@/mycomponents/wincom/winBlankView.vue'
	import winScanButton from '@/mycomponents/wincom/winScanButton.vue'
	import winScanByPosition from '@/mycomponents/wincom/winScanByPosition.vue'
	import comMessage from '@/mycomponents/common/comMessage.vue'
	import comNumberBox from '@/mycomponents/common/comNumberBox.vue';

	export default {
		name: 'purchasePutaway',
		components: {
			winBlankView,
			comMessage,
			winScanButton,
			winScanByPosition,
			comNumberBox
		},
		data() {
			return {
				itemList: [],
				dataContent: {}
			};
		},
		props: {
			// locationTypes: {
			// 	type: [Array, String, Number],
			// 	value: ''
			// },
		},

		onNavigationBarButtonTap(e) {
			if (e.index === 0) {
				goHome();
			} else if (e.index === 1) {
				window.location.reload();
			}
		},
		mounted: function() {
			this.options = getRemoveOption();
			this.openScanPopup();
		},

		methods: {
			openScanPopup() {
				this.$refs.scanPopup.openScanPopup()
			},

			getScanResult(result) {
				this.dataContent = result;
				result.qty =result.stdPackQty;
				this.itemList.push(result)
				this.$forceUpdate()

			},

			clear() {
				this.itemList = [];
				this.dataContent = {}
			},


			submit() {
				let that = this;
				if (that.itemList.length === 0) {
					that.showMessage('请扫描位置码');
					return;
				}

				uni.showLoading({
					title: "提交中....",
					mask: true
				});
				var params = this.setParams();
				console.log(JSON.stringify(params))
				kittingIssueRequest(params).then(res => {
					uni.hideLoading();
					this.showMessage("提交成功")
					this.clear();
				}).catch(error => {
					uni.hideLoading();
					this.showMessage(error.message);
				})

			},
			setParams() {
				var param = {
					worker: localStorage.userName_CN == "" ? localStorage.userName : localStorage.userName_CN,
					remark: "",
					extraProperties: {},
					activeDate: getISODateTime(),
					autoSubmit: true,
					autoAgree: false,
					autoHandle: false,
					autoCompleteJob: false,
					directCreateNote: true,
					issueRequestType: 4,
					useOnTheWayLocation: false,
					details: []
				}

				this.itemList.forEach(res => {
					var data = {
						remark: "",
						qty:res.qty,
						itemCode: res.partCode,
						itemName: res.partName,
						itemDesc1: res.itemDesc2,
						uom:  res.basicUom,
						stdPackQty: res.stdPackQty,
						toLocationCode:  res.locationCode,
						prodLine: null,
						requestStatus: 1,
						issuedQty: res.qty,
						receivedQty: 0,
						status: 1,
						positionCode: this.dataContent.code,
						recommendType: 0,
						boxQty:  res.qty

					}
					param.details.push(data)
				})
				return param;

			},

			qtyChanged(value, item, index) {
				if (value <= 0) {
					this.showMessage('发料数量不能小于或等于0')
					item.handledQty = item.defaultHandleQty
					this.$refs['comNumberBox_' + index][0].setValue(item.handledQty);
					return;
				}
				// else if (value > item.defaultHandleQty) {
				// 	item.handledQty = item.defaultHandleQty
				// 	this.showMessage('发料数量不能大于库存数量:' + item.handledQty)
				// 	this.$refs['comNumberBox_' + index][0].setValue(item.handledQty);
				// }
			},
			showMessage(message) {
				this.$refs.comMessage.showMessage(message);
			},
			showConfirmMessage(message) {
				this.$refs.comMessage.showConfirmMessage(message);
			},
			showCommitSuccess() {
				this.$refs.comMessage.showCommitSuccess();
			},
			showScanMessage(message) {
				this.$refs.comMessage.showScanMessage(message);
			},
			showRescanMessage(message) {
				this.$refs.comMessage.showRescanMessage(message);
			},

			closeScanMessage() {
				this.scanPopupGetFocus();
			},

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

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

			scanPopupLoseFocus(message) {
				this.$refs.scanPopup.losefocus();
			},
		}
	};
</script>

<style scoped lang="scss">
</style>