<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="scroll-Y" @scrolltoupper="upper"
			@scrolltolower="lower" @scroll="scroll" style="padding-bottom:150px">
			<view class="detail-list " v-for="(item, index) in itemList" :key="index">
				<view class="detail-content">
					<view class="" style="">
						<view class="uni-flex uni-row u-col-center">
							<view class="" style="margin-top: 20rpx; margin-left: 10rpx; font-weight: bold;">
								({{index+1}}).
							</view>
							<comItemCode :itemData="item"></comItemCode>
						</view>

						<uni-table border stripe style="">
							<!-- <uni-tr>
								<uni-td align="center">ERP料号</uni-td>
								<uni-td>{{item.itemCode}}</uni-td>
							</uni-tr> -->
							<uni-tr>
								<uni-td align="center">单位</uni-td>
								<uni-td>{{item.uom}}</uni-td>
							</uni-tr>
							<uni-tr>
								<uni-td align="center">标包数</uni-td>
								<uni-td>{{item.stdPackQty}}</uni-td>
							</uni-tr>
							<uni-tr>
								<uni-td align="center">来源库位</uni-td>
								<uni-td>{{item.fromLocationCode}}</uni-td>
							</uni-tr>

							<uni-tr>
								<uni-td align="center">数量</uni-td>
								<uni-td>
									<view class="uni-flex uni-row">
										<view class="uni-flex uni-row space-between" style="width: 100%;">
											<view class="">
												<com-number-box :ref="'comNumberBox_'+index" v-model="item.qty"
													:max="99999" :min="0" @change="qtyChanged($event,item,index)">
												</com-number-box>
											</view>

											<view class="">
												<button type="primary" size="mini" style="margin-left: 30rpx;"
													@click="remove(item,index)">移除</button>
											</view>

										</view>
									</view>
								</uni-td>
							</uni-tr>
						</uni-table>
					</view>
				</view>
			</view>
		</scroll-view>

		<div class="new_bot_box" v-show="itemList.length>0">
			<win-collapse-location ref="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>
		<win-scan-button @goScan='openScanPopup' v-if="itemList.length>0"></win-scan-button>
		<winScanByProductCode ref="scanPackPopup" title="单件码" @getScanResult='getScanResult'></winScanByProductCode>
		<com-balance ref="balanceItems" @selectedItem='selectedBalanceItem'></com-balance>
		<com-message ref="comMessage" @afterClose='afterCloseMessagg'></com-message>
	</view>
	</view>
