<template>
	<view class="input-box">
		<input :adjust-position="false" :focus="isFocus" type="text" v-model.trim="search" @confirm="getInfo" @blur="handlerBlur" />
		<!-- <input type="text" v-model.trim="search" @confirm="getInfo($event)" /> -->
	</view>
</template>

<script>
	import * as WarehoseApi from '@/api/warehouse/warehose.js';
	export default {
		name: 'pda-scan-input',
		props: {
			hasFocus: {
				default: true,
				type: Boolean
			},
			type: {
				// 1-仓库扫码 2-成品满箱扫码 3-扫出库单 4-退库
				default: 1,
				type: Number
			},
			warehouseInfo: {
				default: null
			}
		},
		data() {
			return {
				isFocus: true,
				// search: 'A1-01',
				search: '',
				time: null
			};
		},
		watch: {
			isFocus(n) {}
		},
		mounted() {
			this.init();
		},
		beforeDestroy() {
			if (this.time) {
				clearInterval(this.time);
				this.time = null;
			}
		},
		methods: {
			// 初始化
			init() {
				// this.time = setInterval(() => {
				// 	if (!this.isFocus) {
				// 		this.$nextTick(function () {
				// 			this.isFocus = false;
				// 		});
				// 	}
				// }, 60);
			},
			// 传输数据
			emitInputChange(data, type) {
				this.$emit('getInfo', data, type);
			},
			handlerBlur() {
				if (this.hasFocus) {
					this.isFocus = false;
					setTimeout(() => {
						this.$nextTick(function() {
							this.isFocus = true;
						});
					}, 1500);
				}
			},
			// 获取扫码信息
			async getInfo(e) {
				const text = e.target.value;
				const type = this.type;
				setTimeout(() => {
					this.$nextTick(function() {
						this.search = '';
					});
				}, 500);
				if (type === 1) {
					this.handleScanWareHouseCode(text);
				} else if (type === 2) {
					this.handleScanGoodsCode(text);
				} else if (type === 3) {
					this.handleScanDeliveryOrderCode(text);
				} else if (type === 4) {
					this.handleScanStockReturnCode(text);
				}
			},
			// type = 1 入库扫仓库编码
			async handleScanWareHouseCode(text) {
				// 是否为库位码
				const locationCheckData = {
					production_location_code: text
				};
				const checkRes = await WarehoseApi.isProductionLocation(locationCheckData);
				if (checkRes.code !== 200 || !checkRes.data) {
					uni.showModal({
						title: '提示',
						content: '仓库编号异常！',
						showCancel: false,
						confirmText: '确定'
					});
					return;
				}
				const getProductLocationData = {
					locationcode: locationCheckData.production_location_code,
					warehouse_num: 1
				};
				const WarehoseRes = await WarehoseApi.getProductLocationInfo(getProductLocationData);
				if (WarehoseRes.code !== 200 || !WarehoseRes.data) {
					uni.showModal({
						title: '提示',
						content: '获取仓库信息异常！',
						showCancel: false,
						confirmText: '确定'
					});
					return;
				}
				// 待返回的结果数据
				let emitData = {
					warehoseInfo: WarehoseRes.data
				};
				this.emitInputChange(emitData, 1);
			},
			// type = 2 入库扫货物编码
			async handleScanGoodsCode(text) {
				let package_code = text;
				let resolutionData = {
					code: package_code
				};
				const emitRes = await WarehoseApi.resolutionPackage(resolutionData);
				if (emitRes.code !== 200 || emitRes.data === null) {
					uni.showModal({
						title: '提示',
						content: '该箱号数据解析异常！',
						showCancel: false,
						confirmText: '确定'
					});
					return;
				}
				this.emitInputChange(emitRes.data, 2);
				return;
				// 临时库处理
				// let _warehouseInfo = this.warehouseInfo;
				// if (_warehouseInfo != null && _warehouseInfo != undefined) {
				// 	if (_warehouseInfo.remark === '临时' || _warehouseInfo.locaiton === 'LS1-01') {
				// 		let resolutionData = {
				// 			code: package_code
				// 		};
				// 		const emitRes = await WarehoseApi.resolutionPackage(resolutionData);
				// 		if (emitRes.code !== 200 || emitRes.data === null) {
				// 			uni.showModal({
				// 				title: '提示',
				// 				content: '该箱号数据解析异常！',
				// 				showCancel: false,
				// 				confirmText: '确定'
				// 			});
				// 			return;
				// 		}
				// 		this.emitInputChange(emitRes.data, 2);
				// 		return;
				// 	}
				// }
				// 成品库处理
				// 成品满箱扫码
				let locationCheckData = {
					package_code
				};
				// 判断是否是箱号
				const checkRes = await WarehoseApi.isProductionPackage(locationCheckData);
				console.log(checkRes);
				if (checkRes.code !== 200) {
					uni.showModal({
						title: '提示',
						content: '成品箱号异常！',
						showCancel: false,
						confirmText: '确定'
					});
					return;
				}
				if (checkRes.data === 0) {
					uni.showModal({
						title: '提示',
						content: checkRes.msg,
						showCancel: false,
						confirmText: '确定'
					});
					return;
				} else if (checkRes.data === 2) {
					uni.showModal({
						title: '提示',
						content: checkRes.msg,
						showCancel: false,
						confirmText: '确定'
					});
					return;
				} else if (checkRes.data === 1) {
					// 判断是否为满箱
					// const isFullRes = await WarehoseApi.isFullPackage(locationCheckData);
					// if (isFullRes.code !== 200 || !isFullRes.data) {
					// 	uni.showModal({
					// 		title: '提示',
					// 		content: '该标签不为满箱标签！',
					// 		showCancel: false,
					// 		confirmText: '确定'
					// 	});
					// 	return;
					// }
					let resolutionData = {
						code: package_code
					};
					const emitRes = await WarehoseApi.resolutionPackage(resolutionData);
					if (emitRes.code !== 200 || emitRes.data === null) {
						uni.showModal({
							title: '提示',
							content: '该箱号数据解析异常！',
							showCancel: false,
							confirmText: '确定'
						});
						return;
					}
					this.emitInputChange(emitRes.data, 2);
				}
			},
			// type = 3 扫出货单
			async handleScanDeliveryOrderCode(text) {
				let id = text;
				const getWmOutOrderRes = await WarehoseApi.getWmOutOrder(id);
				if (getWmOutOrderRes.code != 200) {
					uni.showModal({
						title: '提示',
						content: '出库单号异常！',
						showCancel: false,
						confirmText: '确定'
					});
					return;
				}
				this.emitInputChange(getWmOutOrderRes.data, 3);
			},
			// type = 4 退货扫货物编码
			async handleScanStockReturnCode(text) {
				const checkData = {
					originalCode: text
				};
				// 判断是否仓库中有记录
				const checkRes = await WarehoseApi.isExistedWarehouse(checkData);
				if (!checkRes.data) {
					uni.showModal({
						title: '提示',
						content: '该编码不在仓库记录中:' + checkRes.msg,
						showCancel: false,
						confirmText: '确定'
					});
					return;
				}
				const resolutionData = {
					code: text
				};
				const emitRes = await WarehoseApi.resolutionPackage(resolutionData);
				if (emitRes.code !== 200 || emitRes.data === null) {
					uni.showModal({
						title: '提示',
						content: '该箱号数据解析异常！',
						showCancel: false,
						confirmText: '确定'
					});
					return;
				}
				this.emitInputChange(emitRes.data, 4);
			}
		}
	};
</script>

<style scoped>
	.input-box {
		background-color: #e6e6e6;
		/* 	width: 100%;
	position: absolute;
	bottom: 0; */
		/* height: 0px; */
		/* opacity: 0; */
		/* visibility: hidden; */
		/* display:none; */
	}
</style>