<template>
	<view class="app-container">
		<uni-nav-bar dark :fixed="true" shadow background-color="#007AFF" status-bar :title="formData.billTypeName"
			left-icon="left" left-text="返回" @clickLeft="handleBack" right-icon="refreshempty"
			@clickRight="SendNumEmpty" />
		<view class="scan-header">
			<uni-forms-item name="remark" label="条码" :label-width="lableWidth" labelAlign="right"
				style="margin-bottom: 0px; padding: 0px 15px 0px 15px;">
				<uni-easyinput type="text" :inputBorder="true" v-model="barCode" :focus="remarkFocus" :clearable="false"
					@confirm="scanSearch"></uni-easyinput>
				<view class="icon" @click="handleScanClick">
					<uni-icons type="scan" size="20"></uni-icons>
				</view>
			</uni-forms-item>
		</view>
		<view class="cprk-content">
			<scroll-view :scroll-top="scrollTop" :show-scrollbar="true" scroll-y="true" class="scroll-Y"
				@scrolltoupper="upper" @scrolltolower="lower" @scroll="scroll">
				<view class="form-container">
					<uni-forms :rules="rules" :value="formData" ref="form" validate-trigger="submit"
						err-show-type="toast">
						<uni-forms-item name="warehouseCode" :required="true" label="仓库" :label-width="lableWidth"
							labelAlign="right">
							<uni-easyinput type="text" v-model="formData.warehouseCode" v-show="false"></uni-easyinput>
							<uni-easyinput type="text" :disabled="true" :styles="styleInput" :inputBorder="true"
								v-model="formData.warehouseName" suffixIcon="compose"
								@iconClick="setScan('WH')"></uni-easyinput>
						</uni-forms-item>
						<uni-forms-item name="taskCode" :required="true" label="任务编码" :label-width="lableWidth"
							labelAlign="right">
							<uni-easyinput type="text" :disabled="true" :styles="styleInput" :inputBorder="true"
								v-model="formData.taskCode" suffixIcon="search"
								@iconClick="searchIconClick('task')"></uni-easyinput>
						</uni-forms-item>
						<uni-forms-item name="deptCode" label="部门" :label-width="lableWidth" labelAlign="right">
							<uni-easyinput type="text" :disabled="true" :styles="styleInput" :inputBorder="true"
								v-show="false" v-model="formData.deptCode"></uni-easyinput>
							<uni-easyinput type="text" :disabled="true" :styles="styleInput" :inputBorder="true"
								v-model="formData.deptName" suffixIcon="search"
								@iconClick="searchIconClick('dept')"></uni-easyinput>
						</uni-forms-item>
						<uni-forms-item name="employeeCode" label="职员" :label-width="lableWidth" labelAlign="right">
							<uni-easyinput type="text" :disabled="true" :styles="styleInput" :inputBorder="true"
								v-model="formData.employeeCode" v-show="false"></uni-easyinput>
							<uni-easyinput type="text" :disabled="true" :styles="styleInput" :inputBorder="true"
								v-model="formData.employeeName" suffixIcon="search"
								@iconClick="searchIconClick('empl')"></uni-easyinput>
						</uni-forms-item>
						<uni-forms-item name="locationCode" :required="requiredLocation" label="货位"
							:label-width="lableWidth" labelAlign="right">
							<uni-easyinput type="text" v-model="formData.locationCode" v-show="false"></uni-easyinput>
							<uni-easyinput type="text" :disabled="true" :styles="styleInput" :inputBorder="true"
								v-model="formData.locationName" suffixIcon="compose"
								@iconClick="setScan('PT')"></uni-easyinput>
						</uni-forms-item>
						<uni-forms-item name="labelCode" :required="true" label="标签编码" :label-width="lableWidth"
							labelAlign="right">
							<uni-easyinput type="text" :disabled="true" :styles="styleInput" :inputBorder="true"
								v-model="formData.labelCode" suffixIcon="compose"
								@iconClick="setScan('L')"></uni-easyinput>
						</uni-forms-item>
						<uni-forms-item name="materialCode" label="物料" :label-width="lableWidth" labelAlign="right">
							<uni-easyinput type="text" v-model="formData.materialCode" v-show="false"></uni-easyinput>
							<uni-easyinput type="text" :disabled="true" :styles="styleInput" :inputBorder="true"
								v-model="formData.materialName"></uni-easyinput>
						</uni-forms-item>
						<uni-forms-item name="batch" label="批号" :label-width="lableWidth" labelAlign="right">
							<uni-easyinput type="text" :disabled="true" :styles="styleInput" :inputBorder="true"
								v-model="formData.batch"></uni-easyinput>
						</uni-forms-item>
						<uni-forms-item name="unitCode" label="单位" :label-width="lableWidth" labelAlign="right">
							<uni-easyinput type="text" v-model="formData.unitCode" v-show="false"></uni-easyinput>
							<uni-easyinput type="text" :disabled="true" :styles="styleInput" :inputBorder="true"
								v-model="formData.unitName"></uni-easyinput>
						</uni-forms-item>
						<uni-forms-item name="stockQty" :required="true" label="盘点数量" :label-width="lableWidth"
							labelAlign="right">
							<uni-easyinput type="number" :inputBorder="true" v-model="formData.stockQty"
								@blur="stockQtyBlur"></uni-easyinput>
						</uni-forms-item>
						<uni-forms-item name="palletCode" label="托盘" :label-width="lableWidth" labelAlign="right">
							<uni-easyinput type="text" :disabled="true" :styles="styleInput" :inputBorder="true"
								v-model="formData.palletCode" suffixIcon="search"
								@iconClick="searchIconClick('pall')"></uni-easyinput>
						</uni-forms-item>
						<uni-forms-item name="packageCode" label="包装" :label-width="lableWidth" labelAlign="right">
							<uni-easyinput type="text" :disabled="true" :styles="styleInput" :inputBorder="true"
								v-model="formData.packageCode" suffixIcon="search"
								@iconClick="searchIconClick('pack')"></uni-easyinput>
						</uni-forms-item>
					</uni-forms>
				</view>
			</scroll-view>
		</view>

		<view class="page-bottom">
			<view class="p-b-btn">
				<uni-badge class="uni-badge-left-margin" :text="mxNum" :max-num="999" absolute="rightTop"
					:offset="[-3, -3]" size="small">
					<view class="box"><text class="box-text" @click="handleMx">明细</text></view>
				</uni-badge>
			</view>
			<view class="p-b-btn" @click="$noMultipleClicks(handleSave)">
				<text class="box-text" v-show="isShowSaveButton">暂存</text>
			</view>
			<view class="p-b-btn" @click="handleAuto">
				<text class="box-text">{{autoButtonTitle}}</text>
			</view>
			<view class="p-b-btn" @click="handleBack">
				<text class="box-text">返回</text>
			</view>
		</view>

		<scan-code />
		<!-- 明细弹窗 -->
		<MxPopup ref="mxPopup" @sendNum="getSendNum" :formData="formData" @sendData="getSendData" />
		<!-- 任务编码弹窗 -->
		<RwbmPopup ref="rwbmPopup" :title="rwbmTitle" :idxFlag="rwbmFlag" :formData="formData"
			@sendData="getSendData" />
		<!-- 部门弹窗 -->
		<BmPopup ref="bmPopup" :title="bmTitle" :idxFlag="bmFlag" :formData="formData" @sendData="getSendData" />
		<!-- 职员 -->
		<ZyPopup ref="zyPopup" :title="zyTitle" :idxFlag="zyFlag" :formData="formData" @sendData="getSendData" />
		<!-- 托盘 -->
		<TpPopup ref="tpPopup" :title="tpTitle" :idxFlag="tpFlag" :formData="formData" @sendData="getSendData" />
		<!-- 包装 -->
		<BzPopup ref="bzPopup" :title="bzTitle" :idxFlag="bzFlag" :formData="formData" @sendData="getSendData" />

	</view>
