<template>
	<view class="content">
		<view class="top">
			<u-search placeholder="搜索百知文库内容" v-model="searchValue" :showAction="false" @search="search" @change="clear"></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 >
			<view class="list2">
				<block v-for="(item,index) in searchList" :key="index">
					<view class="item_box">
						<view class="box_title">{{item.name}}</view>
						<view class="hflex acenter">
							<view v-if="item.img !== ''">
								<image :src="item.img" mode="aspectFill" class="item_img"></image>
							</view>
							<view>
								<view class="item_text">{{item.content}}</view>
								<view class="hflex acenter">
									<view class="avatar_box">
										<image :src="item.avatar" class="avatar"></image>
									</view>
									<view class="user_text">{{item.title}}</view>
									<view v-if="item.type" class="blue_v">
										V
									</view>
								</view>
							</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: [
					{
						name: '在高质量发展中扎实推动共同富裕！',
						img: '/static/images/index/class_img3.png',
						content: '在高质量发展中扎实推动共同富裕！！在高质量发展中扎实推动共同富！在…',
						avatar: '/static/images/mine/avatar1.jpg',
						title: '船百知官方账号',
						type: 1
					},
					{
						name: '在高质量发展中扎实推动共同富裕！',
						img: '',
						content: '在高质量发展中扎实推动共同富裕！！在高质量发展中扎实推动共同富！在…',
						avatar: '/static/images/mine/avatar1.jpg',
						title: '船百知官方账号',
						type: 1
					},
				],
			}
		},
		onLoad() {
			that = this
			that.history = uni.getStorageSync('historyBZ')
			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("historyBZ",that.history)
				that.is_search = 1
			},
			clear(e) {
				if(e == '') {
					that.is_search = 0
				}
			}
		},
	}
</script>

<style lang="scss" scoped>
	.content {
		padding: 0 30rpx;
		.top {
			width: 100%;
			background: #fff;
			padding: 36rpx 30rpx 36rpx;
			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;
				}
			}
		}
		.list2 {
			
			
			.item_box {
				width: 100%;
				padding: 36rpx 0;
				border-bottom: 1rpx solid #F3F3F3;
				
				.box_title {
					font-size: 32rpx;
					font-weight: 400;
					color: #333333;
					line-height: 44rpx;
					margin-bottom: 16rpx;
				}
				.item_img {
					width: 200rpx;
					height: 144rpx;
					border-radius: 12rpx;
					margin-right: 20rpx;
				}
				.item_text {
					font-size: 26rpx;
					font-weight: 400;
					color: #666666;
					line-height: 36rpx;
					text-overflow: ellipsis;
					overflow: hidden;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					box-orient: vertical;
					line-clamp: 2;
					-webkit-line-clamp: 2;
					margin-bottom: 36rpx;
				}
				.avatar_box {
					margin-right: 8rpx;
					.avatar {
						width: 32rpx;
						height: 32rpx;
						border-radius: 50%;
					}
				}
				.user_text {
					font-size: 24rpx;
					font-weight: 400;
					color: #666666;
					line-height: 34rpx;
				}
				.blue_v {
					background: #506DFF;
					border-radius: 12rpx 4rpx 12rpx 4rpx;
					margin-left: 8rpx;
					text-align: center;
					line-height: 28rpx;
					// padding: 6rpx;
					width: 28rpx;
					height: 28rpx;
					color: #fff;
					font-size: 14rpx;
				}
			}
		}
	}
</style>