<template>
	<el-layout>
		<el-navbar :placeholder="true" :fixed="true" :autoBack="true" :border="true" title="搜索"></el-navbar>
		<view class="search-box">
			<search :mode="2" button="inside" :placeholder="data.defaultKeyword" @search="doSearch(false)" @input="inputChange" @confirm="doSearch(false)" v-model="data.keyword"></search>
		</view>
		
		<view class="search-keyword" >
			<scroll-view class="keyword-list-box" v-show="data.isShowKeywordList" scroll-y>
				<block v-for="(row,index) in data.keywordList" :key="index">
					<view class="keyword-entry" hover-class="keyword-entry-tap" >
						<view class="keyword-text" @click.stop="doSearch(data.keywordList[index].name)">
							<rich-text :nodes="row.htmlStr"></rich-text>
						</view>
						<view class="keyword-img" @click.stop="setKeyword(data.keywordList[index].keyword)">
							<image src="/static/search/back.png"></image>
						</view>
					</view>
				</block>
				
			</scroll-view>
			<scroll-view class="keyword-box" v-show="!data.isShowKeywordList" scroll-y>
				<view class="keyword-block" v-if="data.oldKeywordList.length>0">
					<view class="keyword-list-header">
						<view>历史搜索</view>
						<view>
							<image @click="oldDelete" src="/static/search/delete.png"></image>
						</view>
					</view>
					<view class="keyword">
						<view v-for="(keyword,index) in data.oldKeywordList" @click="doSearch(keyword)" :key="index">{{keyword}}</view>
					</view>
				</view>
				<view class="keyword-block">
					<view class="keyword-list-header">
						<view>热门搜索</view>
						<view>
							<image @click="hotToggle" :src="'/static/search/attention'+data.forbid+'.png'"></image>
						</view>
					</view>
					<view class="keyword" v-if="data.forbid==''">
						<view v-for="(keyword,index) in data.hotKeywordList" @click="doSearch(keyword)" :key="index">{{keyword}}</view>
					</view>
					<view class="hide-hot-tis" v-else>
						<view>当前搜热门搜索已隐藏</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</el-layout>
</template>

<script setup>
	import { onLoad } from "@dcloudio/uni-app";
	import { ref } from "vue";
	import { navigateTo } from "@/el-ui/libs/navigate";
	
	let data = ref({
		defaultKeyword: "",
		keyword: "",
		oldKeywordList: [],
		hotKeywordList: [],
		keywordList: [],
		forbid: '',
		isShowKeywordList: false
	});
	
	onLoad(()=>{
		init();
	});
	
	const init = ()=>{
		loadDefaultKeyword();
		loadOldKeyword();
		loadHotKeyword();
	}
	
	const blur = ()=>{
		uni.hideKeyboard()
	}
	
	const loadDefaultKeyword = ()=>{
		data.value.defaultKeyword = "默认关键字";
	}
	
	const loadOldKeyword = ()=>{
		uni.getStorage({
			key: 'OldKeys',
			success: (res) => {
				let OldKeys = JSON.parse(res.data);
				data.value.oldKeywordList = OldKeys;
			}
		});
	}
	
	const loadHotKeyword = ()=>{
		uni.$http.get("search/keywords").then(res=>{
			if(res.code){
				data.value.hotKeywordList = res.data;
			}
		});
	}
	
	const inputChange = (event)=>{
		//兼容引入组件时传入参数情况
		let keyword = event.detail?event.detail.value:event;
		if (!keyword) {
			data.value.keywordList = [];
			data.value.isShowKeywordList = false;
			return;
		}
		
		data.value.isShowKeywordList = true;
		data.value.keyword = keyword;
		uni.$http.get("search/index",{ wd: keyword }).then(res=>{
			if(res.code){
				data.value.keywordList = [];
				data.value.keywordList = drawCorrelativeKeyword(res.data, keyword);
			}
		});
	}
	
	const drawCorrelativeKeyword = (keywords, keyword)=>{
		let len 		= keywords.length;
		let keywordArr  = [];
		for (let i = 0; i < len; i++) {
			let row = keywords[i];
			let html = row.replace(keyword, "<span style='color: #9f9f9f;'>" + keyword + "</span>");
			html = '<div>' + html + '</div>';
			let tmpObj = {
				name: keyword,
				keyword: row,
				htmlStr: html
			};
			
			keywordArr.push(tmpObj)
		}
		
		return keywordArr;
	}
	
	const setKeyword = (index)=>{
		data.value.keyword = data.value.keywordList[index].keyword;
	}
	
	const oldDelete = ()=>{
		uni.showModal({
			content: '确定清除历史搜索记录？',
			success: (res) => {
				if (res.confirm) {
					data.value.oldKeywordList = [];
					uni.removeStorage({
						key: 'OldKeys'
					});
				} else if (res.cancel) {}
			}
		});
	}
	
	const hotToggle = ()=>{
		data.value.forbid = data.value.forbid ? '' : '_forbid';
	}
	
	const doSearch = (keyword)=>{
		keyword = keyword === false ? data.value.keyword : keyword;
		data.value.keyword = keyword;
		saveKeyword(keyword);
		// uni.showToast({
		// 	title: keyword,
		// 	icon: 'none',
		// 	duration: 2000
		// });
		navigateTo(`pages/search/list?keyword=${keyword}`);
	}
	
	const saveKeyword = (keyword)=>{
		if(keyword.length <= 0){
			return ;
		}
		uni.getStorage({
			key: 'OldKeys',
			success: (res) => {
				let OldKeys = JSON.parse(res.data);
				let findIndex = OldKeys.indexOf(keyword);
				if (findIndex == -1) {
					OldKeys.unshift(keyword);
				} else {
					OldKeys.splice(findIndex, 1);
					OldKeys.unshift(keyword);
				}
				//最多10个纪录
				OldKeys.length > 10 && OldKeys.pop();
				uni.setStorage({
					key: 'OldKeys',
					data: JSON.stringify(OldKeys)
				});
				data.value.oldKeywordList = OldKeys; //更新历史搜索
			},
			fail: (e) => {
				let OldKeys = [keyword];
				uni.setStorage({
					key: 'OldKeys',
					data: JSON.stringify(OldKeys)
				});
				data.value.oldKeywordList = OldKeys; //更新历史搜索
			}
		});
	}
	
