<template>
	<view class="searchTarget-container container">
		<view class="searchTarget-header">
			<input type="text" 
			placeholder="请输入关键字" 
			placeholder-class="sea_ipt_placeholder" 
			class="sea_ipt" 
			v-model="searchTxt"
			focus="true"
			confirm-type="search"
			@confirm="searchHandle"/>
			<icon type="search" size="15" class="sea_ico"/>
			<view class="ipt-right">
				<icon type="clear" size="16" color="#E0E0E0" v-show="searchTxt" @click="clearIpt"/>
				<text  class="line">|</text>
				<text @click="searchHandle" style="color: #3385FF;">搜索</text>
			</view>
		</view>
		<view class="search-cont">
			<template v-if="!isResult">
				<view class="search-cont-top" v-if="historySearchList.length">
					<view class="cont-tit">
						<text>搜索历史</text>
						<image src="@/static/img/empty_ico.png" class="empty_ico" @click="clearHistory"></image>
					</view>
					<view class="targetList">
						<view class="target-item" v-for="(item,index) in historySearchList" :key="index" @click="chooseTarget(item)">{{item}}</view>
					</view>
				</view>
				<view class="search-cont-top">
					<view class="cont-tit">
						<text>弘则推荐</text>
					</view>
					<view class="targetList">
						<view class="target-item" v-for="(item,index) in keywordList" :key="index" @click="chooseTarget(item)">{{item}}</view>
					</view>
				</view>
			</template>
			<template v-else>
				<block v-if="haveResult">
          <!-- 等接口待完善 -->
<view class="tab-cont">
				<scroll-view 
				scroll-x="true" 
				scroll-with-animation 
				class="scroll-tab" 
				:scroll-into-view="'_'+tabIndex">
					<block v-for="(item, index) in tabBars" :key="item.ChartPermissionId">
						<view :id="'_'+index" class="scroll-tab-item" :class="{ active: tabAct_id === item.ChartPermissionId }" @click.stop="toggleTab(item,index)">
						{{item.PermissionName}}
						<image src="@/static/img/border_act.png" mode="" class="border_act" v-if="tabAct_id === item.ChartPermissionId"></image>
						</view>
					</block>
				</scroll-view>
			</view>
					<view class="result-data">
						<view class="report-ul">
							<view class="report-item" v-for="(report,index) in resultList" :key="index" v-if="index%2 === 0" @click="goDetail(report)">
								<!-- <view class="item-content">{{report.content}}</view> -->
								<view class="item-content"><u-parse :html="report.Body[0]+'...'"></u-parse></view>
								<view class="line"></view>
								<view class="item-title"><u-parse :html="report.Title"></u-parse></view>
								<view class="item-abstract" v-if="report.abstract">
									<image src="@/static/img/report_ico.png" class="report_ico"></image>
									{{report.abstract}}
								</view>
								<text class="item-createtime">{{report.PublishDate}}</text>
							</view>
						</view>
						<view class="report-ul">
							<view class="report-item" v-for="(report,index) in resultList" :key="index"  v-if="index%2 !== 0" @click="goDetail(report)">
								<!-- <view class="item-content">{{report.content}}</view> -->
								<view class="item-content"><u-parse :html="report.Body[0]+'...'"></u-parse></view>
								<view class="line"></view>
								<view class="item-title"><u-parse :html="report.Title"></u-parse></view>
								<view class="item-abstract" v-if="report.ExpertBackground">
									<image src="@/static/img/report_ico.png" class="report_ico"></image>
									{{report.ExpertBackground}}
								</view>
								<text class="item-createtime">{{report.PublishDate}}</text>
							</view>
						</view>
					</view>
					<u-loadmore :status="status" icon-type="flower" :load-text="loadText"  bg-color="#F7F7F7" padding-bottom="20rpx"/>
					<!-- v-if="totalPage>1" -->
				</block>
				<view class="nodata" v-else>
					<image src="@/static/img/nodata.png" mode="" class="nodata_ico"></image>
					<text>未找到您想搜索的内容</text>
				</view>
			</template>
		</view>
	</view>
</template>

