<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="materialCode">
						<uni-easyinput suffixIcon="scan" @iconClick="scanBar" v-model="formData.materialCode"
							@confirm="scanBarMaterialCode" type="text" v-if="key=='1'" />
						<uni-easyinput suffixIcon="scan" @iconClick="scanBar" v-model="formData.materialCode"
							@confirm="scanBarMaterialCodePre" type="text" v-if="key=='2'" />
					</uni-forms-item>
					<uni-forms-item label="物料名称" :labelWidth='90' name="materialName">
						<uni-easyinput v-model="formData.materialName"></uni-easyinput>
					</uni-forms-item>
					<uni-forms-item label="批号" :labelWidth='90' name="batchNo" v-if="key=='1'">
						<uni-easyinput v-model="formData.batchNo" type="text" />
					</uni-forms-item>
					<uni-forms-item label="库位条码" :labelWidth='90' name="storageLocationBarcode" v-if="key=='1'">
						<uni-easyinput suffixIcon="scan" @iconClick="scanBarstorageLocationBarcode"
							@confirm="splitStlBarcode" type="text" v-model="formData.storageLocationBarcode" />
					</uni-forms-item>
					<button @click="key=='1'?queryStockInfo():queryStockTotalInfo()" type="primary"
						style="text-align: center;font-size: 18px;width: 50%;">查询</button>
				</uni-collapse-item>

				<uni-collapse-item title="快速盘点单明细" :open="true">

					<uni-swipe-action>
						<uni-swipe-action-item v-for="(item, index) in quickInvStockList" :key="index"
							:rightOptions="rightOptions" @click="(data) => clickDetail(index,data)">
							<uni-badge :text="index+1" type="primary"></uni-badge>
							<uni-forms-item label="批号" :labelWidth='90' v-if="key=='2'" style="margin-bottom: 0;">
								{{item.batchNo}}
							</uni-forms-item>
							<uni-forms-item label="新批号" :labelWidth='90' style="margin-bottom: 0;">
								<uni-easyinput suffixIcon="scan" @iconClick="scanBarstorageLocationBarcode" type="text"
									v-model="item.newBatchNo" />
							</uni-forms-item>
							<uni-forms-item label="部门编码" :labelWidth='90' style="margin-bottom: 0;">
								{{item.deptCode}}
							</uni-forms-item>
							<uni-forms-item label="物料规格" :labelWidth='90' style="margin-bottom: 0;">
								{{item.specification}}
							</uni-forms-item>
							<uni-forms-item label="物料类型" :labelWidth='90' style="margin-bottom: 0;">
								<uni-tag :text="item.typeText" type="primary"></uni-tag>
							</uni-forms-item>
							<uni-forms-item label="理论数量" :labelWidth='90' style="margin-bottom: 0;">
								{{item.theoryNumber}}
							</uni-forms-item>
							<uni-forms-item label="锁定数量" :labelWidth='90' style="margin-bottom: 0;">
								{{item.lockNumber}}
							</uni-forms-item>
							<uni-forms-item label="实际数量" :labelWidth='90' style="margin-bottom: 0;">
								<uni-easyinput type="text" v-model="item.actualNumber" />
							</uni-forms-item>
							<uni-forms-item label="四级库位" :labelWidth='90' v-if="key=='2'" style="margin-bottom: 0;">
								{{item.connectLocation}}
							</uni-forms-item>
							<uni-forms-item label="新库位条码" :labelWidth='90' name="newstorageLocationBarcode"
								style="margin-bottom: 0;">
								<uni-easyinput suffixIcon="scan" @iconClick="scanBarnewstorageLocationBarcode(index)"
									@change="newsplitStlBarcode(index)" type="text"
									v-model="item.newstorageLocationBarcode" />
							</uni-forms-item>
							<uni-forms-item label="单位" :labelWidth='90' style="margin-bottom: 0;">
								{{item.unitId}}
							</uni-forms-item>
							<uni-forms-item label="备注" :labelWidth='90' style="margin-bottom: 0;">
								<uni-easyinput type="text" v-model="item.remark" />
							</uni-forms-item>
						</uni-swipe-action-item>
					</uni-swipe-action>
					<button @click="handleAddPreInvStock" size="mini" type="primary" plain="true"
						style="text-align: center;font-size: 18px;width: 50%;margin-bottom: 10px;display: block;margin-top: 10px;"
						v-if="key=='2'">添加</button>
				</uni-collapse-item>
			</uni-forms>
		</uni-collapse>
		<u-button type="primary" @click="submit">提交</u-button>
	</view>
</template>