</template>

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

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

	import winBlankView from '@/mycomponents/wincom/winBlankView.vue'
	import comBalanceItem from '@/mycomponents/comItem/comBalanceItem.vue'
	import winScanButton from '@/mycomponents/wincom/winScanButton.vue'
	import winScanByPack from '@/mycomponents/wincom/winScanByPack.vue'
	import comMessage from '@/mycomponents/common/comMessage.vue'
	import comBalance from '@/mycomponents/common/comBalance.vue'
	import winCollapseLocation from '@/mycomponents/wincom/winCollapseLocation.vue'
	import winScanByProductCode from '@/mycomponents/wincom/winScanByProductCode.vue'
	import comItemCode from '@/mycomponents/comItem/comItemCode.vue'
	import comNumberBox from '@/mycomponents/common/comNumberBox.vue';

	export default {
		name: 'comtransfer',
		components: {
			winBlankView,
			comBalanceItem,
			comMessage,
			winScanButton,
			comBalance,
			winCollapseLocation,
			winScanByProductCode,
			comItemCode,
			comNumberBox
		},
		data() {
			return {
				options: [],
				itemList: [],
				fromLocationCode: '',
				fromLocationInfo: {},
				toLocationCode: '',
				scrollTop: 0,
				old: {
					scrollTop: 0
				},
				inventoryStatus: -1,
				inventoryStatusArray: [],
				isClearPackCode: false, //清除箱码
				isClearContainerCode: false, //清除托码
				isClearLot: false, //清除批次,
				locationErpCode: '',
				toLocationErpCode: '',
				locationGotFocus: false,
				transferType:"Transfer_Injection"
			};
		},
		props: {
			// locationTypes: {
			// 	type: [Array, String, Number],
			// 	value: ''
			// },
			byLocation: {
				type: Boolean,
				value: false
			},
		},

		watch: {},
		mounted: function() {
			this.openScanPopup();
			this.options = getRemoveOption();

		},


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

			getScanResult(result) {
				let that = this;
				let code = result.itemCode;
				let datas = that.itemList.filter(r => {
					return r.itemCode == code
				})

				if (datas.length > 0) {
					showConfirmMsg('物料【' + code + '】已经存在,是否要重新扫描?', confirm => {
						if (confirm) {
							that.itemList.forEach((r, i) => {
								if (r.packingCode == code) {
									that.itemList.splice(i, 1);
									return;
								}
							});
							// that.getBalance(code);
						}
					});
				} else {
					var item = {
						itemCode: result.itemCode,
						itemName: result.itemName,
						itemDesc1: result.itemDesc1,
						stdPackQty: result.stdPackQty,
						uom: result.uom,
						qty: result.stdPackQty,
						fromLocationCode: "ZPCP1",
						fromStatus: 2,
						toStatus:2
					}

					this.itemList.unshift(item);
					this.$refs.scanPackPopup.closeScanPopup()
				}
			},

			qtyChanged(value, item, index) {
				if (value <= 0) {
					this.showMessage('退货数量必须大于0')
					item.handledQty = item.qty
					this.$refs['comNumberBox_' + index][0].setValue(item.handledQty);
				}
			},
			getFromLocation(code, callBack) {
				locations(code).then(res => {
					if (res) {
						this.fromLocationInfo = res;
						callBack(true)
					}
				}).catch(err => {
					callBack(false)
				})
			},

			getToLocation(code) {
				if (code == '') {
					this.showMessage('目标库位不能为空');
					return;
				}
				uni.showLoading({
					title: "扫描中",
					mask: true
				});
				let that = this;
				locations(code).then(res => {
					if (res == null) {
						that.toLocationCode = ''
						that.showMessage('目标库位【' + code + '】不存在');
						this.$refs.location.clearLocation()
						this.locationGotFocus = true;
					} else {
						that.toLocationCode = code;
						that.toLocationErpCode = res.erpLocationCode;
					}
					uni.hideLoading();
				}).catch(err => {
					that.toLocationCode = ''
					this.locationGotFocus = true;
					that.showMessage(err.message);
					uni.hideLoading();
				})
			},

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

			submit() {
				let that = this;
				if (that.itemList.length === 0) {
					this.showMessage('请选择要提交的零件');
					return;
				}
				if (that.toLocationCode === '') {
					this.showMessage('请扫描目标库位');
					return;
				}
				this.getFromLocation(this.itemList[0].fromLocationCode, res => {
					if (res) {
						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: "",
							type: this.transferType,
							details: []
						}

						this.itemList.forEach(r => {
							r.toLocationCode = that.toLocationCode;
							r.toLocationErpCode = this.toLocationErpCode
							r.fromLot ="";
							r.fromLocationArea = this.fromLocationInfo.areaCode;
							r.fromLocationGroup = this.fromLocationInfo.locationGroupCode;
							r.fromLocationErpCode = this.fromLocationInfo.erpLocationCode;
							r.fromWarehouseCode = localStorage.warehouseCode;
							
							r.toLot ="";
							r.fromPackingCode="";
							r.toPackingCode = "";
							
							r.toWarehouseCode =localStorage.warehouseCode;
							r.worker = localStorage.userName_CN == "" ? localStorage.userName :
								localStorage.userName_CN
							item.details.push(r);
						})

						let params = JSON.stringify(item);
						console.log('params', params);
						completeTransfer(params)
							.then(res => {
								that.showCommitSuccess();
								that.clearInfo();
								uni.hideLoading();
							})
							.catch(err => {
								that.showMessage(err.message);
								uni.hideLoading();
							});
					}
				})

			},

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

			clearInfo() {
				let that = this;
				that.itemList = [];
				that.toLocationCode = '';
				that.inventoryStatus = -1;
				that.$refs.location.clearLocation()
			},

			afterCloseMessagg() {
				if (this.locationGotFocus) {
					this.$refs.location.gotFocus();
				}
			},

			clearPackCode() {
				this.isClearPackCode = !this.isClearPackCode;
			},

			clearContainerCode() {
				this.isClearContainerCode = !this.isClearContainerCode;
			},

			clearLot() {
				this.isClearLot = !this.isClearLot;
			},

			upper: function(e) {
				// console.log(e)
			},
			lower: function(e) {
				// console.log(e)
			},
			scroll: function(e) {
				// console.log(e)
				this.old.scrollTop = e.detail.scrollTop;
			},
			showMessage(message) {
				this.$refs.comMessage.showMessage(message);
			},
			showCommitSuccess() {
				this.$refs.comMessage.showCommitSuccess();
			},
		}
	};
</script>

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

	/deep/ .uni-collapse-item__title-text {
		font-size: 16px;
	}

	/deep/ .uni-collapse-item--border {
		border-bottom-width: 0px;
		border-bottom-color: #ebeef5;
	}

	/deep/ .uni-collapse-item--border {
		border-bottom-width: 1px;
		border-bottom-color: #ebeef5;
	}
</style>