<template>
	<view class="container">
		<uni-forms ref="form" :modelValue="formData" labelWidth="120px" :rules="rules">
			<uni-forms-item label="盘点作业单" name="wmsMatInvCode" required>
				<uni-easyinput suffixIcon="scan" @iconClick="scanBar" @change="scanBarCode"
					v-model="formData.wmsMatInvCode" type="text" :focus='true' />
			</uni-forms-item>
			<uni-forms-item label="盘点单明细编码" name="wmsInvDetailDefCode" required>
				<uni-easyinput suffixIcon="scan" @iconClick="scanBar" @change="getInvDetailInfo"
					v-model="formData.wmsInvDetailDefCode" type="text" />
			</uni-forms-item>
			<u-divider text="详细信息"></u-divider>
			<uni-forms-item label="物料编码" name="materialCode">
				<uni-easyinput v-model="formData.materialCode" type="text" disabled />
			</uni-forms-item>
			<uni-forms-item label="物料名称" name="materialName">
				<uni-easyinput type="text" v-model="formData.materialName" disabled />
			</uni-forms-item>
			<uni-forms-item label="物料批号" name="batchNo">
				<uni-easyinput disabled type="text" v-model="formData.batchNo" />
			</uni-forms-item>
			<uni-forms-item label="仓库编码" name="whCode">
				<uni-easyinput disabled type="text" v-model="formData.whCode" />
			</uni-forms-item>
			<uni-forms-item label="库区编码" name="areaCode">
				<uni-easyinput disabled type="text" v-model="formData.areaCode" />
			</uni-forms-item>
			<uni-forms-item label="库位编码" name="storageLocationCode">
				<uni-easyinput disabled type="text" v-model="formData.storageLocationCode" />
			</uni-forms-item>
			<uni-forms-item label="账面数量" name="theoryNumber">
				<uni-easyinput disabled type="number" v-model="formData.theoryNumber" />
			</uni-forms-item>
			<uni-forms-item label="实盘数量" name="actualNumber">
				<u-number-box inputWidth="auto" button-size="36" v-model="formData.actualNumber" min="0"></u-number-box>
			</uni-forms-item>
		</uni-forms>
		<u-button type="primary" @click="submit">提交</u-button>
	</view>
</template>

