<!--扫描组件-->
<template>
	<page-meta root-font-size="18px"></page-meta>
	<view>
		<textarea v-model="scanMsg" :focus="boxfocus" trim="all" @keydown.enter="handelScanMsg" @focus="getfocus"
			maxlength="1000" :placeholder="placeholderValue" @blur="blurfocus"></textarea>
	</view>
</template>

<script>
	import {
		analyseScanInfo
	} from '@/common/scan.js';
	export default {
		name: "winComScan",
		props: {
			placeholder: {
				type: String,
				default: '箱标签'
			},
			clearResult: {
				type: Boolean,
				default: true
			},
			boxFocus: {
				type: Boolean,
				default: true
			},
		},
		data() {
			return {
				scanMsg: "",
				items: null,
				boxfocus: this.boxFocus,
				placeholderValue: ''
			}
		},
		watch: {
			placeholder() {
				this.placeholderValue = '请扫描' + this.placeholder;
			}
		},
		created() {
			this.placeholderValue = '请扫描' + this.placeholder;
		},
		methods: {
			setValue(val) {
				this.scanMsg = val
			},
			iconclick(type) {
				console.log(type)
			},
			confirm() {
				this.handelScanMsg();
			},
			handelScanMsg() {
				let that = this;
				if (that.scanMsg == "")
					return;
				setTimeout(() => {
					let content = that.scanMsg.replace(/[\r\n]/g, "");
					let scanResult = analyseScanInfo(content);
					if (scanResult.sucess) {
						that.boxfocus = true;
						that.$emit("getScanResult", scanResult);
						if (that.clearResult) {
							that.clear();
						}
					} else {
						uni.showToast({
							title: scanResult.message,
							icon: 'error',
							duration: 2000
						})
					}
				}, 100)
			},
			getfocus() {
				this.boxfocus = true;
				this.$nextTick(r => {
					this.boxfocus = true;
				});
			},
			blurfocus() {
				this.boxfocus = false;
			},
			clear() {
				let that = this;
				that.scanMsg = ''
				// this.$refs.scanInput.onClear(); //删除input的内容
				that.boxfocus = true;
			},
			iconClick(type) {
				this.$emit("clearResult", this.scanMsgk);
			},
			getValue() {
				return this.scanMsg
			}
		}
	}
</script>
<style scoped lang="scss">

</style>
