<template>
	<view>
		<uni-collapse>
			<uni-forms ref="form" :modelValue="formData" :rules="rules">
				<uni-collapse-item title="物料移库单" :open="true">
					<uni-forms-item label="发起人" :labelWidth='90' name="transferBy">
						<uni-easyinput type="text" suffixIcon="scan" @iconClick="scanBarTransferBy"
							v-model="formData.transferBy" />
					</uni-forms-item>
					<uni-forms-item label="运输员" :labelWidth='90' name="transportBy">
						<uni-easyinput type="text" suffixIcon="scan" @iconClick="scanBarTransportBy"
							v-model="formData.transportBy" />
					</uni-forms-item>
					<uni-forms-item label="原库位条码" :labelWidth='90' name="storageLocationBarcode">
						<uni-easyinput suffixIcon="scan" @iconClick="scanBarstorageLocationBarcode"
							@confirm="splitStlBarcode" type="text" v-model="formData.storageLocationBarcode" />
					</uni-forms-item>
				</uni-collapse-item>
				<uni-collapse-item title="库位库存明细" :open="true">
					<u-checkbox-group v-model="checked" @change="getCheckedList" :borderBottom="true" placement="column"
						iconPlacement="right">
						<u-checkbox size="25" iconSize="25" activeColor="green"
							v-for="(item, index) in wmsMaterialDetailList" :key="index" :disabled="item.disabled"
							:name="item.id" :label="'\n'+' 序号 : '+(index+1)+'\n\n'+
							'物料编码 : '+(item.materialCode?item.materialCode:'')+'\n\n'+
							'批号 : '+(item.batchNo?item.batchNo:'')+'\n\n'+
							'箱号 : '+(item.lotNo?item.lotNo:'')+'\n\n'+
							'物料名称 : '+(item.materialName?item.materialName:'')+'\n\n'+
							'库位编码 : '+(item.storageLocationCode?item.storageLocationCode:'')+'\n\n'+
							'入库时间 : '+(item.storageInTime?item.storageInTime:'')+'\n\n'+
							'库存总数 : '+(item.number?item.number:'')+'\n\n'+
							'锁定数量 : '+(item.lockNumber?item.lockNumber:'')+'\n\n'+
							'可用数量 : '+(item.MaterialAvailableNumber?item.MaterialAvailableNumber:'')+'\n'
							">
						</u-checkbox>
					</u-checkbox-group>
				</uni-collapse-item>
				<uni-collapse-item title="已选物料" :open="true">
					<uni-card :is-shadow="false" is-full v-for="item,index in wmsStockTransferDetailList"
						:key="item.id">
						<div><strong>物料编码</strong>:{{item.materialCode}}</div>
						<div><strong>物料名称</strong>:{{item.materialName}}</div>
						<div><strong>批号</strong>:{{item.batchNo}}</div>
						<div><strong>可用数量</strong>:{{item.MaterialAvailableNumber}}</div>
						<uni-forms-item label="目标库位" :labelWidth='80' name="newStorageLocationBarcode"
							style="margin-top: 10px;">
							<uni-easyinput suffixIcon="scan" @iconClick="scanBarNewStorageLocationBarcode(index)"
								@confirm="splitNewStlBarcode(index)" type="text"
								v-model="item.newStorageLocationBarcode" />
						</uni-forms-item>
						<uni-forms-item label="转移数量" :labelWidth='80' name="actualNumber" style="margin-top: 10px;">
							<u-number-box button-size="36" inputWidth="120" v-model="item.actualNumber"
								min="0"></u-number-box>
						</uni-forms-item>
					</uni-card>
				</uni-collapse-item>
			</uni-forms>
		</uni-collapse>
		<u-button type="primary" @click="submit">提交</u-button>
	</view>
</template>

