<template>
	<view class="goods-search flex-col news-list">
		<view class="header-wrap">
			<view class="search">
				<u-search v-model="keyword" @focus="showHistory = true" :focus="showHistory" @search="onSearch"
					bg-color="#F4F4F4"></u-search>
			</view>
		</view>
		<view v-show="showHistory" class="search-content bg-white">
			<scroll-view :scroll-y="true" style="height: 100%">
				<view v-if="hotList.length" class="search-words">
					<view class="title">热门搜索</view>
					<view class="words flex flex-wrap">
						<view v-for="(item, index) in hotList" :key="index"
							class="item br60  m-r-20 m-b-20 lighter sm line-1" @tap="onChangeKeyword(item)">{{item}}</view>
					</view>
				</view>
				<view v-if="historyList.length" class="search-words">
					<view class="title flex row-between">
						<view>历史搜索</view>
						<view class="xs muted m-r-20" style="padding: 10rpx 20rpx" @tap="clearSearchFun">清空</view>
					</view>
					<view class="words flex flex-wrap">
						<view v-for="(item, index) in historyList" :key="index"
							class="item br60  m-r-20 m-b-20 lighter sm line-1" @tap="onChangeKeyword(item)">{{item}}</view>
					</view>
				</view>
			</scroll-view>
		</view>
		<view class="main">
			<mescroll-uni ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback"
				:up="upOption" :down="{auto: true}" :fixed="false">
				<view class="article-list">
					<view class="article-item bg-white"  v-for="(item, index) in newsList" :key="index" >
						<router-link :to="{path: '/bundle/pages/resource_details/resource_details', query: {id: item.id, type}}">
							<view class="flex col-top">
								<view class="info flex-1">
									<view class="title lg line-2 m-b-20">{{ item.title }}</view>
									<view class="lighter line-2">
										<view>{{ item.intro }}</view>
									</view>
									<view class="flex row-between price">
										<view class="xs muted">价格: <text style="color: red;">{{item.price_str}}</text></view>
									</view>
								</view>
								<u-image width="240rpx" height="180rpx" class="img m-l-20" :src="item.image" />
							</view>
							
							<view class="flex row-between m-t-20">
								<view class="xs muted">发布时间: {{item.create_time}}</view>
								<view class="flex">
									<image class="icon-sm" src="/static/images/icon_see.png"></image>
									<view class="m-l-10 xs muted">{{ item.visit }}人浏览</view>
								</view>
							</view>
						</router-link>
					</view>
				</view>
			</mescroll-uni>
		</view>
	</view>
</template>

