<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="purchaseReceiveCode">
					<uni-easyinput suffixIcon="scan" @iconClick="scanBar" @confirm="scanBarReceiveCode"
						v-model="formData.purchaseReceiveCode" type="text" />
				</uni-forms-item>
				<uni-forms-item label="采购质检单" :labelWidth='90' name="purchaseQualityCode">
					<uni-easyinput suffixIcon="scan" @iconClick="scanBarQuality" @confirm="scanBarQualityCode"
						v-model="formData.purchaseQualityCode" type="text" />
				</uni-forms-item>
				<uni-forms-item label="采购入库单" :labelWidth='90' name="purchaseInCode">
					<uni-easyinput suffixIcon="scan" @iconClick="scanBarPInCode" @confirm="scanBarpurchaseInCode"
						v-model="formData.purchaseInCode" type="text" />
				</uni-forms-item>
				<uni-forms-item label="仓库编码" :labelWidth='90' name="warehouseCode">
					<uni-easyinput type="text" suffixIcon="scan" @iconClick="scanBarwarehouseCode"
						v-model="formData.warehouseCode" />
					<uni-data-select style="margin-top: 5px;" v-model="formData.warehouseCode"
						:localdata="wcList"></uni-data-select>
				</uni-forms-item>
				<uni-forms-item label="上架员" :labelWidth='90' name="shelfPutBy">
					<uni-easyinput type="text" suffixIcon="scan" @iconClick="scanBarshelfPutBy"
						v-model="formData.shelfPutBy" @confirm="isEmp" />
					<uni-data-picker popup-title="请选择上架员" :localdata="dataTree" v-model="pickerData" @change="onchange"
						@nodeclick="onnodeclick" @popupopened="onpopupopened" @popupclosed="onpopupclosed"
						style="margin-top: 5px;" :preload="true">
					</uni-data-picker>
				</uni-forms-item>
				<uni-forms-item label="入库方式" :labelWidth='90'>
					<u-radio-group v-model="value" iconPlacement="left">
						<u-radio label="正常" name="正常"></u-radio>
						<u-radio label="扫物料标签" name="扫物料标签" style="margin-left: 10px;"></u-radio>
					</u-radio-group>
				</uni-forms-item>
				<button size="mini"
					v-if="value=='扫物料标签' &&  isSupplierCode == true &&formData.wmsPurchaseInDetailList.length != 0"
					type="primary" style="text-align: center;margin-left: 30%;font-size: 18px;"
					@click="show=!show">添加物料标签</button>
				<button size="mini"
					v-if="value=='扫物料标签' &&formData.wmsPurchaseInDetailList.length == 0 &&isSupplierCode == false"
					type="primary" style="text-align: center;margin-left: 30%;font-size: 18px;"
					@click="show=!show">添加物料标签</button>
				<u-modal :show="show" title="扫描物料标签编码" showCancelButton closeOnClickOverlay
					@cancel="cancelMaterialLabel" @close="cancelMaterialLabel" :showConfirmButton="false">
					<uni-easyinput suffixIcon="scan" @iconClick="scanBarMaterialLabel" v-model="materialLabel"
						type="text" @confirm="confirmMaterialLabel" maxlength="-1" :focus="true" />
				</u-modal>
				<!-- </uni-collapse-item> -->
				<uni-collapse-item title="采购入库单单明细" :open="true">
					<uni-swipe-action>
						<uni-swipe-action-item v-for="(item, index) in formData.wmsPurchaseInDetailList" :key="index">
							<view>
								<uni-badge :text="index+1" class="uni-badge-left-margin" type="primary"></uni-badge>
								<button @click="open(index,item)" size="mini"
									v-if="value=='扫物料标签' &&  isSupplierCode == true"
									:disabled="buttonDisabled ||!item.id || !item.materialBatchNo || !item.number"
									type="primary">编辑</button>
								<button @click="open(index,item)" size="mini" v-else
									:disabled="buttonDisabled ||!formData.id || !item.materialBatchNo || !item.number"
									type="primary">编辑</button>

							</view>
							<uni-forms-item label="物料编码" :labelWidth='90'>
								<uni-easyinput type="text" disabled v-model="item.materialCode" />
							</uni-forms-item>
							<uni-forms-item label="物料名称" :labelWidth='90'>
								<uni-easyinput type="text" disabled v-model="item.materialName" />
							</uni-forms-item>
							<uni-forms-item label="物料批号" :labelWidth='90'>
								<uni-easyinput type="text" :disabled="item.id" v-model="item.materialBatchNo" />
							</uni-forms-item>
							<uni-forms-item label="箱号" v-if="item.remark" :labelWidth='90'>
								<uni-easyinput type="text" disabled v-model="item.remark" />
							</uni-forms-item>
							<uni-forms-item label="上架数量" :labelWidth='90'>
								<u-number-box inputWidth="120" :disabled="item.id" button-size="36"
									v-model="item.secondNumber" min="0"></u-number-box>
							</uni-forms-item>

							<uni-forms-item label="单位" :labelWidth='90' :disabled="true">
								<uni-easyinput type="text" disabled v-model="item.unitText" />
								<!-- <uni-data-select style="margin-top: 10px;" v-model="item.secondUnitId"
									:localdata="unitList"></uni-data-select> -->
							</uni-forms-item>
						</uni-swipe-action-item>
					</uni-swipe-action>
				</uni-collapse-item>
			</uni-forms>
		</uni-collapse>
		<uni-row :gutter="10">
			<uni-col :span="12">
				<u-button type="success" @click="savesubmit">保存</u-button>
			</uni-col>
			<uni-col :span="12">
				<u-button type="primary" :disabled="!isAllListed" @click="submit">提交</u-button>
			</uni-col>
		</uni-row>
	</view>