</script>

<style lang="scss">
	view{display:block;}
	.search-box {width:95%;background-color:rgb(242,242,242);padding:15upx 2.5%;display:flex;justify-content:space-between;position:sticky;top: 0;}
	.search-box .mSearch-input-box{width: 100%;}
	.search-box .input-box {width:85%;flex-shrink:1;display:flex;justify-content:center;align-items:center;}
	.search-box .search-btn {width:15%;margin:0 0 0 2%;display:flex;justify-content:center;align-items:center;flex-shrink:0;font-size:28upx;color:#fff;background:linear-gradient(to right,#ff9801,#ff570a);border-radius:60upx;}
	.search-box .input-box>input {width:100%;height:60upx;font-size:32upx;border:0;border-radius:60upx;-webkit-appearance:none;-moz-appearance:none;appearance:none;padding:0 3%;margin:0;background-color:#ffffff;}
	.placeholder-class {color:#9e9e9e;}
	.search-keyword {width:100%;background-color:rgb(242,242,242);}
	.keyword-list-box {height:calc(100vh - 110upx);padding-top:10upx;border-radius:20upx 20upx 0 0;background-color:#fff;}
	.keyword-entry-tap {background-color:#eee;}
	.keyword-entry {width:94%;height:80upx;margin:0 3%;font-size:30upx;color:#333;display:flex;justify-content:space-between;align-items:center;border-bottom:solid 1upx #e7e7e7;}
	.keyword-entry image {width:60upx;height:60upx;}
	.keyword-entry .keyword-text,.keyword-entry .keyword-img {height:80upx;display:flex;align-items:center;}
	.keyword-entry .keyword-text {width:90%;}
	.keyword-entry .keyword-img {width:10%;justify-content:center;}
	.keyword-box {height:calc(100vh - 110upx);border-radius:20upx 20upx 0 0;background-color:#fff;}
	.keyword-box .keyword-block {padding:10upx 0;}
	.keyword-box .keyword-block .keyword-list-header {width:94%;padding:10upx 3%;font-size:27upx;color:#333;display:flex;justify-content:space-between;}
	.keyword-box .keyword-block .keyword-list-header image {width:40upx;height:40upx;}
	.keyword-box .keyword-block .keyword {width:94%;padding:3px 3%;display:flex;flex-flow:wrap;justify-content:flex-start;}
	.keyword-box .keyword-block .hide-hot-tis {display:flex;justify-content:center;font-size:28upx;color:#6b6b6b;}
	.keyword-box .keyword-block .keyword>view {display:flex;justify-content:center;align-items:center;border-radius:60upx;padding:0 20upx;margin:10upx 20upx 10upx 0;height:60upx;font-size:28upx;background-color:rgb(242,242,242);color:#6b6b6b;}
</style>