<template>
	<view>
		<uni-easyinput class="uni-mt-5" @blur="onInput" v-model="inputCodeValue" placeholder="请扫描或输入中控编号"
			suffixIcon="scan" @iconClick="onScan">
		</uni-easyinput>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue';
	import {
		showModelMessage,
		getUrlParams
	} from '@/utils/tools.js';

	const inputCodeValue = ref('');
	const {
		codeValue
	} = defineProps(["codeValue"]);
	inputCodeValue.value = codeValue;

	const emit = defineEmits(['scan-change']);
	const onScan = () => {
		uni.scanCode({
			onlyFromCamera: true, //只能扫码
			scanType: ["qrCode"],
			success: function(res) {
				const {
					result
				} = res;
				if (!result || result.toLowerCase().indexOf("imei") == -1) {
					showModelMessage("无效的中控二维码");
					inputCodeValue.value = "";
					emit('scan-change', {
						imei: "",
						sn: ""
					});
				} else {
					const arr = result.split(" ");
					const imei = arr[0].replace("IMEI:", "");
					const sn = arr[1].replace("SN:", "");
					const params = {
						imei,
						sn
					};
					inputCodeValue.value = imei;
					emit('scan-change', params);
				}
			}
		})
	};

	const onInput = (value) => {
		if (value.detail.value == "") return;
		inputCodeValue.value = value.detail.value;
		emit('scan-change', {
			imei: value.detail.value,
			sn: ""
		});
	}

	const onSetValue = (value) => {
		inputCodeValue.value = value;
	}
	defineExpose({
		onSetValue
	});
</script>
<style>

</style>