<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"
						 :isShowHistory="isShowHistory" headerType="">
					</win-com-scan>
				</view>
			</view>
		</view>
	</uni-popup>
	<!-- 模拟扫描功能 -->
	<win-com-scan  v-show="false" ref="comscansimulate" @getResult="getScanResult" :clearResult="true" :boxFocus="true" :isShowHistory="false" headerType=""></win-com-scan>
	<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","clearFromLocation"],
		props: {
			title: {
				type: String,
				default: ''
			},
			locationAreaTypeList: {
				type: Array,
				default: null
			},
			isShowHistory: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				code: '',
				location: {}
			}
		},

		created() {

		},
		methods: {
			openScanPopup() {
				this.$refs.popup.open('bottom')
				setTimeout(res => {
					this.getfocus()
				}, 500)
			},
			openScanPopupSimulate(location) {
				this.$refs.comscansimulate.setItemCodeSimulate(location)
				this.$refs.comscansimulate.clickScanMsg();
			},
			closeScanPopup() {
				this.losefocus()
				if(this.$refs.popup){
					this.$refs.popup.close()
				}
				
			},
			scanClick() {
				this.$refs.scan.clickScanMsg();
			},
			cancelClick() {
				this.$refs.scan.clearScanValue();
			},
			getScanResult(result) {
				uni.showLoading({
					title: '扫描中...',
					mask: true
				});
				console.log(111,result)
				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("扫描库位为空,请输入正确的库位")
					return 
				}
				console.log(888,this.code)
				getBasicLocationByCode(this.code).then(res => {
					
					uni.hideLoading();
					if (res.data.total > 0) {
						let result = res.data.list[0];
						if(result.code!=this.code){
							this.showErrorMessage('未查询到库位[' + this.code + ']')
							this.$emit('clearFromLocation',this.code)
							return;
						}
						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 + ']')
						this.$emit('clearFromLocation',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() {
				if(this.$refs.scan){
					this.$refs.scan.getfocus();
				}
			},
			losefocus() {
				if(this.$refs.scan){
					this.$refs.scan.losefocus();
				}
			},
			showErrorMessage(message) {
				setTimeout(r => {
					this.losefocus();
					this.$refs.comMessage.showErrorMessage(message, res => {
						this.code = '';
						this.getfocus();
					})
				})
			}
		}
	}
</script>

<style>
</style>