<script>
	import {
		getInvJob
	} from "@/api/wms/invJob.js";
	import {
		getInvJobDetail,
		updateInvJobDetail
	} from "@/api/wms/invJobDetail.js"
	import {
		listMaterial
	} from "@/api/wms/request.js";
	export default {
		data() {
			return {
				wmsInvDetailList: null,
				invJobDetailInfo: null,
				formData: {
					wmsMatInvCode: null,
					wmsInvDetailDefCode: null,
					materialCode: null,
					materialName: null,
					batchNo: null,
					whCode: null,
					areaCode: null,
					storageLocationCode: null,
					theoryNumber: null,
					actualNumber: null,
				},
				rules: {
					wmsMatInvCode: {
						rules: [{
							required: true,
							errorMessage: '请输入盘点单!'
						}]
					},
					wmsInvDetailDefCode: {
						rules: [{
							required: true,
							errorMessage: '请输入盘点单明细编码!'
						}]
					},
					actualNumber: {
						rules: [{
							required: true,
							errorMessage: '请输入实盘数量!'
						}]
					}
				}
			}
		},
		methods: {
			clearInvDeatil() {
				this.actualNumber = 0
				// 重置表单信息
				Object.keys(this.formData).forEach(key => {
					if (key != "wmsMatInvCode" && key != "wmsInvDetailDefCode") {
						this.formData[key] = null
					}
				})
			},
			async scanBarCode() {
				//重置表单信息
				this.clearInvDeatil()
				if (this.formData.wmsMatInvCode) {
					const invId = this.formData.wmsMatInvCode.slice(4);
					const res = await getInvJob(invId)
					this.wmsInvDetailList = res.data.wmsInvJobDetailList
				}
				/*
				console.log(invId);
				getInvJob(invId).then(async res => {
					console.log(res);
					if (res.data != null) {
						for (var i in res.data.wmsMatInvDetailDefList) {
							if (res.data.wmsMatInvDetailDefList[i].wmsInvDetailDefCode == this
								.formData.wmsInvDetailDefCode) {
								console.log(res.data.wmsMatInvDetailDefList[i].actualNumber);
								this.formData.materialCode = res.data.wmsMatInvDetailDefList[i]
									.materialCode;
								this.formData.materialName = res.data.wmsMatInvDetailDefList[i]
									.materialName;
								this.formData.batchNo = res.data.wmsMatInvDetailDefList[i].batchNo;
								this.formData.whCode = res.data.wmsMatInvDetailDefList[i]
									.whCode;
								this.formData.areaCode = res.data.wmsMatInvDetailDefList[i]
									.areaCode;
								this.formData.storageLocationCode = res.data.wmsMatInvDetailDefList[i].storageLocationCode;
								this.formData.theoryNumber = res.data.wmsMatInvDetailDefList[i]
									.theoryNumber;
							}
						}
					} else {
						this.$modal.msg("未检索到该盘点信息！")
					}
				})
				*/
			},
			getInvDetailInfo() {
				this.clearInvDeatil()
				if (this.formData.wmsMatInvCode) {
					const invDetail = this.wmsInvDetailList.find(
						item => item.wmsInvJobDetailCode === this.formData.wmsInvDetailDefCode
					);
					this.invJobDetailInfo = invDetail

					this.formData.materialCode = invDetail.materialCode
					this.formData.materialName = invDetail.materialName
					this.formData.batchNo = invDetail.batchNo
					this.formData.whCode = invDetail.whCode
					this.formData.areaCode = invDetail.areaCode
					this.formData.shelvesCode = invDetail.shelvesCode
					this.formData.storageLocationCode = invDetail.storageLocationCode
					this.formData.theoryNumber = invDetail.theoryNumber
				}
			},
			//盘点单
			scanBar() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.wmsMatInvCode = res.result;
						_this.scanBarCode();
					}
				});
			},
			//盘点单明细编码
			scanBar1() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.wmsInvDetailDefCode = res.result;
						_this.scanBarCode();
					}
				});
			},
			submit() {
				const _this = this;
				_this.$refs.form.validate().then(res => {
					uni.showModal({
						title: '提示',
						content: '您确定盘点该物料吗？',
						success: function(res) {
							if (res.confirm) {
								let obj = _this.formData.wmsMatInvCode.slice(4);
								_this.invJobDetailInfo.actualNumber = _this.formData.actualNumber
								updateInvJobDetail(_this.invJobDetailInfo).then(() => {
									_this.$modal.closeLoading();
									_this.$modal.msgSuccess("盘点成功!");
									setTimeout(() => {
										_this.$tab.switchTab("/pages/work/index");
									}, 500);
								});
								/*
								getInvJob(obj).then(async res => {
									for (var i in res.data
											.wmsMatInvDetailDefList) {
										if (res.data
											.wmsMatInvDetailDefList[i]
											.wmsInvDetailDefCode == _this
											.formData
											.wmsInvDetailDefCode) {
											res.data
												.wmsMatInvDetailDefList[i]
												.actualNumber =
												_this.formData.actualNumber;
											_this.$modal.loading('提交中')
											updateInvJobDetail(res.data).then(
												async res => {
													_this.$modal
														.closeLoading();
													_this.$modal
														.msgSuccess(
															"盘点成功!"
														);
													setTimeout(
														() => {
															_this
																.$tab
																.switchTab(
																	"/pages/work/index"
																);
														}, 500);
												});
										}
									}
								});
								*/
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				});
			},
		}
	}
</script>

<style scoped>
	.container {
		padding: 15px;
	}

	.uni-forms-item {
		margin-bottom: 16px;
	}
</style>