</template>

<script>
	import {
		listQuality,
		getQuality,
		addIn,
		listReceive,
		getReceive,
		getListDetail,
		updateIn,
		listIn,
		getIn,
		getQualityDetail,
		getReceiveDetail,
		getDetailListsBySRM,
		getListDetailByBL,
		listSupplier
	} from "@/api/wms/purchase.js";
	import {
		listMaterial
	} from "@/api/wms/request.js";
	import {
		listUnit
	} from "@/api/basic/unit";
	import {
		listEmployee
	} from "@/api/mes/jobIn.js";
	import {
		getConfigKey
	} from "@/api/system/config.js";
	import {
		listDepartment
	} from "@/api/basic/department";
	import {
		listWarehouse
	} from "@/api/wms/warehouse";
	import SecondNumberChangeToConvert from "@/components/SecondNumberChangeToConvert/index.vue";

	import _ from 'lodash';
	export default {
		created() {
			//监听编辑批号分批后的状态
			uni.$on('backWithParam', (param) => {
				if (param.status && param.index) {
					this.formData.wmsPurchaseInDetailList[Number(param.index)].status = param.status
				}
				this.checkIsListed();
			});
		},
		mounted() {
			//U9库存模式
			getConfigKey("wms.u9.whMode").then((response) => {
				this.wmsu9whMode = response.msg;
			});
			listDepartment().then((res) => {
				this.dptList = res.rows
			})
			listEmployee().then((res) => {
				this.empList = res.rows
			})
			listWarehouse().then((res) => {
				this.wcList = res.rows.map(item => {
					let obj = {
						text: item.warehouseName,
						value: item.warehouseCode
					}
					return obj
				})
			})
			listUnit().then((res) => {
				this.unitList = res.rows.map(item => {
					let obj = {
						text: item.unitCode + ":" + item.unitName,
						value: item.id
					}
					return obj
				})
			})
		},
		data() {
			return {
				buttonDisabled: false,
				dptList: [],
				empList: [],
				wcList: [],
				unitList: [],
				item: '',
				dataTree: [],
				pickerData: '',
				wmsu9whMode: false,
				isSupplierCode: false,
				value: '正常',
				show: false,
				materialLabel: null,
				//是否都入库
				isAllListed: false,
				isRk: false,
				//是否打开编辑按钮
				isSplit: true,
				rightOptions: [{
					text: '删除',
					style: {
						backgroundColor: '#ff2a17'
					}
				}, ],
				formData: {
					purchaseReceiveCode: null,
					warehouseCode: null,
					wmsPurchaseInDetailList: [],
					status: "1",
					id: null,
					purchaseInCode: null,
					shelfPutBy: null
				},
				rules: {
					shelfPutBy: {
						rules: [{
							required: true,
							errorMessage: '请输入上架员!'
						}]
					},
					warehouseCode: {
						rules: [{
							required: true,
							errorMessage: '请输入仓库编码!'
						}]
					},
				}
			}
		},
		methods: {
			onnodeclick(e) {
				console.log(e)
				this.item = e
				this.onchange(this.item)
			},
			onpopupopened(e) {
				this.dataTree = []
				this.empList.filter(item => item.deptId).forEach(item => {
					item.departmentTitle = this.dptList.find(item2 => item2.id == item.deptId).departmentTitle
					// 检查dataTree中是否已存在相同部门
					let existingDept = this.dataTree.find(dept => dept.value === item.deptId);
					if (existingDept) {
						// 如果已存在相同部门，则将员工信息push进该部门的children数组
						existingDept.children.push({
							text: item.name,
							value: item.empCode
						});
					} else {
						// 如果不存在相同部门，则创建一个新部门对象，包括children数组，并将员工信息push进去
						let newDept = {
							text: item.departmentTitle,
							value: item.deptId,
							children: [{
								text: item.name,
								value: item.empCode
							}]
						};
						this.dataTree.push(newDept);
					}
				})
				console.log(this.dataTree)
			},
			onpopupclosed() {
				//处理不同步
				// this.$nextTick(() => {
				// this.pickerData = this.item.value;
				// this.formData.pickerData = this.item.value;
				// if (!this.item) return
				// this.onchange(this.item)
				// });
			},
			onchange(e) {
				console.log(e)
				this.formData.shelfPutBy = null
				this.$set(this.formData, 'shelfPutBy', e.value.split('/')[0])
			},
			deleteDetail(index) {
				this.formData.wmsPurchaseInDetailList.splice(index, 1);
			},
			clickDetail(itemIndex, {
				position,
				index
			}) {
				if (index == 0) {
					this.deleteDetail(itemIndex);
				}
			},
			addMaterialLabel() {
				this.show = true;
			},
			cancelMaterialLabel() {
				this.materialLabel = null;
				this.show = false;
			},
			confirmMaterialLabel(data) {
				//判断是否供应商扫码
				if (data.includes('@')) {
					// console.log(1)
					this.isSupplierCode = true;
					let array = data.split('@');
					let new_supplierCode = array[0];
					let new_u9SourceBillCode = array[3].split('|')[0];
					let new_erpDocCode1 = array[4].split('|')[0];
					let new_erpDocLineKey1 = array[4].split('|')[1];
					let lotNo = array[4].split('|')[2];
					let supplierCode = null;
					console.log(new_erpDocLineKey1, lotNo);
					getDetailListsBySRM({
						u9SourceBillCode: new_u9SourceBillCode,
						erpDocCode1: new_erpDocCode1,
						erpDocLineKey1: new_erpDocLineKey1
					}).then(res => {
						console.log(res)
						//查供应商编码
						listSupplier({
							remark: new_supplierCode
						}).then(res => {
							if (res.rows.length > 0) {
								supplierCode = res.rows[0].supplierCode
							}
						})
						//判断有没有入库
						if (res.inList.length == 0) {
							//未入库
							//检查是否质检
							if (res.qualityList.length == 0) {
								this.$modal.msg("该条物料还未质检！");
								this.materialLabel = null;
								this.show = false;
							} else {
								//已质检才能入库
								//判断有无完成质检
								if (res.qualityList[0].status == 3) {
									console.log(1)
									this.formData.purchaseQualityCode = res.qualityList[0].purchaseQualityCode
									//判断当前单据下是否已扫描添加过明细
									if (this.formData.wmsPurchaseInDetailList.length > 0) {
										//有明细
										//判断是否在同个主表同一物料
										if (this.formData.u9SourceBillCode == new_u9SourceBillCode && this
											.formData
											.erpDocCode1 == new_erpDocCode1 && this.formData
											.wmsPurchaseInDetailList[0].erpDocLineKey1 == new_erpDocLineKey1) {
											//判断是总码还是外箱
											if (this.formData.wmsPurchaseInDetailList.length[0].remark) {
												//外箱
												//查询箱号是否重复
												if (this.formData.wmsPurchaseInDetailList.find(item =>
														item.remark == lotNo
													)) {
													this.$modal.msg("该条物料已添加！")
												} else {
													//箱号没重复
													let obj = {
														materialCode: array[1],
														materialBatchNo: array[2],
														number: Number(array[6]),
														//ASN行号
														erpDocLineKey1: array[4].split('|')[1],
														remark: lotNo
													}
													// //供应商代码
													// this.formData.supplierCode = new_supplierCode;
													// //采购订单号
													// this.formData.u9SourceBillCode = new_u9SourceBillCode;
													// //ASN单号
													// this.formData.erpDocCode1 = new_erpDocCode1;
													this.formData.wmsPurchaseInDetailList.push(obj);
												}
											} else {
												this.$modal.msg("该条物料已添加！")
											}
										} else {
											this.$modal.msg("该条物料与现有明细不在同一ASN行号下！")
										}
									} else {
										//未入库，且当前单据下没有明细
										//判断是总码还是外箱，外箱码是多加一个箱数量
										if (array.length < 7) {
											//总码
											let obj = {
												materialCode: array[1],
												materialBatchNo: array[2],
												number: Number(array[5]),
												//ASN行号
												erpDocLineKey1: new_erpDocLineKey1,
											}
											//供应商代码
											this.formData.supplierCode = supplierCode;
											//采购订单号
											this.formData.u9SourceBillCode = new_u9SourceBillCode;
											//ASN单号
											this.formData.erpDocCode1 = new_erpDocCode1;
											this.formData.wmsPurchaseInDetailList.push(obj);
										} else {
											console.log(2)
											//外箱
											let obj = {
												materialCode: array[1],
												materialBatchNo: array[2],
												number: Number(array[5]),
												//ASN行号
												erpDocLineKey1: new_erpDocLineKey1,
												remark: lotNo
											}
											//供应商代码
											this.formData.supplierCode = supplierCode;
											//采购订单号
											this.formData.u9SourceBillCode = new_u9SourceBillCode;
											//ASN单号
											this.formData.erpDocCode1 = new_erpDocCode1;
											this.formData.wmsPurchaseInDetailList.push(obj);
										}
									}
								} else {
									this.$modal.msg("该条物料还未完成质检！");
								}

							}
						} else {
							//已有入库
							console.log(1)
							//判断是总码还是外箱，外箱码是多加一个箱数量
							if (array.length < 7) {
								//总码
								this.$modal.msg("该条物料已入库！")
							} else {
								//外箱
								//判断当前单据下是否已扫描添加过明细
								if (this.formData.wmsPurchaseInDetailList.length > 0) {
									//有明细
									//判断是否在同个主表同一物料
									if (this.formData.u9SourceBillCode == new_u9SourceBillCode && this.formData
										.erpDocCode1 == new_erpDocCode1 && this.formData
										.wmsPurchaseInDetailList[0].erpDocLineKey1 == new_erpDocLineKey1) {
										//查询数据库箱号是否重复
										if (res.inList[0].wmsPurchaseInDetailList.find(item =>
												item.remark == lotNo
											)) {
											this.$modal.msg("该条物料已添加！")
										} else {
											//查询当前单据明细箱号是否重复
											if (this.formData.wmsPurchaseInDetailList.find(item =>
													item.remark == lotNo
												)) {
												this.$modal.msg("该条物料已添加！")
											} else {
												console.log(1)
												//外箱
												let obj = {
													materialCode: array[1],
													materialBatchNo: array[2],
													number: Number(array[5]),
													//ASN行号
													erpDocLineKey1: new_erpDocLineKey1,
													remark: lotNo
												}
												// //供应商代码
												// this.formData.supplierCode = new_supplierCode;
												// //采购订单号
												// this.formData.u9SourceBillCode = new_u9SourceBillCode;
												// //ASN单号
												// this.formData.erpDocCode1 = new_erpDocCode1;
												this.formData.wmsPurchaseInDetailList.push(obj);
											}
										}
									} else {
										this.$modal.msg("与现有明细不在同一ASN行号下！")
									}
								} else {
									console.log(2)
									//查询数据库箱号是否重复
									if (res.inList[0].wmsPurchaseInDetailList.find(item =>
											item.remark == lotNo
										)) {
										this.$modal.msg("该条物料已添加入库！")
										this.formData = res.inList[0]
									} else {
										this.formData = res.inList[0]
										//外箱
										let obj = {
											materialCode: array[1],
											materialBatchNo: array[2],
											number: Number(array[5]),
											//ASN行号
											erpDocLineKey1: new_erpDocLineKey1,
											remark: lotNo
										}
										// //供应商代码
										// this.formData.supplierCode = new_supplierCode;
										// //采购订单号
										// this.formData.u9SourceBillCode = new_u9SourceBillCode;
										// //ASN单号
										// this.formData.erpDocCode1 = new_erpDocCode1;
										this.formData.wmsPurchaseInDetailList.push(obj);
									}
								}
							}
						}
					});
					this.materialLabel = null;
					this.show = false;
				} else {
					data = JSON.parse(data)
					if (data) {
						getQualityDetail({
							purchaseReceiveDetailId: data.id
						}).then(res => {
							//判断有没有质检
							console.log(res)

							if (res.data.length > 0 && res.data[0].qualityStatus == '3') {
								let num = 0;
								console.log(res.data)
								for (var i in res.data) {
									num += res.data[i].inNumber
								}
								let unitText = null;
								let unitobj = this.unitList.find(i => i.value == res.data[0].secondUnitId)
								if (unitobj) {
									unitText = unitobj.text
								}
								let obj = {
									materialCode: res.data[0].materialCode,
									materialName: res.data[0].materialName,
									materialBatchNo: res.data[0].materialBatchNo,
									number: num,
									purchaseReceiveDetailId: data.id,
									purchaseQualityDetailId: res.data[0].id,
									dynamicField1: res.data[0].dynamicField1,
									unitId: res.data[0].unitId,
									unit: res.data[0].unit,
									secondNumber: res.data[0].secondInNumber,
									secondUnitId: res.data[0].secondUnitId,
									unitText: unitText,
									status: 0
								}
								this.formData.purchaseReceiveCode = res.data[0].purchaseReceiveCode;
								this.formData.purchaseQualityCode = res.data[0].purchaseQualityCode;
								this.formData.supplierCode = res.data[0].supplierCode;
								this.formData.supplierName = res.data[0].supplierName;
								this.formData.warehouseCode = res.data[0].warehouseCode;
								this.formData.wmsPurchaseInDetailList.push(obj);
								this.materialLabel = null;
								this.show = false;
							} else {
								this.$modal.msg("该条物料还未质检，无法入库！");
							}
						})
					}
				}
			},
			scanBarMaterialLabel() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.materialLabel = res.result;
						// console.log(materialLabel)
						_this.confirmMaterialLabel(_this.materialLabel);
					}
				});
			},
			//进入编辑页
			open(index, row) {
				console.log(row)
				// 禁用按钮
				this.buttonDisabled = true;
				//拆分下的列表
				this.wmsLotNoList = [];
				//选中的物料明细项
				let selectedRow = row;
				//是否分批
				let isSecondOpen = false;
				//每份数量
				let eachNumber = null;
				//拆分数量
				let splitNumber = null;
				//头部信息
				let singleInfo = {};
				singleInfo.materialCode = row.materialCode;
				singleInfo.materialBatchNo = row.materialBatchNo;
				singleInfo.batchNo = row.materialBatchNo;
				// singleInfo.materialLotNo = row.materialLotNo;
				singleInfo.number = row.number;
				singleInfo.unit = row.unit;
				singleInfo.purchaseInCode = row.purchaseInCode;
				singleInfo.materialName = row.materialName;
				// singleInfo.materialType = row.type;
				singleInfo.unitName = row.unit;
				singleInfo.specification = row.specification;
				singleInfo.u9StorageCode = row.u9StorageCode;
				singleInfo.whCode = row.whCode;
				singleInfo.areaCode = row.areaCode;
				singleInfo.shelvesCode = row.shelvesCode;
				singleInfo.storageLocationCode = row.storageLocationCode;
				singleInfo.storageInBillCode = row.purchaseInCode;
				singleInfo.storageInBillDetailCode = row.purchaseInDetailCode;
				singleInfo.u9LineId = row.u9RcvLineId;
				singleInfo.erpDocLineKey1 = row.erpDocLineKey1;
				singleInfo.unitId = row.unitId;
				singleInfo.secondUnitId = row.secondUnitId;
				singleInfo.secondNumber = row.secondNumber;
				this.formData.purchaseInCode = row.purchaseInCode;
				//U9模式下，使用入库单明细进行查询
				if (this.wmsu9whMode == "true") {
					console.log(row);
					getListDetailByBL({
						storageInBillDetailCode: row.purchaseInDetailCode,
					}).then((response) => {
						console.log(response);
						this.handleListDetail(row, response, singleInfo, index);
					});
				} else {
					getListDetailByBL(singleInfo).then((response) => {
						console.log(response);
						this.handleListDetail(row, response, singleInfo, index);
					});
				}
			},
			handleListDetail: _.debounce(function(row, response, singleInfo, index) {
				console.log(1)
				//选中的物料明细项
				let selectedRow = row;
				//是否分批
				let isSecondOpen = false;
				//每份数量 
				let eachNumber = null;
				//拆分数量
				let splitNumber = null;
				console.log(2)
				/** 导入拆分信息 */
				if (response.materialDetailList.length > 0) {
					if (response.materialDetailList[0].splitNumber != null) {
						console.log(3)
						splitNumber = response.materialDetailList[0].splitNumber;
					} else if (response.materialDetailList[0].eachNumber != null) {
						console.log(4)
						eachNumber = response.materialDetailList[0].eachNumber;
					}
				}
				console.log(response)
				/** 导入拆分详情 */

				if (response.materialStockList) {
					if (response.materialStockList.length > 0) {
						console.log(5)
						this.wmsLotNoList = response.materialStockList.map((item) => {
							item.batchNo = item.materialBatchNo;
							item.locCascade = [
								item.whCode,
								item.areaCode,
								item.shelvesCode,
								item.storageLocationCode,
							];
							return item;
						});
					}

					if (response.materialStockList.length != 0) {
						isSecondOpen = true;
					}
				}

				console.log(6)


				console.log(this.wmsLotNoList);
				uni.navigateTo({
					url: '/pages/wms/purchase/purchaseListing?singleInfo=' +
						encodeURIComponent(JSON
							.stringify(
								singleInfo)) +
						'&wmsLotNoList=' + encodeURIComponent(JSON.stringify(
							this.wmsLotNoList)) +
						'&selectedRow=' + encodeURIComponent(JSON.stringify(
							selectedRow)) +
						'&eachNumber=' + eachNumber +
						'&splitNumber=' + splitNumber +
						'&isSecondOpen=' + isSecondOpen +
						'&index=' + index
				});
				// 在页面跳转后恢复按钮状态
				setTimeout(() => {
					this.buttonDisabled = false;
				}, 500); // 延时，确保跳转完成后再启用按钮
			}, 1000, {
				leading: true,
				trailing: false
			}),
			splitStlBarcode() {

				const _this = this;
				const detail = _this.formData;

				detail.whCode = null;
				detail.areaCode = null;
				detail.shelvesCode = null;
				detail.storageLocationCode = null;

				let barcode = detail.storageLocationBarcode;
				if (!barcode) {
					return; // 如果没有条码，不做任何处理
				}

				let [whCode, areaCode, shelvesCode, storageLocationCode, extra] = barcode.split('-');

				const checkAndSetField = (obj, field, value, msg) => {
					if (!value) {
						_this.$modal.msg(msg);
						_this.$set(obj, field, null);
						_this.$nextTick(() => {
							_this.$set(obj, "storageLocationBarcode", null);
						});
						return false;
					}
					return true;
				};

				let warehouseObj = _this.$store.getters.warehouseOptions.find(item => item.warehouseCode == whCode);
				if (!checkAndSetField(detail, 'whCode', warehouseObj, "仓库不存在！")) return;

				let areaObj = _this.$store.getters.areaOptions.find(item => item.storageAreaCode == areaCode);
				if (!checkAndSetField(detail, 'areaCode', areaObj, "库区不存在！")) return;

				let shelvesObj = _this.$store.getters.shelvesOptions.find(item => item.storageShelvesCode == shelvesCode);
				if (!checkAndSetField(detail, 'shelvesCode', shelvesObj, "货架不存在！")) return;

				let locationObj = _this.$store.getters.locationOptions.find(item => item.storageLocationCode ==
					`${storageLocationCode}-${extra}`);
				if (!checkAndSetField(detail, 'storageLocationCode', locationObj, "库位不存在！")) return;

				// 更新formData中的对应字段
				detail.whCode = whCode || null;
				detail.areaCode = areaCode || null;
				detail.shelvesCode = shelvesCode || null;
				detail.storageLocationCode = extra ? `${storageLocationCode}-${extra}` : storageLocationCode || null;

			},
			scanBarstorageLocationBarcode(i) {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						// _this.formData.storageLocationBarcode = res.result;
						_this.$set(_this.formData, "storageLocationBarcode", res
							.result);
						_this.splitStlBarcode();
					}
				});
			},
			scanBarQualityCode() {
				listQuality(this.formData.purchaseQualityCode).then(res => {
					if (res.total > 0) {
						getQuality(res.rows[0].id).then((res) => {
							res.data.wmsPurchaseQualityDetailList.forEach(
								item => {
									item.id = null;
									item.number = item.actualNumber - item
										.number + item
										.passNumber;
									item.u9RcvLineId = item.u9DocLineKey;
								})
							this.formData.supplierCode = res.data.supplierCode;
							this.formData.u9SourceBillCode = res.data
								.u9SourceBillCode;
							this.formData.erpDocCode1 = res.data.erpDocCode1;
							this.formData.purchaseReceiveCode = res.data
								.purchaseReceiveCode;
							this.formData.wmsPurchaseInDetailList = res.data
								.wmsPurchaseQualityDetailList;
						});
					} else {
						this.$modal.msg("未查询到该质检单相关信息！");
					}
				})
			},
			scanBarQuality() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.purchaseQualityCode = res.result;
						_this.scanBarQualityCode();
					}
				});
			},
			scanBarReceiveCode() {
				if (this.formData.purchaseReceiveCode && this.formData.purchaseReceiveCode !=
					"") {
					this.formData.purchaseInCode = null;
					listReceive({
						purchaseReceiveCode: this.formData.purchaseReceiveCode
					}).then(async res => {
						if (res.rows.length > 0) {
							getReceive(res.rows[0].id).then(res => {
								this.formData.wmsPurchaseInDetailList = res
									.data
									.wmsPurchaseReceiveDetailList
								this.formData.wmsPurchaseInDetailList
									.forEach(element => {
										element.number = element
											.actualNumber;
										element.status = 0;
										element.id = null;
									});
								this.formData.supplierCode = res.data
									.supplierCode;
								this.formData.u9SourceBillCode = res.data
									.u9SourceBillCode;
								this.formData.erpDocCode1 = res.data
									.erpDocCode1;
							})
						} else {
							this.$modal.msg("未查询到该收货单相关信息！");
						}
					});
				} else {
					this.$modal.msg("请输入采购收货单!");
				}
			},
			//采购收货单
			scanBar() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.purchaseReceiveCode = res.result;
						_this.scanBarReceiveCode();
					}
				});
			},
			scanBarshelfPutBy() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.shelfPutBy = res.result;
						_this.isEmp(res.result);
					}
				});
			},
			//验证是否为员工
			isEmp(code) {
				listEmployee({
					empCode: code
				}).then(res => {
					if (res.total == 0) {
						this.$modal.msg("未查询到该员工！");
						this.formData.shelfPutBy = null;
					}
				})
			},
			scanBarPInCode() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.purchaseInCode = res.result;
						_this.scanBarpurchaseInCode()
					}
				});
			},
			scanBarpurchaseInCode() {
				if (this.formData.purchaseInCode && this.formData.purchaseInCode != "") {
					this.formData.purchaseReceiveCode = null;
					listIn({
						purchaseInCode: this.formData.purchaseInCode
					}).then(res => {
						if (res.rows.length > 0) {
							console.log(res.rows[0].id)
							getIn(res.rows[0].id).then((response) => {
								this.formData = response.data;
								this.checkIsListed();
							});
						} else {
							this.$modal.msg("未查询到该入库单相关信息！");
						}
					});
				}
			},
			//仓库编号
			scanBarwarehouseCode() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.warehouseCode = res.result;
					}
				});
			},
			savesubmit() {
				const _this = this;
				// this.$refs["jobInForm"].validate().then(valid => {
				_this.$refs.form.validate().then(res => {
					uni.showModal({
						title: '提示',
						content: '您确定保存吗？',
						success: function(res) {
							if (res.confirm) {
								// if (_this.formData.purchaseReceiveCode) {

								console.log(_this.formData)
								if (_this.formData.id != null) {
									_this.$modal.loading('提交中')
									updateIn(_this.formData).then((
										response) => {
										_this.$modal.closeLoading();
										_this.formData =
											response.data;
										_this.checkIsListed();
										_this.$modal
											.msgSuccess(
												"修改成功！");
									});
								} else {
									console.log(_this
										.formData);
									_this.$modal.loading('提交中')
									addIn(_this.formData).then((
										response) => {
										_this.$modal.closeLoading();
										console.log(_this
											.formData);
										_this.formData =
											response.data;
										_this.checkIsListed();
										_this.$modal
											.msgSuccess(
												"保存成功！");
									});
								}
								// }
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				});
			},
			submit() {
				const _this = this;
				_this.formData.status = 2;
				if (
					_this.formData.wmsPurchaseInDetailList.filter((obj) => obj.status != 1)
					.length == 0
				) {
					_this.formData.status = 3;
				}
				_this.$refs.form.validate().then(res => {
					uni.showModal({
						title: '提示',
						content: '您确定入库该物料吗？',
						success: function(res) {
							if (res.confirm) {
								console.log(_this.formData)
								_this.$modal.loading('提交中')
								updateIn(_this.formData).then(
									async res => {
										_this.$modal.msgSuccess(
											"入库成功!");
										setTimeout(() => {
											_this.$modal.closeLoading();
											_this.$tab
												.switchTab(
													"/pages/work/index"
												);
										}, 500);
									});
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				});
			},
			//检查是否都入库
			checkIsListed() {
				let flag = true;
				this.formData.wmsPurchaseInDetailList.forEach((item) => {
					if (item.status == 0 || item.status == null) flag = false;
				});
				this.isAllListed = flag;
			},
		}
	}
</script>

<style>
</style>