<template>
	<view class="content">
		<view class="top">
			<u-search placeholder="热门搜索" v-model="searchValue" :showAction="true" @search="search" actionText="取消" @custom="clear" @change="change"></u-search>
		</view>
		<view v-if="is_search == 0">
			<view class="hflex acenter jbetween">
				<view class="title">搜索历史</view>
				<u-icon name="trash" color="#555555" size="16"></u-icon>
			</view>
			<view class="list hflex acenter fwrap">
				<block v-for="(item,index) in history" :key="index">
					<view class="item" @click="search(item)">{{item}}</view>
				</block>
			</view>
			<view class="">
				<view class="title">热门搜索</view>
			</view>
			<view class="list hflex acenter fwrap">
				<block v-for="(item,index) in hotList" :key="index">
					<view class="item hflex acenter" @click="search(item.name)">
						<view>{{item.name}}</view>
						<view class="hot" v-if="item.is_hot == 1">热</view>
					</view>
				</block>
			</view>
		</view>
		<view v-else class="bg">
			<view class="list2 hflex acenter fwrap">
				<block v-for="(item,index) in searchList" :key="index">
					<view class="list_item" @click="toDetail(item.id)">
						<image :src="item.img" mode="aspectFill" class="item_img"></image>
						<view style="box-sizing: border-box;padding: 12rpx 10rpx 0;">
							<view class="item_name">{{item.name}}</view>
							<!-- <view class="hflex acenter " style="padding-top: 12rpx;">
								<view class="item_type">{{item.material}}</view>
								<view class="item_type">{{item.parts}}</view>
							</view> -->
							<view class="item_price">￥{{item.price}}</view>
						</view>
					</view>
				</block>
			</view>
		</view>
	</view>
</template>

<script>
	import $api from '@/static/js/api.js'
	var that = ''
	export default {
		data() {
			return {
				searchValue: '',
				history: [],
				hotList: [
					{
						name: '船机维修',
						is_hot: 1
					},
					{
						name: '船机维修',
						is_hot: 1
					},
					{
						name: '船机维修',
						is_hot: 0
					},
					{
						name: '船机维修',
						is_hot: 0
					},
					{
						name: '船机维修',
						is_hot: 0
					},
					{
						name: '船机维修',
						is_hot: 0
					},
					{
						name: '船机维修',
						is_hot: 1
					},
					
				],
				is_search: 0,
				searchList: [
					{
						id: 1,
						img: '/static/images/index/class_img4.png',
						name: '王思聪同款王思聪同款豪华游艇大型钓鱼船户外海上滑梯水豪华游艇大型钓鱼船户外海上滑梯水',
						material: '玻璃钢',
						parts: '氧气罐',
						price: '9928.00',
					},
					{
						id: 1,
						img: '/static/images/index/class_img4.png',
						name: '王思聪同款王思聪同款豪华游艇大型钓鱼船户外海上滑梯水豪华游艇大型钓鱼船户外海上滑梯水',
						material: '玻璃钢',
						parts: '氧气罐',
						price: '9928.00',
					},
				],
			}
		},
		onLoad() {
			that = this
			that.history = uni.getStorageSync('historySC')
			if(!that.history) {
				that.history = []
			}
		},
		methods: {
			search(e) {
				console.log(e);
				var is_some = 0
				that.searchValue = e
				if (that.history.length > 0) {
					for(var i=0;i<that.history.length;i++) {
						if(that.history[i] == that.searchValue) {
							is_some = 1;
						}
					}
					if(is_some == 0) {
						that.history.unshift(that.searchValue)
					}
				} else {
					that.history.push(that.searchValue)
				}
				if(that.history.length > 9) {
					that.history.pop()
				}
				uni.setStorageSync("historySC",that.history)
				that.is_search = 1
			},
			change(e) {
				if(e == '') {
					that.is_search = 0
				}
			},
			clear(e) {
				that.is_search = 0,
				that.searchValue = ''
			}
		},
	}
</script>

<style lang="scss" scoped>
	.content {
		padding: 0 30rpx;
		position: relative;
		.top {
			width: 100%;
			background: #fff;
			padding: 16rpx 30rpx 8rpx;
			box-sizing: border-box;
			// margin-top: ;
		}
		.title {
			font-size: 30rpx;
			font-weight: 500;
			color: #222222;
			line-height: 42rpx;
			padding: 24rpx 0;
		}
		.list {
			.item {
				width: auto;
				background: #F2F2F2;
				border-radius: 12px;
				box-sizing: border-box;
				padding: 8rpx 20rpx;
				font-size: 24rpx;
				font-weight: 400;
				color: #555555;
				margin: 0 24rpx 28rpx 0;
				.hot {
					width: 28rpx;
					height: 28rpx;
					margin-left: 12rpx;
					background-color: #ff5c37;
					border-radius: 20%;
					font-size: 18rpx;
					color: #fff;
					text-align: center;
					line-height: 28rpx;
				}
			}
		}
		.bg{
			position: absolute;
			left: 0;
			top: 97rpx;
			min-height: calc(100vh - 97rpx);
			width: 100%;
			background: #F4F4F4;
			box-sizing: border-box;
			padding: 20rpx 30rpx;
		}
		.list2 {
			
			.list_item {
				width: 334rpx;
				background: #FFFFFF;
				border-radius: 20rpx;
				box-sizing: border-box;
				padding: 0 0 22rpx;
				margin: 0 22rpx 20rpx 0;
				.item_img {
					width: 334rpx;
					height: 280rpx;
					border-radius: 20rpx 20rpx 0px 0px;
				}
				.item_name {
					width: 100%;
					text-overflow: ellipsis;
					overflow: hidden;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					box-orient: vertical;
					line-clamp: 2;
					-webkit-line-clamp: 2;
					font-size: 28rpx;
					font-weight: 400;
					color: #222222;
					line-height: 40rpx;
				}
				.item_type {
					
					height: 28rpx;
					background: #F5F5F5;
					border-radius: 4rpx; 
					font-size: 20rpx;
					font-weight: 400;
					color: #999999;
					line-height: 28rpx;
					padding: 0 14rpx;
					margin-right: 12rpx;
				}
				.item_price {
					padding-top: 12rpx;
					font-size: 28rpx;
					font-weight: bold;
					color: #FF4747;
					line-height: 32rpx;
				}
			}
			.list_item:nth-child(2n+2) {
				margin: 0 0 20rpx;
			}
		}
	}
</style>