<script>
	import {
		listStock,
		addTransfer
	} from "@/api/wms/stock.js";
	export default {
		mounted() {},
		data() {
			return {
				checked: null,
				formData: {
					whCode: null,
					areaCode: null,
					shelvesCode: null,
					storageLocationCode: null,
					storageLocationBarcode: null,
					newStorageLocationBarcode: null,
					actualNumber: null
				},
				wmsMaterialDetailList: [],
				wmsStockTransferDetailList: [],
				rules: {

				}
			}
		},

		methods: {
			test() {},
			getCheckedList(e) {
				this.wmsStockTransferDetailList = [];
				var array = JSON.parse(JSON.stringify(e));
				for (var i = 0; i < e.length; i++) {
					let obj = {};
					obj = this.wmsMaterialDetailList.filter(item => {
						return item.id == e[i]
					})[0];
					obj.wmsMaterialStockId = array[i];
					obj.materialBatchNo = obj.batchNo;
					obj.materialLotNo = obj.lotNo;
					obj.originAreaCode = this.formData.areaCode;
					obj.originShelvesCode = this.formData.shelvesCode;
					obj.originLocationCode = this.formData.storageLocationCode;
					obj.targetAreaCode = null;
					obj.targetShelves = null;
					obj.targetLocation = null;
					obj.availableNumber = obj.MaterialAvailableNumber;
					obj.schemeNumber = null;
					obj.actualNumber = null;
					obj.numUnitId = null;
					obj.delStatus = null;
					obj.remark = null;
					obj.id = null;
					this.wmsStockTransferDetailList.push(obj)
				}
			},
			splitStlBarcode(e) {
				console.log(e)
				this.formData.whCode = null;
				this.formData.areaCode = null;
				this.formData.shelvesCode = null;
				this.formData.storageLocationCode = null;
				let array = e.split('-');
				switch (array.length) {
					case 1:
						this.formData.whCode = array[0];
						this.getMatList();
						break;
					case 2:
						this.formData.whCode = array[0];
						this.formData.areaCode = array[1];
						this.getMatList();
						break;
					case 3:
						this.formData.whCode = array[0];
						this.formData.areaCode = array[1];
						this.formData.shelvesCode = array[2];
						this.getMatList();
						break;
					case 4:
						this.formData.whCode = array[0];
						this.formData.areaCode = array[1];
						this.formData.shelvesCode = array[2];
						this.formData.storageLocationCode = array[3];
						this.getMatList();
						break;
					case 5:
						this.formData.whCode = array[0];
						this.formData.areaCode = array[1];
						this.formData.shelvesCode = array[2];
						this.formData.storageLocationCode = array[3] + '-' + array[4];
						this.getMatList();
						break;
				}
			},
			scanBarstorageLocationBarcode() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.$set(_this.formData, "storageLocationBarcode", res
							.result);
						_this.splitStlBarcode(res.result);
					}
				});
			},
			splitNewStlBarcode(i) {
				this.wmsStockTransferDetailList[i].whCode = null;
				this.wmsStockTransferDetailList[i].targetAreaCode = null;
				this.wmsStockTransferDetailList[i].targetShelves = null;
				this.wmsStockTransferDetailList[i].targetLocation = null;
				let array = this.wmsStockTransferDetailList[i].newStorageLocationBarcode.split('-');
				switch (array.length) {
					case 1:
						if (this.judgeSameWhCode(array[0])) {
							this.wmsStockTransferDetailList[i].whCode = array[0];
						} else {
							this.wmsStockTransferDetailList[i].newStorageLocationBarcode = null;
							this.$modal.msg("目标仓库与原仓库不一致!");
						}
						break;
					case 2:
						if (this.judgeSameWhCode(array[0])) {
							this.wmsStockTransferDetailList[i].whCode = array[0];
							this.wmsStockTransferDetailList[i].targetAreaCode = array[1];
						} else {
							this.wmsStockTransferDetailList[i].newStorageLocationBarcode = null;
							this.$modal.msg("目标仓库与原仓库不一致!");
						}
						break;
					case 3:
						if (this.judgeSameWhCode(array[0])) {
							this.wmsStockTransferDetailList[i].whCode = array[0];
							this.wmsStockTransferDetailList[i].targetAreaCode = array[1];
							this.wmsStockTransferDetailList[i].targetShelves = array[2];
						} else {
							this.wmsStockTransferDetailList[i].newStorageLocationBarcode = null;
							this.$modal.msg("目标仓库与原仓库不一致!");
						}
						break;
					case 4:
						if (this.judgeSameWhCode(array[0])) {
							this.wmsStockTransferDetailList[i].whCode = array[0];
							this.wmsStockTransferDetailList[i].targetAreaCode = array[1];
							this.wmsStockTransferDetailList[i].targetShelves = array[2];
							this.wmsStockTransferDetailList[i].targetLocation = array[3];
						} else {
							this.wmsStockTransferDetailList[i].newStorageLocationBarcode = null;
							this.$modal.msg("目标仓库与原仓库不一致!");
						}
						break;
					case 5:
						if (this.judgeSameWhCode(array[0])) {
							this.wmsStockTransferDetailList[i].whCode = array[0];
							this.wmsStockTransferDetailList[i].targetAreaCode = array[1];
							this.wmsStockTransferDetailList[i].targetShelves = array[2];
							this.wmsStockTransferDetailList[i].targetLocation = array[3] + '-' + array[4];
						} else {
							this.wmsStockTransferDetailList[i].newStorageLocationBarcode = null;
							this.$modal.msg("目标仓库与原仓库不一致!");
						}
						break;
				}
			},
			judgeSameWhCode(code) {
				if (code == this.formData.whCode) return true;
				return false;
			},
			scanBarNewStorageLocationBarcode(i) {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.$set(_this.wmsStockTransferDetailList[i], "newStorageLocationBarcode", res
							.result);
						_this.splitNewStlBarcode(i);
					}
				});
			},
			getMatList() {
				listStock(this.formData).then(res => {
					console.log(res)
					for (let obj of res.rows) {
						obj.MaterialAvailableNumber = obj.number - obj.lockNumber;
					}
					this.wmsMaterialDetailList = res.rows
				})
			},
			//运输员
			scanBarTransportBy() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.transportBy = res.result;
					}
				});
			},
			//发起人
			scanBarTransferBy() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.transferBy = res.result;
					}
				});
			},
			submit() {
				let judge = true;
				for (let i = 0; i < this.wmsStockTransferDetailList.length; i++) {
					if (this.wmsStockTransferDetailList[i].wmsMaterialStockId == null) {
						console.log(this.wmsStockTransferDetailList)
						this.$modal.msg("存在未选择库存的明细！");
						judge = false;
						break;
					} else if (this.wmsStockTransferDetailList[i].actualNumber == null) {
						this.$modal.msg("实际数量不能为空！");
						judge = false;
						break;
					} else if (this.wmsStockTransferDetailList[i].targetAreaCode == null) {
						this.$modal.msg("目标库区不能为空！");
						judge = false;
						break;
					}
				}
				console.log(judge);
				if (judge == true) {
					const _this = this;
					this.$refs.form.validate().then(res => {
						uni.showModal({
							title: '提示',
							content: '您确定转移该物料吗？',
							success: function(res) {
								if (res.confirm) {
									let data = {
										warehouse: _this.formData.whCode,
										//跳过审批流
										transferStatus: '4',
										transferBy: _this.formData.transferBy,
										transportBy: _this.formData.transportBy,
										wmsStockTransferDetailList: _this
											.wmsStockTransferDetailList
									}
									addTransfer(data).then(async res => {
										_this.$modal.msgSuccess("转移成功!");
										setTimeout(() => {
											_this.$tab.switchTab(
												"/pages/work/index");
										}, 500);
									});
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
						});
					});
				}

			},
		}
	}
</script>

<style scoped>
	.divider {
		display: flex;
		align-items: center;
		justify-content: center;
		margin-top: 10px;
		margin-bottom: 10px;
		height: 1px;
		background-color: #F1F1F1;
	}

	.divider span {
		padding: 5px;

	}
</style>