<script>
	import {
		listInvQuick,
		advListInvQuick,
		getInvQuick,
		delInvQuick,
		addInvQuick,
		updateInvQuick,
		listQuickInvStockInfo,
		invQuickUpdateStock,
		preInvUpdateStock,
		listPreInvStockInfo
	} from "@/api/wms/invQuick";
	import {
		listMaterial
	} from "@/api/mes/material.js";
	import {
		getDicts
	} from "@/api/system/dict/dictData.js";
	export default {
		onLoad: function(option) {
			getDicts("material_stock_type").then(res => {
				this.typeOptions = res.data.map(dict => {
					return {
						text: dict.dictLabel,
						value: dict.dictValue,
						diasble: false
					}
				});
			})
			//赋值盘点类型
			this.key = option.key
		},
		mounted() {},
		data() {
			return {
				//盘点类型
				key: null,
				typeOptions: [],
				formData: {
					storageLocationBarcode: null,
					materialCode: null,
					materialName: null,
					batchNo: null,
				},
				rightOptions: [{
					text: '删除',
					style: {
						backgroundColor: '#ff2a17'
					}
				}, ],
				quickInvStockList: [],
				rules: {
					materialCode: {
						rules: [{
							required: true,
							errorMessage: '请输入物料编码!'
						}]
					},
					materialName: {
						rules: [{
							required: true,
							errorMessage: '请输入物料名称!'
						}]
					},
				}
			}
		},
		methods: {
			deleteDetail(index) {
				this.quickInvStockList.splice(index, 1);
			},
			clickDetail(itemIndex, {
				position,
				index
			}) {
				if (index == 0) {
					this.deleteDetail(itemIndex);
				}
			},
			newsplitStlBarcode(i) {
				this.quickInvStockList[i].whCode = null;
				this.quickInvStockList[i].areaCode = null;
				this.quickInvStockList[i].shelvesCode = null;
				this.quickInvStockList[i].storageLocationCode = null;
				let array = this.quickInvStockList[i].newstorageLocationBarcode.split('-');
				console.log(array);
				this.quickInvStockList[i].newWhCode = array.length > 0 ? array[0] : null;
				this.quickInvStockList[i].newAreaCode = array.length > 1 ? array[1] : null;
				this.quickInvStockList[i].newShelvesCode = array.length > 2 ? array[2] : null;
				this.quickInvStockList[i].newStorageLocationCode = array.length > 3 ? array[3] : null;
				if (array.length > 4) this.quickInvStockList[i].newStorageLocationCode = array[3] + '-' + array[4];
			},
			scanBarnewstorageLocationBarcode(i) {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.$set(_this.quickInvStockList[i], "newstorageLocationBarcode", res
							.result);
						// _this.wmsLotNoList[i].storageLocationBarcode = res.result;
						_this.newsplitStlBarcode(i);
					}
				});
			},
			splitStlBarcode(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.queryStockInfo();
						break;
					case 2:
						this.formData.whCode = array[0];
						this.formData.areaCode = array[1];
						this.queryStockInfo();
						break;
					case 3:
						this.formData.whCode = array[0];
						this.formData.areaCode = array[1];
						this.formData.shelvesCode = array[2];
						this.queryStockInfo();
						break;
					case 4:
						this.formData.whCode = array[0];
						this.formData.areaCode = array[1];
						this.formData.shelvesCode = array[2];
						this.formData.storageLocationCode = array[3];
						this.queryStockInfo();
						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.queryStockInfo();
						break;
				}
			},
			scanBarstorageLocationBarcode() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.$set(_this.formData, "storageLocationBarcode", res
							.result);
						_this.splitStlBarcode(res.result);
					}
				});
			},
			scanBar() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.$set(_this.formData, "materialCode", res
							.result);
						_this.scanBarMaterialCode(res.result);
					}
				});
			},
			scanBarMaterialCode(code) {
				listMaterial({
					materialCode: code
				}).then(res => {
					console.log(res)
					if (res.total > 0) {
						console.log(res)
						this.formData.materialName = res.rows[0].materialName
					}
				})
			},
			scanBarMaterialCodePre(code) {
				console.log(1)
				listMaterial({
					materialCode: code
				}).then(res => {
					console.log(res)
					if (res.total > 0) {
						console.log(res)
						this.formData.materialName = res.rows[0].materialName
						this.queryStockTotalInfo();
					} else {
						this.formData.materialCode = null;
						this.$modal.msg("未查询到该物料，请重新输入!");
					}
				})
			},
			queryStockInfo() {
				listQuickInvStockInfo(this.formData).then(response => {
					this.quickInvStockList = response.data.map(item => {
						if (item.type) item.typeText = this.typeOptions.find(tp => tp.value == item.type)
							.text
						return item
					});
					console.log(response.data, this.quickInvStockList);
				})
			},
			queryStockTotalInfo() {
				listPreInvStockInfo(this.formData).then(response => {
					this.quickInvStockList = response.data.map(item => {
						if (item.type) item.typeText = this.typeOptions.find(tp => tp.value == item.type)
							.text
						return item
					});
				})
			},
			handleAddPreInvStock() {
				let obj = {}
				if (this.quickInvStockList.length > 0) {
					obj = {
						...this.quickInvStockList[0]
					};
				} else {
					obj = this.formData;
				}
				/**新增库存的标志*/
				obj.newStock = 1;
				//目前仅支持合格品
				obj.type = 1;
				obj.batchNo = null;
				obj.newBatchNo = null;
				obj.theoryNumber = 0;
				obj.lockNumber = 0;
				obj.actualNumber = 0;
				obj.whCode = null;
				obj.areaCode = null;
				obj.shelvesCode = null;
				obj.storageLocationCode = null;
				obj.connectLocation = null;
				obj.newWhCode = null;
				obj.newAreaCode = null;
				obj.newShelvesCode = null;
				obj.newStorageLocationCode = null;
				obj.remark = null;
				this.quickInvStockList.push(obj);
			},
			submit() {
				const _this = this;
				_this.$refs.form.validate().then(res => {
					uni.showModal({
						title: '提示',
						content: '您确定盘点该物料吗？',
						success: function(res) {
							if (res.confirm) {
								const quickInvStockList = _this.quickInvStockList;
								// console.log(quickInvStockList)

								if (_this.key == '1') {
									invQuickUpdateStock(quickInvStockList).then(() => {
										_this.$modal.msgSuccess("盘点成功!");
										setTimeout(() => {
											_this.$tab.switchTab(
												"/pages/work/index");
										}, 500);
									}).catch(() => {});
								} else if (_this.key == '2') {
									preInvUpdateStock(quickInvStockList).then(() => {
										_this.$modal.msgSuccess("盘点成功!");
										setTimeout(() => {
											_this.$tab.switchTab(
												"/pages/work/index");
										}, 500);
									}).catch(() => {});
								}
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				});
			},
		}
	}
</script>

<style>
</style>