<script>
	import { Search } from '@/config/api.js';
	import { Debounce,Throttle } from '@/config/util.js'
	export default {
		data() {
			return {
				searchTxt:'',//搜索关键字
				isResult:false,//显示搜索结果
				haveResult:true,//是否有搜索数据
				// 历史搜索列表
				historySearchList:[],
				// 关键字列表
				keywordList:[],
				targetList:[],//所有指标列表
				// 搜索结果列表
				resultList:[],
				page_no: 1,
				pageSize: 10,
				totalPage: 0,
				orderColumn:'Comprehensive',
				loadText:{
					loadmore: '上拉加载更多',
					loading: '加载中',
					nomore: '已经到底了'
				},
				status:'loadmore',
				tabAct_id: 1,
				isTabAct:true,
				tabBars: [{
        					PermissionName: '综合排序',
							ChartPermissionId:1,
							mode:'Comprehensive'
        				}, {
        					PermissionName: '匹配度排序',
							ChartPermissionId:2,
							mode:'Matching'
        				}, {
        					PermissionName: '发布时间排序',
							ChartPermissionId:3,
							mode:'PublishDate'
        				}],
			};

		},
		watch:{
			searchTxt(newVal) {
				if(newVal.length <= 0) {
					this.tabAct_id=1
					this.isResult = false;
				}
			},
      //监听tabs的变化
      tabAct_id: {
      			handler() {
					if(this.isTabAct){
						// console.log(111)
						// this.getDataList();
					}
      				
      			},
      			// immediate:true
      		}
		},
		methods:{
      //tabs切换事件
     	toggleTab (item,index) {
			this.orderColumn=item.mode
     			if(this.tabAct_id !== item.ChartPermissionId) {
     				this.tabAct_id = item.ChartPermissionId;
     				this.pageNum = 1;
					this.getDataList();
     				uni.pageScrollTo({
     				   scrollTop: 0,
     				   duration: 0,
     				});
     			}
     		},
			/* 获取关键词 */
			getKeyWord() {
				Search.getKeys().then(res => {
					if(res.Ret === 200) {
						this.keywordList = res.Data.Item.ConfigValue ? res.Data.Item.ConfigValue.split(',') : [];
					}
				})
			},
			// 选择历史搜索
			chooseTarget(item) {
				this.searchTxt = item;
				this.SecName = item;
				this.resultList = [];
				this.page_no = 1;
				this.getDataList();	
			},
			// 键盘输入过程中
			searchDoing() {
				this.isResult = false;
				//全部指标列表
				let arr = JSON.parse(JSON.stringify(this.targetList));
				let filterArr = [];
				// // 过滤出符合搜索条件的值
				arr.forEach((item,index)=>{
					if(item.SecName.includes(this.searchTxt)){
						item.SecName = this.join(item.SecName,this.searchTxt)
						filterArr.unshift(item);
					}
				});
				this.keywordList = filterArr;
			},
			// 拼接
			join(str,key){
				return str.replace(new RegExp(`${key}`, 'g'), `%%${key}%%`).split('%%');
			},
			// 搜索数据
			searchHandle: Debounce(function() {
				if(this.searchTxt) {
					//添加搜索记录
					if(!this.historySearchList.includes(this.searchTxt)) {
						this.historySearchList.unshift(this.searchTxt);
						this.$db.set('historySearchList',JSON.stringify(this.historySearchList))
					}
					this.resultList = [];
					this.page_no = 1;
					this.getDataList();
				}else {
					this.$util.toast('请输入关键字')
				}
			}),
			// 查找数据
			getDataList() {
				this.isResult = true;
				Search.getResult({
					KeyWord: this.searchTxt,
					// PageSize: this.pageSize,
					// CurrentIndex: this.page_no,
					OrderColumn:this.orderColumn
				}).then(res => {
					if(res.Ret === 200) {
						// this.status = this.page_no < res.Data.Paging.Pages ? 'loadmore' : 'nomore';
						this.status = 'nomore';
						this.totalPage = res.Data.Paging.Pages;//总页数
						if(this.page_no === 1) {
							this.resultList = res.Data.List || [];
							this.haveResult = this.resultList.length ? true : false
						}else {
							this.resultList = this.resultList.concat(res.Data.List)
						}
					}
				})
			},
			// 点击数据列表修改数据
			itemClick(item) {
				let data = encodeURIComponent(JSON.stringify(item));
				uni.navigateTo({
					url:'/pages/recordData/recordData?data=' + data
				})
			},
			/* 表单清空 */
			clearIpt() {
				
				this.searchTxt = '';
				this.isTabAct=false
				this.orderColumn='Comprehensive'
				// 
			},
			/* 历史搜索清空 */
			clearHistory() {
				this.historySearchList = [];
				this.$db.del('historySearchList');
			},
			/* 进入详情 校验是否有该品种权限 */
			goDetail(item) {
				uni.navigateTo({
					url:'/pages/reportDetail/reportDetail?id=' + item.ArticleId,
				});
				
			},
		},
		/* 触底 */
		onReachBottom: Throttle(function() {
			// this.status = 'nomore';
			if(this.isResult) {
				// if(this.status === 'nomore') return ;
				
				// this.page_no++;
				// this.getDataList();				
			}
		}),
		onLoad() {
			// 获取历史搜索记录
			if(this.$db.get('historySearchList')) {
				let historyList = JSON.parse(this.$db.get('historySearchList'));
				this.historySearchList = historyList;
			}
		},
		onShow() {
			this.getKeyWord();
			
		}
	}
</script>

