<template>
	<view class="containers">
		<topWindow :isHome="true"></topWindow>
		<view class="content">
			<image src="@/static/logo.png" style="width: 100px; height: 50px;" mode="aspectFit"></image>
			<view class="tipsCss">{{$t('hdk.tips')}}</view>
			<view class="flex-row">
				<b-input ref="inputField" class="inputCss" autofocus="autofocus" :placeholder="$t('hdk.placeholder')"
					@change="handleChange" @input="handleInput" v-model="SNcode"></b-input>
				<!-- <input type="text"> -->
				<b-button class="SearchBtnCss my-2 my-sm-0" size="sm" @click="search"
					style="background:#00AEAA;">{{$t('hdk.search')}}</b-button>
			</view>
			<view class="errorTipCss" v-if="isError">
				<image src="/static/images/icon/warning.png" style="width:16px;height:16px;margin: 2px;" mode="">
				</image>
				{{$t('hdk.sninexistence')}}
			</view>
			<view class="btnSNscan" @click="scanClick" v-if="$isMobile">
				<u-icon name="/static/images/icon/scan2.png"></u-icon>
				<text style="margin-left: 10rpx;">{{$t('hdk.scan')}}</text>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getProductFind
	} from '@/api/scan'
	import topWindow from "@/mobile/pages/component/topWindow.vue"
	export default {
		data() {
			return {
				systemLocale: '',
				applicationLocale: '',
				isError: false,
				SNcode: "",
				open: false,
				lastTimeLen: null
			}
		},
		components: {
			topWindow,
		},
		computed: {
			locales() {
				return [{
						text: this.$t('locale.en'),
						code: 'en'
					},
					{
						text: this.$t('locale.zh-hans'),
						code: 'zh-Hans'
					},
				]
			}
		},
		mounted() {
			// const inputElement = this.$refs.inputField.$el.querySelector('input');
			document.addEventListener('keydown', this.handleKeyDown);
		},
		onLoad() {
			let systemInfo = uni.getSystemInfoSync();
			this.systemLocale = systemInfo.language;
			this.applicationLocale = uni.getLocale();
			this.isAndroid = systemInfo.platform.toLowerCase() === 'android';
		},
		methods: {
			getFirstSnFromUrl(url) {
				const regex = /[?&]sn=([^&#]*)/i;
				const match = url.match(regex);
				if (match && match.length > 1) {
					const sn = match[1];
					return sn;
				}
				return url;
			},
			handleChange(event) {
				this.isError = false;
				this.SNcode = this.getFirstSnFromUrl(event);
				// if (sncode) {
				// 	this.SNcode = sncode;
				// 	this.lastTimeLen = eventLen; // 重置 lastTimeLen
				// } else {
				// 	if (eventLen > this.lastTimeLen) {
				// 		this.SNcode = event.slice(this.lastTimeLen, eventLen);
				// 		this.lastTimeLen = eventLen;
				// 	}
				// }
			},
			handleBarcodeInput(event) {
				// 捕获扫码枪输入的内容
				console.log(event);
				const barcodeContent = event.target.value;
				console.log(barcodeContent);
				if (key === 'Enter') {
					console.log(event);
				}

			},
			search() {
				console.log("查询");
				if (this.SNcode === "") {
					return
				}
				uni.showLoading({
					title: '查询中...'
				})
				getProductFind(this.SNcode).then(res => {
					uni.hideLoading()
					if (res.code == 0) {
						console.log("sn查询结果", res)
						const data = res.data;
						uni.navigateTo({
							url: "/mobile/pages/product/index?sn=" + this.SNcode
						})
					} else {
						this.isError = true
					}
				})
			},
			scanClick() {
				uni.navigateTo({
					url: "/mobile/pages/product/scan"
				})
			},
			getResult(res) {
				console.log(res);
			},
			geterror(e) {
				console.log(e);
			}
		}
	}
</script>

<style lang="scss" scoped>
	.containers {
		width: 100vw;
		height: 100vh;
		background: #1E5866;
	}

	.content {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		height: 80vh;
	}

	.flex-row {
		display: flex;
		justify-content: center;
		align-items: center;
	}

	//移动端样式
	@media screen and (max-width: 767px) {
		.inputCss {
			width: 278px;
			height: 44px;
		}

		.btnSNscan {
			width: 100px;
			height: 38px;
			margin: 8px;
			padding: 2px;
			display: flex;
			align-items: center;
			justify-content: center;
			border-radius: 54px;
			background-color: rgba(0, 174, 170, 0.25);
			box-sizing: border-box;
			font-family: '苹方 中等', '苹方', sans-serif;
			color: #ffffff;
			text-align: center;
		}

		.SearchBtnCss {
			width: 80px;
			height: 44px;
			border-radius: 4px;
			background-color: #00aeaa;
			box-sizing: border-box;
			font-family: '苹方 中等', '苹方', sans-serif;
			color: #ffffff;
			text-align: center;
		}
	}

	//PC端样式
	@media screen and (min-width: 768px) {
		.inputCss {
			width: 566px;
			height: 44px;
		}

		.btnSNscan {
			width: 130px;
			height: 44px;
			margin: 20px 0 0 0;
			padding: 2px;
			display: flex;
			justify-content: center;
			align-items: center;
			border-radius: 54px;
			background-color: rgba(0, 174, 170, 0.25);
			box-sizing: border-box;
			font-family: '苹方 中等', '苹方', sans-serif;
			color: #ffffff;
			text-align: center;
		}

		.SearchBtnCss {
			width: 80px;
			height: 44px;
			border-radius: 4px;
			background-color: #00aeaa;
			box-sizing: border-box;
			font-family: '苹方 中等', '苹方', sans-serif;
			color: #ffffff;
			text-align: center;
		}
	}

	.tipsCss {
		margin: 16px 0 22px 0;
		font-size: 16px;
		color: #ffffff;
	}

	.errorTipCss {
		display: flex;
		justify-content: center;
		align-items: center;
		margin-top: 16px;
		font-size: 16px;
		color: red;
	}
</style>