<script>
	import {
		getResourceList
	} from '@/api/store';
	import {
		mapGetters
	} from 'vuex';
	import {
		loadingType
	} from '@/utils/type';
	import {
		getRect,
		trottle
	} from '@/utils/tools';

	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins";
	export default {
		mixins: [MescrollMixin],
		data() {
			return {
				upOption: {
					auto: false,
					empty: {
						icon: '/static/images/goods_null.png',
						tip: "暂无资料",
					}
				},
				keyword: '',
				status: loadingType.LOADING,
				page: 1,
				sortConfig: {
					goodsType: 'one',
					priceSort: '',
					saleSort: '',
				},
				goodsList: [],
				showHistory: false,
				hotList: [],
				historyList: [],
				newsList:[]
			};
		},

		watch: {
			// 监听属性
			keyword(value, old) {
				if (!value && !this.id) {
					this.showHistory = true
				}
			},
			showHistory(value) {
				if (value) {
					this.getSearchpageFun();
				}
			},
			'sortConfig.saleSort'() {
				this.onSearch()
			},
			'sortConfig.priceSort'() {
				this.onSearch()
			}

		},

		onLoad(options) {
			this.onSearch = trottle(this.onSearch, 500, this);
			this.init(options);

		},

		computed: {
			...mapGetters(['sysInfo'])
		},
		methods: {
			downCallback() {
				this.onRefresh()
			},
			upCallback(page) {
				let pageNum = page.num; // 页码, 默认从1开始
				let pageSize = page.size; // 页长, 默认每页10条
				let {
					goodsList,
					keyword,
					sortConfig: {
						priceSort,
						saleSort,
					}
				} = this;
				
				const params = {
					page_size: pageSize,
					page_no: pageNum,
					keyword
				}
				getResourceList(params).then(({
					data
				}) => {
					if(this.keyword != ""){
						this.historyList.forEach((item, index) => {
							if(item == this.keyword){
								this.historyList.splice(index, 1)
							}
						})
						this.historyList.unshift(this.keyword)
						uni.setStorageSync('history', this.historyList)
						console.log("历史列表",this.historyList)
					}
					
					if (page.num == 1) this.newsList = [];
					let curPageData = data.list;
					let curPageLen = curPageData.length;
					let hasNext = !!data.more;
					this.newsList = this.newsList.concat(curPageData);
					this.mescroll.endSuccess(curPageLen, hasNext);
					
				}).catch(() => {
					this.mescroll.endErr()
				})
			},
			
			onChange(e) {
				this.keyword = e.value
			},


			clearSearchFun() {
				uni.removeStorageSync("history")
				this.historyList  = []
			},



			init(option) {
				const {
					id,
					name,
					type
				} = this.$Route.query
				this.type = type
				if (id) {
					uni.setNavigationBarTitle({
						title: name
					});
					this.id = id;
					this.$nextTick(() => {
						this.onRefresh()
					})
				} else {
					uni.setNavigationBarTitle({
						title: '资料搜索'
					});
					this.showHistory = true
					
				}
			},

			getSearchpageFun() {
				this.historyList = uni.getStorageSync('history') || []
			},

			onClear() {
				if (this.id) {
					this.onRefresh();
				}
			},
			onSearch() {
				this.showHistory = false
				this.$nextTick(() => {
					this.onRefresh()
				})
			},
			onRefresh() {
				this.goodsList = []
				this.mescroll.resetUpScroll();
			},

			onChangeKeyword(item) {
				this.keyword = item
				this.showHistory = false
				this.onRefresh();
			},
		}
	};
</script>
<style lang="scss">
	.news-list {
		.mainnav {
			padding: 16rpx 150rpx;
			background-color: $-color-white;
		    &--item {
		        width: 150rpx;
		        font-size: 32rpx;
		        font-weight: 500;
		        text-align: center;
		        color: #BBBBBB;
		        transition: all .2s linear;
			
		        .new {
		            width: 10rpx;
		            height: 10rpx;
		            border-radius: 50%;
		            display: inline-block;
		            margin-bottom: 24rpx;
		            background-color: $-color-primary;
		        }
		    }
			
		    .active {
		        color: #000000;
		    }
		}
		
		.main {
			.article-list {
				padding-top: 20rpx;
	
				.article-item {
					padding: 20rpx;
					align-items: flex-start;
					&:not(:last-of-type){
						border-bottom: $-solid-border;
					}
				}
			}
		}
	}
	page {
		height: 100%;
		padding: 0;
	}
	.goods-search {
		height: 100%;
		position: relative;
		.header-wrap {
			position: relative;
			z-index: 999;
			.search {
				box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.03);
				position: relative;
				z-index: 1;
			}
		}
		.search-content {
			position: absolute;
			width: 100%;
			height: 100%;
			padding-top: 100rpx;
			z-index: 100;
			.search-words {
				padding-left: 24rpx;
				padding-bottom: 20rpx;
			
				.title {
					padding: 26rpx 0;
				}
			
				.words {
					.item {
						line-height: 52rpx;
						height: 52rpx;
						padding: 0 24rpx;
						background-color: #F5F5F5;
					}
				}
			}
		}

		.content {
			flex: 1;
			min-height: 0;

			.goods-list {
				overflow: hidden;
			}
		}
	}
</style>
