<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="">
						<view class="report-ul">
							<view class="report-item" v-for="(item,index) in resultList" :key="index" >
								<view class="box-text" @click="goDetail(item)">
										<view class="" style="padding-left: 30rpx;">
											{{item.IndustryName}}
										</view>
										<view class="text-time">
											<text style="padding-right: 20rpx;">{{item.UpdateTime}}更新</text>
											<u-icon name="arrow-right" color="#BDBDBD" size="34"></u-icon>
										</view>
								</view>
								<view class="" v-if="item.IndustrialSubjectList.length>6">
									<u-read-more :shadow-style="shadowStyle" close-text="展开" color='#D1D1D1'  :show-height="item.IndustrialSubjectList.length>6 ? 170 :200" :toggle="true">
											<view class="arrow-box">
												<view  @click="goDetail(item)" class="box-subject"  v-for="key in item.IndustrialSubjectList" :key="key.RecommendedIndex">
													{{key.SubjectName}}
												</view>
											</view>
									</u-read-more>
								</view>
								<view class="" v-else  @click="goDetail(item)">
									<view class="arrow-box">
										<view class="box-subject"  v-for="key in item.IndustrialSubjectList" :key="key.RecommendedIndex">
											{{key.SubjectName}}
										</view>
									</view>
								</view>
							
							</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 ,Reports} 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,
				loadText:{
					loadmore: '上拉加载更多',
					loading: '加载中',
					nomore: ''
				},
				status:'loadmore',		
				shadowStyle: {
					backgroundImage: "none"
				}
			};

		},
		watch:{
			searchTxt(newVal) {
				if(newVal.length <= 0) {
					this.isResult = false;
				}
			},
		},
		methods:{
			/* 获取关键词 */
			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('historySearchListReport',JSON.stringify(this.historySearchList))
					}
					this.resultList = [];
					this.page_no = 1;
					this.getDataList();
				}else {
					this.$util.toast('请输入关键字')
				}
			}),
			// 查找数据
			getDataList() {
				this.isResult = true;
				Reports.getIndustryList({
					KeyWord: this.searchTxt,
					PageSize: this.pageSize,
					CurrentIndex: this.page_no,
				}).then(res => {
					if(res.Ret === 200) {
						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)
						}
						if(!this.haveResult) return
						this.status = this.page_no < res.Data.Paging.Pages ? 'loadmore' : 'nomore'
						this.totalPage = res.Data.Paging.Pages;//总页数
					}
				})
			},
			// 点击数据列表修改数据
			itemClick(item) {
				let data = encodeURIComponent(JSON.stringify(item));
				uni.navigateTo({
					url:'/pages/recordData/recordData?data=' + data
				})
			},
			/* 表单清空 */
			clearIpt() {
				this.searchTxt = '';
				this.isTabAct=false
			},
			/* 历史搜索清空 */
			clearHistory() {
				this.historySearchList = [];
				this.$db.del('historySearchListReport');
			},
			/* 进入详情 校验是否有该品种权限 */
			goDetail(item) {
				uni.navigateTo({
					url:'/reportPages/IndustryReport/IndustryReport?id=' + item.IndustrialManagementId,
				});
				
			},
		},
		/* 触底 */
		onReachBottom: Throttle(function() {
			if(this.isResult) {
				if(this.status === 'nomore') return ;
				this.status = 'loading';
				this.page_no++;
				this.getDataList();				
			}
		}),
		onLoad(options) {
			this.$store.dispatch("checkHandle",{type:'load',val:option}).then(res=>{
				if(options.text) {
					this.searchTxt=options.text
					this.getDataList();	
				}
				// 获取历史搜索记录
				if(this.$db.get('historySearchListReport')) {
					let historyList = JSON.parse(this.$db.get('historySearchListReport'));
					this.historySearchList = historyList;
				}
			})
			
		},
		onShow() {
			this.$store.dispatch("statistics",{PageType:'ReportSearch'})
			this.getKeyWord()		
		},
		/**
		 * 用户点击分享
		 */
		onShareAppMessage: function(res) {
			return {
				title: "报告",
				path: '/reportPages/reportSearch/reportSearch?text='+this.searchTxt,
				success: (res) => {},
				fail: (err) => {}
			}
		},
	}
</script>

<style lang="scss">
.searchTarget-container {
	background-color: #f6f6f6;
	padding-bottom: 30rpx;
	.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;
			}
		}
	}
	.report-item {
		margin-top: 10rpx;
		width: 100%;
		background-color: #fff;
		.box-text {
			display: flex;
			justify-content: space-between;
			background-color: #fff;
			align-items: center;
			padding: 0 30rpx;
			height: 91rpx;
			color: #333333;
			font-size: 30rpx;
			.text-time {
				display: flex;
				color: #999999;
				font-size: 26rpx;
			}
		}
		.arrow-box {
			display: flex;
			flex-wrap: wrap;
			border-top:3rpx solid #f6f6f6;
			padding: 30rpx 30rpx 0;
			.box-subject {
				margin-bottom: 30rpx;
				margin-left: 40rpx;
				background:url(../../static/img/report_bg.png) no-repeat;
				background-size: 100% 100%;
				width: 170rpx;
				height: 46rpx;
				color: #408FFF;
				text-align: center !important;
				font-size: 24rpx;
				line-height: 46rpx;
				text-indent:0em;
			}
		}
	}
	.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;
				
			
		}
	}
  	.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;
  				}
  			}
  		}
		.u-line {
			display: none !important;
		}
}
</style>
