<template>
	<uni-popup type="bottom" ref="unipopup">
		<view class="popup" :style="{height: screen.height + 'px'}">
			<view :style="{height: screen.statusHeight + 'px'}"></view>
			<view class="popup-head">
				<view class="popup-head-left" @click="bankClick">
					<x-icon class="popup-head-left-icon" name="icon-black" size="18" color="#000"></x-icon>
				</view>
				<view class="popup-head-center">
					<x-icon class="popup-head-center-icon" name="icon-search" size="18" color="#6B6B6E"></x-icon>
					<input class="popup-head-center-input" v-model="value" placeholder="请输入搜索内容" />
				</view>
				<text class="popup-head-right" @click="searchClick">搜 索</text>
			</view>
			
			<view class="popup-hot" v-if="searchlist &&searchlist.length > 0">
				<view class="popup-hot-title">
					<text class="popup-hot-title-text">搜索历史</text>
					<x-icon @click.native="clearClick" class="popup-hot-title-icon" name="icon-deletes" size="18" color="#858688"></x-icon>
				</view>
				<view class="popup-hot-ul">
					<text v-for="(item, index) in searchlist" :key="index" @click="hisClick(item, index)" :class="{'popup-hot-liact': hisIndex == index}" class="popup-hot-li">{{item.name}}</text>
				</view>
			</view>
		</view>
	</uni-popup>
</template>

<script>
	import api from '@/service/api.js'
	import minx from '@/service/mixins/index.js'
	export default {
		mixins: [minx],
		props: {
			widget: {},
			type: {}
		},
		data() {
			return {
				searchlist: [], // 存储搜索历史记录
				data:['女生节', '奶茶店'],
				value: '',
				hisIndex: -1
			}
		},
		methods: {
			async open() {
				this.searchlist = await api.searchhistoryList(this.type)
				this.value = ''
				this.$refs.unipopup.open()
			},
			close() {
				this.$refs.unipopup.close()
			},
			bankClick() {
				this.close()
			},
			searchClick() {
				this.$emit('searchClick', {
					data: this.widget,
					value: this.value
				})
				let find = this.searchlist.find( r => r.name === this.value)
				if (!find) {
					api.searchhistoryAdd(this.type, this.value)
				}
				this.close()
			},
			hisClick(item, index) {
				this.hisIndex = index
				this.value = item.name
				this.searchClick()
			},
			clearClick() {
				api.searchhistoryClear(this.type)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.popup {
		background-color: #FFFFFF;
		&-head{
			height: 44px;
			background-color: #FFFFFF;
			padding: 0 30rpx;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;
			&-left{
				width: 18px;
				height: 18px;
				line-height: 18px;
				margin-right: 20rpx;
			}
			&-center{
				flex: 1;
				height: 32px;
				padding: 0 30rpx;
				flex-direction: row;
				align-items: center;
				background-color: #F1F1F1;
				border-radius: 30rpx;
				&-icon{
					width: 18px;
					height: 18px;
					line-height: 18px;
				}
				&-input{
					flex: 1;
					height: 25px;
					margin-left: 20rpx;
					@include styleFont(#343434, 28rpx);
				}
			}
			&-right{
				margin-left: 20rpx;
				height: 56rpx;
				line-height: 56rpx;
				width: 120rpx;
				border-radius: 10rpx;
				background-color: $gl_background;
				text-align: center;
				@include styleFont(#fff, 28rpx);
			}
		}
		&-hot{
			padding: 30rpx;
			&-title {
				flex-direction: row;
				align-items: center;
				justify-content: space-between;
				margin-bottom: 30rpx;
				&-text{
					@include styleFont(#343434, 32rpx, 1);
				}
				&-clear{
					@include styleFont(#343434, 32rpx, 1);
				}
				&-icon{
					height: 18px;
					width: 18px;
					line-height: 18px;
				}
			}
			&-ul {
				flex-direction: row;
				flex-wrap: wrap;
			}
			&-li{
				@include styleFont(#2C2D2F, 28rpx);
				padding: 15rpx 30rpx;
				background-color: #F5F5F5;
				border-radius: 40rpx;
				margin: 0 10rpx;
				margin-bottom: 20rpx;
			}
			&-liact {
				color: #E14D2E;
				background-color: #FFEDE9;
			}
		}
	}
</style>