</template>

<script>
	import scanCode from "@/components/scan/scan.vue";
	import RwbmPopup from './popup/commonpopup.vue' // 任务编码
	import BmPopup from './popup/commonpopup.vue' // 部门
	import ZyPopup from './popup/commonpopup.vue' // 职业
	import TpPopup from './popup/commonpopup.vue' // 托盘
	import BzPopup from './popup/commonpopup.vue' // 包装

	import MxPopup from './popup/mxPopup.vue'

	import {
		addstockCheck,
		listRwbm,
		listWarehouse,
		listLocation,
		listLabelQueryParams,
		listPallet,
		listPackage
	} from '@/api/wms/stockCheck.js'
	import { nanoid } from 'nanoid'; //唯一值 nanoid

	export default {
		components: {
			scanCode,
			RwbmPopup,
			BmPopup,
			MxPopup,
			ZyPopup,
			TpPopup,
			BzPopup
		},
		props: {},
		async onLoad(option) {
			//下面请求如果失败改为false
			let isRequest = true;

			//请求写这里

			if (isRequest == false) {
				uni.showModal({
					title: '获取单据信息失败',
					content: '请返回菜单重新进入',
					showCancel: false, //不显示取消按钮
					success: function(res) {
						if (res.confirm) {
							uni.navigateBack();
						}
					}
				})
			} else {
				this.isRequest = true
			}
		},
		onUnload() {
			uni.removeStorageSync('stock_key');
		},
		onBackPress(options) {
			if (options.from === 'navigateBack') {
				// 来自于导航条返回按钮或者系统返回按钮的事件
				return false;
			}
			// 双击返回的逻辑
			const now = Date.now();
			if (this.lastBackPress && now - this.lastBackPress < 1000) {
				// 如果两次点击的间隔小于1000毫秒，则可以认为是双击
				//这时退出
				return false;
			} else {
				this.lastBackPress = now;
				uni.showToast({
					title: '再按一次退出',
					icon: 'none'
				});
				setTimeout(() => {
					this.lastBackPress = null;
				}, 1000);
				return true;
			}
		},
		data() {
			return {
				isRequest: false, //onLoad请求成功时改为true，否则不允许保存
				remarkFocus: false,
				mxNum: 0, //明细按钮数量
				isShowSaveButton: true, //是否显示保存按钮
				autoButtonTitle: '手动',
				scrollTop: 0,
				old: {
					scrollTop: 0
				},
				barCode: '', //条码
				formData: {
					status: "1",
					// 单据类型
					billTypeCode: "2", // 单据类型
					billTypeName: "标签盘点", // 单据类型的名称（显示使用）
					// 任务编码
					taskCode: "", // 任务编码
					taskCheckId: "",
					// 部门
					deptCode: "", //部门
					deptName: "", //部门名称
					// 职员
					employeeCode: "", // 职员
					employeeName: "", // 职员名称
					//预留
					extendCode01: "",
					extendCode02: "",
					extendCode03: "",
					extendCode04: "",
					extendCode05: "",
					// 组id
					appGroupId: "",
					// ===== 以下 保存时转化为 wmsStockChecksList=====
					// 仓库
					warehouseCode: "", // 仓库
					warehouseName: "", // 仓库名称
					// 货位
					locationCode: "", // 货位编码
					locationName: "", // 货位名称 
					// 标签
					labelCode: "", // 标签编码
					// 物料
					materialCode: "", // 物料编码
					materialName: "", //物料名称
					// 批号
					batch: "", //批号
					// 托盘
					palletCode: "", // 托盘编码
					palletName: "", // 托盘名称
					// 包装
					packageCode: "", // 包装编码
					packageName: "", // 包装名称
					//单位
					unitCode: "", //单位编码
					unitName: "", //单位名称

					stockQty: "", // 盘点数量
					batch1: "", //批号1
					batch2: "", //批号2
					materialSpecification: "", //物料规格
				},

				requiredLocation: true,
				show: false,
				lableWidth: '80px',
				styleInput: {
					color: '#333',
					backgroundColor: '#fff',
					disableColor: '#fff',
					borderColor: '#e5e5e5',
				},

				// 任务编码
				rwbmTitle: "任务",
				rwbmFlag: "rwbm",
				// 职员
				zyTitle: "职员",
				zyFlag: "zy",
				// 部门
				bmTitle: "部门",
				bmFlag: "bm",
				// 收发类别
				sflbTitle: "收发类别",
				sflbFlag: "sflb",
				// 供应商
				gysTitle: "供应商",
				gysFlag: "gys",
				// 托盘
				tpTitle: "托盘",
				tpFlag: "tp",
				// 包装
				bzTitle: "包装",
				bzFlag: "bz",
				//校验
				rules: {
					warehouseName: {
						rules: [{
							required: true,
							errorMessage: '仓库不能为空'
						}],
					},
					labelCode: {
						rules: [{
							required: true,
							errorMessage: '标签不能为空'
						}],
					},
					stockQty: {
						rules: [{
							required: true,
							errorMessage: '盘点数量不能为空'
						}],
					},
				},
				//配合$noMultipleClicks防止重复点击
				noClick:true,
			}
		},
		methods: {
			upper: function(e) {},
			lower: function(e) {},
			scroll: function(e) {
				this.old.scrollTop = e.detail.scrollTop
			},
			//获取单据类型等信息时，重复尝试获取
			//requestFunction:api函数，requestParams:参数，maxRetries:重试次数
			async retryRequest(requestFunction, requestParams, maxRetries = 1) {
				let retriesLeft = maxRetries;
				while (retriesLeft > 0) {
					try {
						// 调用请求函数并传入参数
						const response = await requestFunction(requestParams);
						if (response && response.code === 200) {
							return response; // 请求成功，返回响应
						} else {
							// 请求失败，但还有重试机会
							//console.warn(`请求失败，正在尝试第 ${maxRetries - retriesLeft + 1} 次重试...`);
							retriesLeft--;
						}
					} catch (error) {
						// 请求发生异常，但还有重试机会
						//console.error('请求发生异常，正在尝试重试...', error);
						retriesLeft--;
					}
				}
				// 所有重试都失败了，抛出错误
				throw new Error('获取信息失败');
			},
			//安卓扫码
			onShow() {
				let that = this;
				uni.$off("scancode"); // 每次进来先 移除全局自定义事件监听器
				uni.$on("scancode", (data) => {
					// 扫描PCS码
					if (data && data !== '') {
						that.barCode = data;
						this.getDataByRemark(that.barCode);
					} else {
						uni.showToast({
							title: '扫码失败!',
							icon: 'none',
							duration: 2000
						});
					}
				});
			},
			// 条码右侧扫码按钮事件
			handleScanClick: function() {
				let that = this;
				uni.scanCode({
					success: function(res) {
						// {"scanType":"QR_CODE","path":"","charSet":"ISO8859_1","result":"PT|04-A-1-1","errMsg":"scanCode:ok"}
						if (res.errMsg == "scanCode:ok") {
							that.barCode = res.result;
							this.getDataByRemark(that.barCode);
						} else {
							uni.showToast({
								title: '扫码失败!',
								icon: 'none',
								duration: 2000
							});
						}

					},
					fail: (err) => {
						//扫码失败
					},
					complete: () => {
						//扫码结束
					}
				});
			},
			// 根据扫码数据查询其他formItem数据
			getDataByRemark(remark) {
				let that = this;
				remark = remark.replace(/^\s+|\s+$/g, '');
				let splitPre = remark.split("|")[0];
				let splitLast = remark.slice(splitPre.length + 1);
				// 条码空 不执行
				if (splitLast == "") {
					return true;
				}
				// 扫码
				switch (splitPre) {
					case "PT": // 货位 PT|04-A-1-1
						this.getScanHwList(splitLast);
						break;
					case "WH": // 仓库 WH|01
						this.getScanCkList(splitLast);
						break;
					case "RW": //  任务扫码 RW|
						this.getScanRwList(splitLast);
						break;
					case "L": // 标签 L|L2311060002 
						this.getScanBqList(splitLast);
						break;
					case "P": // 托盘 P|0002
						this.getScanTpList(splitLast);
						break;
					case "PA": // 包装 PA|0002
						this.getScanBzList(splitLast);
						break;
					default:
						uni.showToast({
							title: "条码不合法",
							icon: "none"
						})
						break;
				}
			},
			// 条码框 回车
			scanSearch(remark) {
				this.getDataByRemark(remark);
			},
			getSendData(res) { //基础资料
				let cFlag = res.currentFlag;
				switch (cFlag) {
					case "rwbm": // 任务编码
						this.formData.taskCode = res.selectData.taskCode;
						this.formData.taskCheckId = res.selectData.id;
						// 赋值部门
						this.formData.deptCode = res.selectData.deptCode;
						this.formData.deptName = res.selectData.deptName;
						// 赋值职员
						this.formData.employeeCode = res.selectData.employeeCode;
						this.formData.employeeName = res.selectData.employeeName;
						break;
					case "bm": // 部门
						// this.formData.deptCode = res.selectData.deptCode;
						this.$set(this.formData, "deptCode", res.selectData.deptCode);
						this.$set(this.formData, "deptName", res.selectData.deptName);
						// this.formData.deptCode = res.selectData.deptCode;
						// this.formData.deptName = res.selectData.deptName;

						// 职员清空
						this.formData.employeeCode = "";
						this.formData.employeeName = "";
						break;
					case "zy": // 职员
						this.formData.employeeCode = res.selectData.employeeCode;
						this.formData.employeeName = res.selectData.employeeName;
						break;
					case "bz": // 包装
						this.formData.packageCode = res.selectData.packageCode;
						this.formData.packageName = res.selectData.packageName;
						if(res.selectData.warehouseCode){
							this.formData.warehouseCode = res.selectData.warehouseCode;
							this.formData.warehouseName = res.selectData.warehouseName;
						}
						if(res.selectData.locationCode){
							this.formData.locationCode = res.selectData.locationCode;
							this.formData.locationName = res.selectData.locationName;
						}
						if(res.selectData.palletCode){
							this.formData.palletCode = res.selectData.palletCode;
							this.formData.palletName = res.selectData.palletName;
						}
						break;
					case "tp": // 托盘
						this.formData.palletCode = res.selectData.palletCode;
						this.formData.palletName = res.selectData.palletName;
						if(res.selectData.warehouseCode){
							this.formData.warehouseCode = res.selectData.warehouseCode;
							this.formData.warehouseName = res.selectData.warehouseName;
						}
						if(res.selectData.locationCode){
							this.formData.locationCode = res.selectData.locationCode;
							this.formData.locationName = res.selectData.locationName;
						}
						this.formData.packageCode = '';
						this.formData.packageName = '';
						break;
					case "stock": // 其它单据
						this.formData = {
							billTypeCode: "2",
							billTypeName: "标签盘点",
							taskCode: res.selectData.taskCode,
							taskCheckId: res.selectData.taskCheckId,
							deptCode: res.selectData.deptCode,
							deptName: res.selectData.deptName,
							employeeCode: res.selectData.employeeCode,
							employeeName: res.selectData.employeeName,
							appGroupId: res.selectData.appGroupId,
							extendCode01: "",
							extendCode02: "",
							extendCode03: "",
							extendCode04: "",
							extendCode05: "",
							// ===== 以下 保存时转化为 wmsStockChecksList=====
							locationCode: "",
							locationName: "",
							labelCode: "",
							materialCode: "",
							materialName: "",
							batch: "",
							palletCode: "",
							palletName: "",
							packageCode: "",
							packageName: "",
							unitCode: "",
							unitName: "",
							stockQty: "",
							batch1: "",
							batch2: "",
							materialSpecification: "",
						};
						break;
					default:
						break;
				}
			},
			searchIconClick(type) { // 放大镜点击
				switch (type) {
					case "task": // 任务编码
						this.$refs.rwbmPopup.showPopup();
						break;
					case "dept": // 部门
						this.$refs.bmPopup.showPopup();
						break;
					case "empl": // 职员
						this.$refs.zyPopup.showPopup();
						break;
					case "pall": // 托盘
						this.$refs.tpPopup.showPopup();
						break;
					case "pack": // 包装
						this.$refs.bzPopup.showPopup();
						break;
					default:
						break;
				}
			},
			remarkGetFocus() {
				this.remarkFocus = false;
				this.$nextTick(() => {
					this.remarkFocus = true
				})

			},
			setScan(str) { //赋值条码框 光标聚焦
				this.barCode = str + "|";
				this.remarkGetFocus();
			},
			stockQtyBlur(value) {
				// 校验数量
			},
			// 下面工具栏 明细点击 事件
			handleMx() {
				this.$refs.mxPopup.showDrawer();
			},
			handleSave() {
				//获取信息失败时不可以保存
				if (this.isRequest == false) {
					uni.showToast({
						title: '获取单据信息失败,请返回菜单重新进入',
						icon: "none"
					})
					return true;
				}
				if (this.isShowSaveButton) {
					uni.showToast({
						title: '保存中，请稍等',
						icon: 'none',
						duration:2000
					})
				}
				//如果唯一值空，生成唯一值
				if(this.formData.appGroupId=='' || this.formData.appGroupId==null){
					this.formData.appGroupId = nanoid(19)
				}

				let that = this;
				that.$refs.form.validate().then(async res => {
					let that = this;
					let wmsStockChecksArr = [];
					let tmpWms = {
						warehouseCode: that.formData.warehouseCode, // 仓库
						warehouseName: that.formData.warehouseName, // 仓库名称
						locationCode: that.formData.locationCode, // 货位编码
						locationName: that.formData.locationName, // 货位名称
						labelCode: that.formData.labelCode, // 标签编码
						materialCode: that.formData.materialCode, // 物料编码
						materialName: that.formData.materialName, //物料名称
						batch: that.formData.batch, //批号
						palletCode: that.formData.palletCode, // 托盘编码
						palletName: that.formData.palletName, // 托盘名称
						unitCode: that.formData.unitCode, //单位编码
						unitName: that.formData.unitName, // 单位名称
						stockQty: that.formData.stockQty, // 标签数量
						batch1: that.formData.batch1, //批号1
						batch2: that.formData.batch2, //批号2
						packageCode: that.formData.packageCode, // 包装编码
					};
					wmsStockChecksArr.push(tmpWms);

					let params = {
						guid: that.getHashId,
						appGroupId: that.formData.appGroupId,
						status: "1",
						stockType: that.formData.billTypeCode, // 单据类型
						taskCheckId: that.formData.taskCheckId, // 任务编号
						deptCode: that.formData.deptCode, //部门
						deptName: that.formData.deptName, //部门名称
						employeeCode: that.formData.employeeCode, // 职员
						employeeName: that.formData.employeeName, // 职员名称
						wmsStockChecksList: wmsStockChecksArr
					}

					addstockCheck(params).then(res => {
						if (res.code === 200) {
							that.mxNum++;
							// 保存单号
							if (res.msg !== "") {
								let storageData = uni.getStorageSync('stock_key');
								let stockIdList = storageData && storageData.stockIdList ? storageData.stockIdList : [];
								stockIdList.push(res.msg);

								let data = {
									"stockId": res.msg,
									"stockIdList": stockIdList
								}
								uni.setStorageSync('stock_key', data);
							}
							// 清空子表信息
							that.EmptyEntry();

							if (that.isShowSaveButton) {
								uni.showToast({
									title: '保存成功',
									icon: 'none',
									duration:2000
								})
							}
						}
					}).catch((err) => {
						uni.vibrateLong({});
					});
				}).catch((err) => {
					uni.vibrateLong({});
				});
			},
			// 任务扫码请求
			getScanRwList(barCode) {
				let that = this;
				let params = {
					keyWord: that.keyWord,
					params: {
						"barCode": barCode,
						"limit": 1
					},
				}
				listRwbm(params).then(res => {
					if (res.code === 200) {
						let data = res.rows ? res.rows[0] : {};
						if (data && Object.keys(data).length !== 0) {
							// 2.带出任务编号、职员名称、职员编码、部门名称、部门编码
							// ​ 3.放大镜选择时，关键字传参 queryParams.keyword = 关键字(无值时传空)，过滤数据
							// ​ 4.扫码时传参，queryParams.barCode = 条码(为空时扫码失败)

							// 赋值任务编号
							this.formData.taskCode = data.taskCode;
							this.formData.taskCheckId = data.id;
							// 赋值部门
							this.formData.deptCode = data.deptCode;
							this.formData.deptName = data.deptName;
							// 赋值职员
							this.formData.employeeCode = data.employeeCode;
							this.formData.employeeName = data.employeeName;
							// 赋值仓库
							this.formData.warehouseCode = data.warehouseCode;
							this.formData.warehouseName = data.warehouseName;
							// 赋值货位
							this.formData.locationCode = data.locationCode;
							this.formData.locationName = data.locationName;
							// 赋值托盘
							this.formData.palletCode = data.palletCode;
							this.formData.palletName = data.palletName;
							// 赋值包装
							this.formData.packageCode = data.packageCode;
							this.formData.packageName = data.packageName;
						} else {
							uni.showToast({
								title: "无数据",
								icon: "none"
							})
							that.formData.taskCode = "";
							that.formData.taskCheckId = "";
							that.formData.deptCode = "";
							that.formData.deptName = "";
							that.formData.employeeCode = "";
							that.formData.employeeName = "";
						}
					} else {
						uni.showToast({
							title: "无数据",
							icon: 'none',
							duration: 3000,
						})
					}
				});
			},
			//仓库扫码请求
			getScanCkList(barCode) {
				let that = this;
				let params = {
					status: "0", // (固定)
					params: {
						"barCode": barCode,
						"limit": 1
					},
				}
				// 仓库网络请求
				listWarehouse(params).then(res => {
					if (res.code === 200) {
						let data = res.rows ? res.rows[0] : {};
						if (data && Object.keys(data).length !== 0) {
							// 赋值仓库、货位
							that.formData.warehouseCode = data.warehouseCode;
							that.formData.warehouseName = data.warehouseName;
							that.formData.locationCode = "";
							that.formData.locationName = "";
							// 清空包装托盘
							that.formData.palletCode = '';
							that.formData.palletName = '';
							that.formData.packageCode = '';
							that.formData.packageName = '';
							// 是否显示货位
							let obj = {
								"field": "locationCode",
								"fieldName": "货位",
								"required": 1,
								"requiredCondition": null,
								"hidden": null,
								"hiddenCondition": null
							}
							obj.required = data.isLocation == "1" ? null : 1;
							obj.hidden = data.isLocation == "1" ? 1 : null;

							// 清空信息
							that.EmptyEntry();
						} else if (this.barCode) {
							uni.showToast({
								title: "无数据",
								icon: "none"
							})
						}
					}
				});
			},
			//货位扫码请求
			getScanHwList(barCode) {
				let that = this;

				let params = {
					status: "0", // (固定)
					params: {
						"barCode": barCode,
						"limit": 1
					},
				}
				// 货位网络请求
				listLocation(params).then(res => {
					if (res.code === 200) {
						let data = res.rows ? res.rows[0] : {};
						if (data && Object.keys(data).length !== 0) {
							if (data.warehouseCode === that.formData.warehouseCode || '' == that.formData.warehouseCode || undefined == that.formData.warehouseCode || null == that.formData.warehouseCode) { // 货位能对上仓库
								// 赋值货位、仓库
								that.formData.locationCode = data.locationCode;
								that.formData.locationName = data.locationName;
								that.formData.warehouseCode = data.warehouseCode;
								that.formData.warehouseName = data.warehouseName;
								// 清空包装托盘
								that.formData.palletCode = '';
								that.formData.palletName = '';
								that.formData.packageCode = '';
								that.formData.packageName = '';
								// 显示货位
								let obj = {
									"field": "locationCode",
									"fieldName": "货位",
									"required": 1,
									"requiredCondition": null,
									"hidden": null,
									"hiddenCondition": null
								}
								obj.required = 1;

								// 清空信息
								that.EmptyEntry();
							} else { // 货位对不上仓库
								uni.showToast({
									title: "货位不属于当前仓库",
									icon: "none",
									duration: 3000
								})
							}
						} else {
							uni.showToast({
								title: "无数据",
								icon: "none"
							})
						}
					}
				});
			},
			// 标签扫码请求
			getScanBqList(barCode) {
				let that = this;
				let params = {
					params: {
						"barCode": barCode,
						"limit": 1,
						"warehouseCode": that.formData.warehouseCode,
						"locationCode": that.formData.locationCode,
						"palletCode": that.formData.palletCode,
						"packageCode": that.formData.packageCode,
					},
				}

				listLabelQueryParams(params).then(res => {
					if (res.code === 200) {
						let data = res.rows ? res.rows[0] : {};
						if (data && Object.keys(data).length !== 0) {

							//返回数量
							that.formData.stockQty = (data.labelQty || 0); // 数量

							// 赋值标签
							that.formData.labelCode = data.labelCode;
							// 赋值物料编码，物料名称 ， 规格
							that.formData.materialName = data.materialName;
							that.formData.materialCode = data.materialCode;
							that.formData.materialSpecification = data.materialSpecification; //规格
							// 赋值批号
							that.formData.batch = data.batch;
							that.formData.batch1 = data.batch1;
							that.formData.batch2 = data.batch2;
							// 赋值单位
							that.formData.unitCode = data.unitCode;
							that.formData.unitName = data.unitName;

							//如果不显示保存按钮则自动保存
							if (!that.isShowSaveButton) {
								setTimeout(() => {
									that.handleSave();
								}, 10)
							}
						} else {
							uni.vibrateLong({});
							uni.showToast({
								title: "标签无效或已扫描",
								icon: "none"
							})
						}
					}
				}).catch((err) => {
					uni.vibrateLong({});
				});
			},
			//托盘扫码请求
			getScanTpList(barCode) {
				let that = this;
				let params = {
					status: "0", // (固定)
					locationCode: this.formData.locationCode,
					warehouseCode: this.formData.warehouseCode,
					params: {
						"barCode": barCode,
						"limit": 1
					},
				}
				// 托盘网络请求
				listPallet(params).then(res => {
					if (res.code === 200) {
						let data = res.rows ? res.rows[0] : {};
						if (data && Object.keys(data).length !== 0) {
							that.formData.palletCode = data.palletCode;
							that.formData.palletName = data.palletName;
							if(data.warehouseCode){
								that.formData.warehouseCode = data.warehouseCode;
								that.formData.warehouseName = data.warehouseName;
							}
							if(data.locationCode){
								that.formData.locationCode = data.locationCode;
								that.formData.locationName = data.locationName;
							}
							that.formData.packageCode = '';
							that.formData.packageName = '';
						} else {
							uni.showToast({
								title: "无数据",
								icon: "none"
							})
						}
					}
				});
			},
			//包装扫码请求
			getScanBzList(barCode) {
				let that = this;
				let params = {
					status: "0", // (固定)
					locationCode: this.formData.locationCode,
					warehouseCode: this.formData.warehouseCode,
					palletCode: this.formData.palletCode,
					params: {
						"barCode": barCode,
						"limit": 1
					},
				}
				// 包装网络请求
				listPackage(params).then(res => {
					if (res.code === 200) {
						let data = res.rows ? res.rows[0] : {};
						if (data && Object.keys(data).length !== 0) {
							that.formData.packageCode = data.packageCode;
							that.formData.packageName = data.packageName;
							if(data.warehouseCode){
								that.formData.warehouseCode = data.warehouseCode;
								that.formData.warehouseName = data.warehouseName;
							}
							if(data.locationCode){
								that.formData.locationCode = data.locationCode;
								that.formData.locationName = data.locationName;
							}
							if(data.palletCode){
								that.formData.palletCode = data.palletCode;
								that.formData.palletName = data.palletName;
							}
						} else {
							uni.showToast({
								title: "无数据",
								icon: "none"
							})
						}
					}
				});
			},
			handleAuto() {
				if (this.autoButtonTitle === "自动") { //手动
					this.autoButtonTitle = "手动";
					this.isShowSaveButton = true;
				} else { //自动
					this.autoButtonTitle = "自动";
					this.isShowSaveButton = false;
				}
			},
			handleBack() {
				uni.showModal({
					title: '提示',
					content: '确认返回?',
					success: function(res) {
						if (res.confirm) {
							//用户点击确定;
							uni.navigateBack();
						} else if (res.cancel) {
							//用户点击取消;
						}
					}
				});
			},
			getSendNum(num) {
				this.mxNum = num;
				if(num == 0){
					this.formData.appGroupId=''
				}
			},
			SendNumEmpty() {
				let that = this;
				if (this.mxNum > 0) {
					uni.showModal({
						title: '新单',
						content: '是否要明细归零，录入新单？',
						success: function(res) {
							if (res.confirm) {
								that.mxNum = 0;
								let data = {
									"stockId": "",
									"stockIdList": []
								}
								uni.setStorageSync('stock_key', data);
								//清空唯一值
								that.formData.appGroupId = '';
							}
						}
					});
				}
			},
			//清空任务、部门、职员、供应商
			EmptyTest() {},
			//清空子表信息
			EmptyEntry() {
				this.formData.labelCode = "";
				this.formData.materialCode = "";
				this.formData.materialName = "";
				this.formData.batch = "";
				this.formData.unitCode = "";
				this.formData.unitName = "";
				this.formData.stockQty = "";
				this.formData.batch1 = "";
				this.formData.batch2 = "";
				this.formData.materialSpecification = "";
				// this.formData.palletCode = "";
				// this.formData.palletName = "";
				// this.formData.packageCode = "";
				// this.formData.packageName = "";
			},
		},
		computed: {
			getHashId(){
				//生成哈希值
				const crypto = require('crypto');
				function hashString(str) {
					const hash = crypto.createHash('sha256');
					hash.update(str);
					return hash.digest('hex');
				}
				//null和undefined返回空
				function IFNULL(str){
					return (str==null || str==undefined) ? "" : str + "";
				}
				//使用头信息和nanoid来生成唯一值
				let groupJson = {
					appGroupId : this.formData.appGroupId,
					billTypeCode : IFNULL(this.formData.billTypeCode),
					taskCheckId : IFNULL(this.formData.taskCheckId),
					deptCode : IFNULL(this.formData.deptCode),
					employeeCode : IFNULL(this.formData.employeeCode),
					extendCode01 : IFNULL(this.formData.extendCode01),
					extendCode02 : IFNULL(this.formData.extendCode02),
					extendCode03 : IFNULL(this.formData.extendCode03),
					extendCode04 : IFNULL(this.formData.extendCode04),
					extendCode05 : IFNULL(this.formData.extendCode05),
				}
				let groupStr = JSON.stringify(groupJson)
				const hash = hashString(groupStr)
				return hash
			},
		},
	}
</script>

<style>
	@import "@/static/scss/stock.css";
</style>