<template>
	<view class="search-page">
		<view class="search-box">
			<input class="search" type="text" placeholder="搜索" v-model="query.queryName" />
			<image class="icon" src="@/static/search.png" mode=""></image>
			<button class="btn" type="default" @click="handleSearch">搜索</button>
		</view>

		<view class="content" v-show="!list.length">
			<view class="title-box">
				<view class="title">
					历史搜索
				</view>
				<u-icon name="trash" color="#999" size="42" @click="delSearchHistory"></u-icon>
			</view>

			<view class="tab-box">
				<view class="tab" v-for="(item,index) in tabList" :key="index" @click="handleHistory(item)">
					{{item}}
				</view>
			</view>
		</view>
		<view class="content" v-show="list.length">
<!-- 			<view class="tab-group">
				<view class="tab" :class="[tab == 1?'active':'']" @click="handleTab(1)">
					店铺
				</view>
				<view class="tab" :class="[tab == 2?'active':'']" @click="handleTab(2)">
					商品
				</view>
			</view>

			<zs-list mt="0" @load="loadMore" :status="status">
				<view :class="[tab == 1?'item':'goods-item']" v-for="(item,index) in list" :key="index">
					<template v-if="tab == 1">
						<zs-img :src="item.logoPath" width="164rpx" height="164rpx" radius="full"
							@click.native="goDetail(item)"></zs-img>
						<view class="info">
							<view class="title">
								{{item.shopVo.shopName}}
							</view>
							<view class="address">
								<image class="position" src="../../static/position.png" mode=""></image>
								距离你{{(item.shopVo.distance/1000).toFixed(2)}}km ·{{item.district}}
							</view>
							<view class="activity" :class="!item.activityContent?'white':''">
								{{item.activityContent || '-'}}
							</view>

						</view>
					</template>
					<template v-else>
							<zs-img  :src="item.goodsImg" width="310rpx" height="310rpx" raduis="full"  mode="" ></zs-img>
							<view class="goods-name">
								{{item.goodsName}}
							</view>
							<view class="type">
								{{goodsTypeObj[item.goodsCode]}}
							</view>
							<view class="price">
								¥{{item.buyPrice}}
							</view>
					</template>
				</view>
			</zs-list> -->

		</view>
	</view>
</template>

<script>
	import {
		getSearchHistory,
		saveSearchHistory,
		delSearchHistory
	} from '@/api/common.js';
	import {
		search
	} from '@/api/shop.js';
	export default {
		data() {
			return {
				status: 'more',
				result: false,
				tab: 1,
				tabList: [],
				list: [],
				query: {
					pageCurrent: 1,
					pageSize: 10,
					'location.lat': 0,
					'location.lon': 0,
					queryName: ''
				}
			}
		},
		methods: {
			handleHistory(val){
				this.query.queryName = val
				this.handleSearch()
			},
			goDetail(item) {
				uni.setStorageSync('shopInfo', JSON.stringify(item))
				uni.navigateTo({
					url: '/detail/shopDetail/shopDetail'
				})
			},
			handleTab(val) {
				this.tab = val
				this.query.pageCurrent = 1
				this.list = []
			},
			handleSearch() {
				this.status = 'more'
				this.query.pageCurrent = 1
				this.list = []
				this.saveSearchHistory()
				uni.navigateTo({
					url:'./searchResult?search='+this.query.queryName
				})
				// this.search()
			},
			getSearchHistory() {
				getSearchHistory().then(res => {
					if (res.state == 'Success') {
						this.tabList = res.content
					}
				})
			},
			saveSearchHistory() {
				saveSearchHistory({
					key: this.query.queryName
				}).then(res => {
					if (res.state == 'Success') {
						this.getSearchHistory()
					}
				})
			},
			delSearchHistory() {
				delSearchHistory().then(res => {
					if (res.state == 'Success') {
						this.getSearchHistory()
					}
				})
			},
			search() {
				if(this.status == 'noMore') return
				this.status = 'loading'
				search(this.query).then(res => {
					if (res.state == 'Success') {
						this.saveSearchHistory()
						this.list = this.list.concat(res.content.records)
						if(res.content.total>=this.list.length){
							this.status = 'noMore'
						}else{
							this.status = 'more'
							this.query.pageCurrent++
						}
					}
				})
			},
			loadMore() {
				if(this.query.queryName){
					this.search()
				}
			}
		},
		onLoad(options) {
			let location = JSON.parse(uni.getStorageSync('location'))
			this.query['location.lat'] = location.latitude
			this.query['location.lon'] = location.longitude
			this.getSearchHistory()
		},
		created() {}
	}
