<template>
	<view class="back">
		<!-- 搜索 -->
		<view class="u-flex u-row-between">
			<view class="" style="width: 622rpx;">
				<u-search  @focus='focus' placeholder="请输入物品名称" v-model="keyword"
					:showAction='false'></u-search>
			</view>
			<text @click="clear" v-if="keyword==''">{{i18n.Cancel}}</text>
			<text @click="enter" v-else>{{i18n.enter}}</text>
		</view>
		<view v-if="blurshow==false && keyword==''" class="title" style="margin-top: 32rpx;">
			{{i18n.Popularshipment}}
		</view>
		<view v-if="blurshow==false && keyword==''" class="u-flex " style="column-gap: 20rpx;flex-wrap: wrap;">
			<view class="tabs" v-for="(item,idx) in record" :key="idx" @click="select(item)">
				{{item}}
			</view>
		</view>
		<scroll-view v-if="blurshow" :scroll-y='true'>
			<view @click="changeitem(item)" class="searchitem" v-for="(item,index) in goods">
				<text v-if="language =='zh-CN'">{{item.name_cn}}</text>
				<text v-if="language =='en-US'">{{item.name_en}}</text>
				<text v-if="language =='es-ES'">{{item.name_es}}</text>
				<text v-if="language =='it-IT'">{{item.name_ita}}</text>
			</view>
		</scroll-view>
	</view>
</template>

<script>
import { vShow } from 'vue';
	export default {
		data() {
			return {
				keyword: '',
				blurshow: false, //是否显示热门寄件
				language: 'zh-CN',
				goods: [], //物品信息
				record: [], //历史记录
			};
		},
		computed: {
			i18n() {
				return this.$t('index')
			}
		},
		onLoad() {
			this.getgoods()
		},
		onShow() {
			this.record = uni.getStorageSync('record')? uni.getStorageSync('record').reverse() : []
			if (uni.getStorageSync('language') != '') {
				this.language = uni.getStorageSync('language')
			}
		},
		methods: {
			//选中标签
			changeitem(item) {
				// console.log(item);
				const eventchannel = this.getOpenerEventChannel();
				eventchannel.emit('searchinfo', item)
				uni.navigateBack()
			},
			//获取物品信息列表
			getgoods() {
				uni.$u.http.get('/api/express-goods-info', {
					params: {
						// is_hot: 0,
						name: this.keyword
					}
				}).then((res) => {
					this.goods = res
				}).catch(() => {

				})
			},
			//取消
			clear() {
				uni.navigateBack()
			},
			enter() {
				console.log(this.record);
				this.record.push(this.keyword)
				
				uni.setStorageSync('record',this.record)
				this.getgoods()
			},
			search() {

			},
			// // 失去焦点
			// blur() {
			// 	this.blurshow = false
			// },
			//获取焦点
			focus() {
				this.blurshow = true
			},
			//选中
			select(name) {
				this.keyword = name
				this.blurshow = true
				this.getgoods()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.searchitem {
		height: 98rpx;
		border-bottom: 2rpx solid rgba(151, 151, 151, 0.1);
		line-height: 98rpx;
	}

	.title {
		font-family: PingFangSC, PingFang SC;
		font-weight: 500;
		font-size: 28rpx;
		color: #222222;
		line-height: 40rpx;
		text-align: left;
		font-style: normal;
	}

	.tabs {
		height: 58rpx;
		background: #F5F5F5;
		border-radius: 36rpx;
		font-family: PingFangSC, PingFang SC;
		font-weight: 400;
		font-size: 24rpx;
		color: #333333;
		// line-height: 58rpx;
		text-align: center;
		font-style: normal;
		display: inline;
		padding: 12rpx 24rpx;
		box-sizing: border-box;
		margin-top: 24rpx;
	}

	.back {
		padding: 28rpx 20rpx !important;
		background-color: #fff;
		min-height: calc(100vh - 44rpx);
	}
</style>