<style lang="scss">
.searchTarget-container {
	background-color: #fff;
	.searchTarget-header {
		padding: 0 34rpx;
		width: 100%;
		background-color: #fff;
		position: fixed;
		top: 0;
		left: 0;
		z-index: 99;
		padding: 30rpx 0;
		display: flex;
		justify-content: center;
		align-items: center;
		.sea_ipt_placeholder {
			color: #E5E5E5;
		}
		.sea_ipt {
			width: 682rpx;
			height: 70rpx;
			line-height: 70rpx;
			box-sizing: border-box;
			border: 1rpx solid #E5E5E5;
			background-color: rgba(245, 245, 245, 0.2);
			font-size: 26rpx;
			color: #4A4A4A;
			padding: 0 180rpx 0 78rpx;
			border-radius: 70rpx;
		}
		.sea_ico {
			width: 31rpx;
			height: 31rpx;
			position: absolute;
			left: 68rpx;
			top: 50%;
			transform: translateY(-50%);
		}
		.ipt-right {
			display: flex;
			align-items: center;
			position: absolute;
			right:59rpx;
			top: 50%;
			transform: translateY(-50%);
			color: #3385FF;
			.line {
				margin: 0 21rpx;
				color: #E0E0E0;
			}
		}
	}
	.search-cont {
		padding-top: 130rpx;
		.search-cont-top {
			padding: 0 34rpx 0;
			margin-bottom: 10rpx;
			padding-top: 20rpx;
			&:last-child {
				margin-bottom: 0;
			}
			.cont-tit {
				color: #666;
				font-size: 28rpx;
				margin-bottom: 30rpx;
				display: flex;
				justify-content: space-between;
				.empty_ico {
					width: 32rpx;
					height: 33rpx;
				}
			}
			.targetList {
				display: flex;
				flex-wrap: wrap;
				// justify-content: space-between;
				.target-item {
					padding: 4rpx 18rpx;
					color: #4A4A4A;
					font-size: 26rpx;
					// border: 1rpx solid #3385ff;
					background-color: #F7F7F7;
					margin-bottom: 30rpx;
					margin-right: 30rpx;
					border-radius: 20rpx;
				}
			}
		}
		.result-cont {
			padding: 0 34rpx 0;
			padding-left: 21rpx;
			.result-list {
				display: flex;
				align-items: center;
				color: #333;
				padding-bottom: 30rpx;
				border-bottom: 1rpx solid #EBEDF0;
				margin-bottom: 30rpx;
				.result_ico {
					width: 28rpx;
					height: 28rpx;
					margin-right: 20rpx;
				}
				text {
					display: inline;
				}
				.highlight {
					color: #3385FF;
				}
			}
		}
		.result-data {
			margin-top: 80rpx;
			min-height: calc(100vh - 130rpx);
			padding: 20rpx 34rpx 40rpx;
			display: flex;
			background-color: #F7F7F7;
			.report-ul {
				width: 50%;
				&:first-child {
					margin-right: 10rpx;
				}
				.report-item {
					padding: 20rpx 20rpx 24rpx 20rpx;
					margin-bottom: 20rpx;
					border-radius: 8rpx;
					box-shadow: 0 3rpx 6rpx rgba($color: #000000, $alpha: 0.16);
					background: #fff;
					.item-content {
						// height: 273rpx;
						font-size: 24rpx;
						line-height: 40rpx;
						color: #7F7F7F;
						display: -webkit-box;
						// text-overflow:ellipsis;
						// overflow: hidden;
						text {
							display: inline;
						}
					}
					.line {
						margin: 18rpx 0;
						content: '';
						width: 100%;
						height: 1px;
						padding: 0 32rpx;
						box-sizing: border-box;
						background-color: #E5E5E5;
						-webkit-transform: scale(1, 0.5);
						transform: scale(1, 0.5);
						-webkit-transform-origin: center bottom;
						transform-origin: center bottom;
					}
					.item-title {
						font-size: 28rpx;
						color: #4A4A4A;
						margin-bottom: 10rpx;
						text {
							display: inline;
						}
					}
					.item-abstract {
						font-size: 26rpx;
						color: #6A6A6A;
						margin-bottom: 10rpx;
						.report_ico {
							width: 32rpx;
							height: 26rpx;
							margin-right: 20rpx;
							display: inline-block;
						}
					}
					.item-createtime {
						color: #ACACAC;
						font-size: 24rpx;
					}
				}
			}
		}
	}
  	.tab-cont {
			position: fixed;
			top: 128rpx;
			width: 100%;
  			padding: 0 26rpx;
  			background-color: #fff;
  			font-size: 32rpx;
			z-index: 99;
  			box-shadow: 0 3rpx 6rpx rgba(187,216,255,0.2);
  			.scroll-tab {
  				width: 100%;
  				white-space: nowrap;
  			}
  			.scroll-tab-item {
  				// flex-grow: 1;
  				text-align: center;
  				display: inline-block;
  				padding: 0 8rpx 30rpx 8rpx;
  				margin-right: 40rpx;
  				border-bottom: 8rpx solid transparent;
  				position: relative;
  				&:last-child {
  					margin-right: 0;
  				}
  				&.active {
  					border-bottom: none;
  					color: #2C83FF;
  					font-weight: 700;
  				}
  				.border_act {
  					width: 100%;
  					height: 8rpx;
  					position: absolute;
  					bottom: 0;
  					left: 0;
  				}
  			}
  		}
}
</style>