</script>

<style lang="scss" scoped>
	.search-page {
		.search-box {
			height: 72rpx;
			width: 686rpx;
			border-radius: 40rpx 40rpx 40rpx 40rpx;
			background: #F6F6F6;
			position: relative;
			margin: 30rpx auto;

			.icon {
				width: 37rpx;
				height: 37rpx;
				position: absolute;
				top: 50%;
				left: 36rpx;
				transform: translateY(-50%);
			}

			.search {
				line-height: 72rpx;
				height: 72rpx;
				padding-left: 100rpx;
				padding-right: 150rpx;
			}

			.btn {
				position: absolute;
				top: 8rpx;
				right: 10rpx;
				width: 128rpx;
				height: 56rpx;
				line-height: 56rpx;
				background: $uni-color-primary;
				color: #fff;
				font-size: 28rpx;
				border-radius: 32rpx;
				padding: 0;
			}
		}
		.content{
			padding: 0 30rpx;
		}

		.title-box {
			display: flex;
			align-items: center;
			justify-content: space-between;

			.title {
				font-weight: bold;
				color: #222222;
				font-size: 32rpx;
			}
		}

		.tab-box {
			display: flex;
			flex-wrap: wrap;
			align-items: center;
			padding: 24rpx 0;

			.tab {
				padding: 0 20rpx;
				line-height: 44rpx;
				border-radius: 22rpx;
				background: #F6F6F6;
				color: #999999;
				font-size: 24rpx;
				max-width: 700rpx;
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
				margin-bottom: 20rpx;
				margin-right: 20rpx;
			}
		}
		
		.tab-group{
			display: flex;
			align-items: center;
			border-bottom: 1rpx solid #F0F0F0;
			margin-top: -30rpx;
			.tab{
				flex: 1;
				height: 80rpx;
				line-height: 80rpx;
				text-align: center;
				font-size: 28rpx;
				color: #222222;
			}
			.tab.active{
				position: relative;
				&::before{
					content: '';
					position: absolute;
					bottom: 0;
					left: 50%;
					width: 40rpx;
					height: 8rpx;
					transform: translateX(-50%);
					border-radius: 2rpx;
					background: $uni-color-primary;
				}
				
				
			}
		}
		
		.zs-list {
			padding: 0 30rpx;
			.item {
				display: flex;
				padding: 20rpx 30rpx;
				background: #fff;
				border-radius: 16rpx;
				margin: 20rpx 30rpx 0;
				.icon{
					border-radius: 8px;
				}	
				.info{
					flex: 1;
					padding-left: 20rpx;
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					position: relative;
					.title{
						font-size: 28rpx;
						font-weight: bold;
						width: 340rpx;
						white-space: nowrap;
						overflow: hidden;
						text-overflow: ellipsis;
						margin-top: 10rpx;
					}
					.address{
						color: #bebebe;
						display: flex;
						align-items: center;
						font-size: 24rpx;
						.u-icon{
							align-self: flex-start;
						}
						.position {
								width: 25rpx;
								height: 29rpx;
								margin-right: 8rpx;
							}
					}
					.activity.white{
						color: #fff;
					}
					.activity{
						font-size: 24rpx;
						color: #E77F57;
						line-height: 42rpx;
					}
				}
			}
			
			.goods-item{
				padding: 12rpx;
				background: #FFFFFF;
				box-shadow: 0rpx 0rpx 24rpx 2rpx rgba(0,0,0,0.08);
				border-radius: 16rpx;
				width: 334rpx;
				box-sizing: border-box;
				margin-top: 20rpx;
				.goods-name{
					font-weight: bold;
					color: #222222;
					font-size: 24rpx;
					margin: 16rpx 0;
					overflow: hidden;
					width: 100%;
					text-overflow: ellipsis;
					display: -webkit-box;
					-moz-box-orient: vertical;
					-webkit-line-clamp: 2;
				}
				.type{
					color: #AAAAAA;
					font-size: 20rpx;
				}
				.price{
					font-weight: bold;
					color: #FF4C3A;
					font-size: 28rpx;
					margin-top: 16rpx;
				}
			}
		}
		
	}
</style>