<template>
	<view>
		<uni-forms ref="form" :modelValue="formData" :rules="rules">
			<uni-row>
				<uni-col :span="24">
					<uni-forms-item label="盘点单" :labelWidth='90' name="wmsMatInvCode">
						<uni-easyinput suffixIcon="scan" @iconClick="scanBar" @confirm="scanBarCode"
							v-model="formData.wmsMatInvCode" type="text" />
					</uni-forms-item>
				</uni-col>
				<uni-col :span="24">
					<uni-forms-item label="盘点单明细编码" :labelWidth='90' name="wmsInvDetailDefCode">
						<uni-easyinput suffixIcon="scan" @iconClick="scanBar1" @confirm="scanBarCode"
							v-model="formData.wmsInvDetailDefCode" type="text" />
					</uni-forms-item>
				</uni-col>
				<uni-col :span="24">
					<uni-forms-item label="物料编码" :labelWidth='90' name="materialCode">
						<uni-easyinput v-model="formData.materialCode" type="text" disabled />
					</uni-forms-item>
				</uni-col>
				<uni-col :span="24">
					<uni-forms-item label="物料名称" :labelWidth='90' name="materialName">
						<uni-easyinput type="text" v-model="formData.materialName" disabled />
					</uni-forms-item>
				</uni-col>
				<uni-col :span="24">
					<uni-forms-item label="物料批号" :labelWidth='90' name="batchNo">
						<uni-easyinput disabled type="text" v-model="formData.batchNo" />
					</uni-forms-item>
				</uni-col>
				<uni-col :span="12">
					<uni-forms-item label="仓库编码" :labelWidth='90' name="warehouseCode">
						<uni-easyinput disabled type="text" v-model="formData.warehouseCode" />
					</uni-forms-item>
				</uni-col>
				<uni-col :span="12">
					<uni-forms-item label="库区编码" :labelWidth='90' name="storageAreaCode">
						<uni-easyinput disabled type="text" v-model="formData.storageAreaCode" />
					</uni-forms-item>
				</uni-col>
				<uni-col :span="12">
					<uni-forms-item label="库位编码" :labelWidth='90' name="locCode">
						<uni-easyinput disabled type="text" v-model="formData.locCode" />
					</uni-forms-item>
				</uni-col>
				<uni-col :span="12">
					<uni-forms-item label="账面数量" :labelWidth='90' name="paperNum">
						<uni-easyinput disabled type="number" v-model="formData.paperNum" />
					</uni-forms-item>
				</uni-col>
				<uni-col :span="24">
					<uni-forms-item label="实盘数量" :labelWidth='90' name="firmNum">
						<u-number-box inputWidth="120" button-size="36" v-model="formData.firmNum"
							min="0"></u-number-box>
					</uni-forms-item>
				</uni-col>
			</uni-row>
		</uni-forms>
		<u-button type="primary" @click="submit">提交</u-button>
	</view>
</template>

<script>
	import {
		getInv,
		listInv,
		listInvDetail,
		getInvDetail,
		updateInv
	} from "@/api/wms/inventory.js";
	import {
		listMaterial
	} from "@/api/wms/request.js";
	export default {

		mounted() {
			this.test();
		},
		data() {
			return {
				formData: {
					wmsMatInvCode: null,
					wmsInvDetailDefCode: null,
					materialCode: null,
					materialName: null,
					batchNo: null,
					warehouseCode: null,
					storageAreaCode: null,
					locCode: null,
					paperNum: null,
					firmNum: null,
				},
				rules: {
					wmsMatInvCode: {
						rules: [{
							required: true,
							errorMessage: '请输入盘点单!'
						}]
					},
					wmsInvDetailDefCode: {
						rules: [{
							required: true,
							errorMessage: '请输入盘点单明细编码!'
						}]
					},
					firmNum: {
						rules: [{
							required: true,
							errorMessage: '请输入实盘数量!'
						}]
					}
				}
			}
		},
		methods: {
			scanBarCode() {
				if (this.formData.wmsMatInvCode && this.formData.wmsMatInvCode != "") {
					if (this.formData.wmsInvDetailDefCode != "" && this.formData.wmsInvDetailDefCode) {
						console.log(this.formData.wmsMatInvCode.length);
						if (this.formData.wmsMatInvCode.length >= 8) {
							let obj = this.formData.wmsMatInvCode.slice(4);
							console.log(obj);
							getInv(obj).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].firmNum);
											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.warehouseCode = res.data.wmsMatInvDetailDefList[i]
												.whCode;
											this.formData.storageAreaCode = res.data.wmsMatInvDetailDefList[i]
												.areaCode;
											this.formData.locCode = res.data.wmsMatInvDetailDefList[i].locCode;
											this.formData.paperNum = res.data.wmsMatInvDetailDefList[i]
												.paperNum;
										}
									}
								} else {
									this.$modal.msg("未检索到该盘点信息！")
								}
							});
						} else {
							this.$modal.msg("请输入有效盘点单！")
						}
					} else {

						this.$modal.msg("请输入盘点单明细编码！")
					}
				} else {
					this.$modal.msg("请输入盘点单！")
				}
			},
			//盘点单
			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);
								getInv(obj).then(async res => {
									for (var i in res.data.wmsMatInvDetailDefList) {
										if (res.data.wmsMatInvDetailDefList[i]
											.wmsInvDetailDefCode == _this.formData
											.wmsInvDetailDefCode) {
											res.data.wmsMatInvDetailDefList[i].firmNum =
												_this.formData.firmNum;
											_this.$modal.loading('提交中')
											updateInv(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>
</style>