<template>
	<uni-popup ref="popup" :maskClick='false'>
		<view class="popup_box">
			<view class="pop_title uni-flex space-between">
				<view class="" style="font-size: 35rpx;">
					扫描{{title}}
				</view>
				<view>
					<image class=" icons_scan_close" src="/static/icons/icons_scan_close.svg" @click="closeScanPopup()">
					</image>
				</view>
			</view>
			<view class="">
				<view class="">
					<win-com-scan ref="scan" labelType="LocationLabel" @getResult="getScanResult" :placeholder='title' :clearResult="false"
						:boxFocus="true" :isShowHistory="isShowHistory">
					</win-com-scan>
				</view>
			</view>
		</view>
	</uni-popup>
	<comMessage ref="comMessage"></comMessage>
</template>

<script>
	import {
		getBasicLocationByCode,
	} from '@/api/request2.js';
	import {
		getLocationTypeName,
		getListLocationTypeDesc,
		checkDirectoryItemExist
	} from '@/common/directory.js';
	import winComScan from '@/mycomponents/scan/winComScan.vue'
	import comMessage from '@/mycomponents/common/comMessage.vue'
	export default {
		components: {
			winComScan,
			comMessage
		},
		emits: ["getLocation"],
		props: {
			title: {
				type: String,
				default: ''
			},
			locationTypeList: {
				type: Array,
				default: []
			},
			isShowHistory: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				code: '',
				location: {}
			}
		},

		created() {

		},
		methods: {
			openScanPopup() {
				this.$refs.popup.open('bottom')
			},
			closeScanPopup() {
				this.$refs.popup.close()
			},
			scanClick() {
				this.$refs.scan.clickScanMsg();
			},
			cancelClick() {
				this.$refs.scan.clearScanValue();
			},
			getScanResult(result) {
				uni.showLoading({
					title: '扫描中...',
					mask: true
				});
				let label = result.label;
				if (label.barType === 'QRCode') {
					this.code = label.locationCode;
				} else if (label.barType === 'BarCode') {
					this.code = label.code;
				}
				if(this.code==undefined){
					uni.hideLoading();
					this.showErrorMessage("扫描库位[" + this.code + "]为空,请输入正确的库位")
					return 
				}
				
				getBasicLocationByCode(this.code).then(res => {
					uni.hideLoading();
					if (res.data.total > 0) {
						let result = res.data.list[0];
						var type = result.type;
						var available = result.available;
						if (available == "TRUE") {
							if (checkDirectoryItemExist(this.locationTypeList, type)) {
								this.location = result;
								this.callBack();
							} else {
								var hint = getListLocationTypeDesc(this.locationTypeList);
								this.showErrorMessage("扫描库位[" + this.code + "]是[" +
									getLocationTypeName(type) + "],需要的库位类型是[" + hint + "]")
							}
						} else {
							this.showErrorMessage("扫描库位[" + this.code + "]不可用")
						}
					} else {
						this.showErrorMessage('未查询到库位[' + this.code + ']')
					}
					
				}).catch(error => {
					uni.hideLoading();
					this.showErrorMessage(error)
				})
			},
			callBack() {
				this.closeScanPopup();
				this.$emit("getLocation", this.location, this.code);
			},
			checkLocationType(type) {
				var isPass = false;
				if (this.locationTypeList.length == 0) {
					isPass = true;
				} else {
					var temp = this.locationTypeList.filter(res => {
						if (res == type) {
							return res
						}
					})
					if (temp != undefined && temp.length > 0) {
						isPass = true
					}
				}
				return isPass
			},
			getfocus() {
				this.$refs.scan.getfocus();
			},
			losefocus() {
				this.$refs.scan.losefocus();
			},
			showErrorMessage(message) {
				setTimeout(r => {
					this.losefocus();
					this.$refs.comMessage.showErrorMessage(message, res => {
						this.code = '';
						this.getfocus();
					})
				})
			}
		}
	}
</script>

<style>
</style>
