<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"  @getResult="getScanResult" :placeholder='title' :clearResult="true"
						:boxFocus="true" :isShowHistory="isShowHistory" headerType="">
					</win-com-scan>
				</view>
			</view>
		</view>
	</uni-popup>
	<comMessage ref="comMessage"></comMessage>
</template>

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

		created() {

		},
		methods: {
			openScanPopup() {
				setTimeout(res => {
					this.$refs.popup.open('bottom')
				}, 500)
			},
			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 areaType = result.areaType
						var available = result.available;
						if (available == "TRUE") {
							if (checkDirectoryItemExist(this.locationAreaTypeList, areaType)) {
								this.location = result;
								this.callBack();
							} else {
								var hint = getListLocationAreaTypeDesc(this.locationAreaTypeList);
								this.showErrorMessage("扫描库位[" + this.code + "]是[" +
									getLocationAreaTypeName(areaType) + "],需要的库区是[" + 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.locationAreaTypeList.length == 0) {
					isPass = true;
				} else {
					var temp = this.locationAreaTypeList.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>
