<template>
	<view class="search-container">
		<view class="search-main">
			<view class="gosearch-btn">
				<text class="iconfont icon-sousuo"></text>
				<input class="input" maxlength="20" type="text" value="" confirm-type="search" @confirm="subMitSearch()" placeholder="请输入关键词" v-model.trim="name"/>
				<text class="iconfont icon-shanchu1" v-if="isShowClose" @click.stop="delInputText()"></text>
			</view>
		</view>
		<view  class="commodity-list-wrapper"  :style="{'overflow':'auto','height':(listData.length>4? windowHeight + 'px' : 'auto')}">
			<scroll-view :style="{'height':(listData.length>4? scrollHeight+'px' : 'auto')}" @scrolltolower="scrolltolower" scroll-y v-if="!showEmpty">
				<view class="Listitem" v-for="(item, index) in listData" :key="index" :class="item.sold==1?'activeImg':''"
				 :productid="item.productID" @click.stop="navToDetailPage(item.productID)">
				    <view class="itemImg onhref" >
				        <image :src="item.imageList[0]" class="bigImg"></image>
						<image src="http://static-b.caimei365.com/app/img/icon/yishou.png" class="yishou_bg" v-if="item.sold==1&&item.newAdded==1||item.sold==1&&item.newAdded==0"></image>
				    </view>
				   <view class="ItemInfo">
						<text class="infotag news" v-if="item.sold==0&&item.newAdded==1">新</text>
				        <view class="productname" href="" :style="item.sold==0&&item.newAdded==1?'text-indent:60rpx':''">
				            {{item.name}}
				        </view>
				        <view class="targetprice" >
				            <text v-if="item.detailTalkFlag ==2 && hasLogin==false">价格详聊</text>
				            <text v-else-if="!hasLogin" class="priceparam">登录查看价格</text>
				            <text v-else-if="hasLogin && item.detailTalkFlag==2">价格详聊</text>
				            <text v-else>￥{{item.price1Str}}</text>
				        </view>
						<view class="shijian">
							<text class="iconfont  icon-shijian"></text> {{item.onLineDateStr}}
						</view>
				        <view class="dizhi">
							<view class="dizhi_left">
								<text class="iconfont  icon-dizhi1"></text>
								{{item.provinceCityDistrict}}
							</view>
				            <view class="liulan_right">
								<text class="iconfont  icon-kejian1"></text>
				               {{item.viewingNum}}
				            </view>
				        </view>
				    </view>
				</view>
				<view v-if="showLoading && listData.length > 4">
					<view class="loading-wrapper loading-wrapper-now" v-if="loadingNow">{{loadingText}}<text v-if="loadingText === '已至底部'">‧ ‧ ‧</text></view>
					<view class="loading-wrapper loading-wrapper-btm" v-else>———<text class="btm-text">已至底部</text>———</view>
				</view>
			</scroll-view>
			<view class="empty-container" v-if="showEmpty">
				<image class="empty-container-image" src="https://img.caimei365.com/group1/M00/03/8D/Cmis215XHXWAHCoqAAELHadZ9Xg365.png"></image>
				<text class="error-text">抱歉，没有相关商品！</text>
			</view>
		</view>
		<!-- 透明模态层 -->
		<modal-layer v-if='isModallayer'></modal-layer>
	</view>
</template>

<script>
	import { mapState,mapMutations } from 'vuex'
	import modalLayer from "@/components/modal-layer"
	import uniGrader from '@/components/uni-grade/uni-grade.vue'
	import authorize from '@/common/config/authorize.js'
	
	export default {
		components: {
			modalLayer,
			uniGrader
		},
		data() {
			return {
				shopId:0,
				userID:0,
				themeClass: 'block',
				show:false,
				userIdentity:'',	
				identity:2,
				searchKeyType:1,
				isShowClose:false,	//是否显示清空输入框图标
				isSearchHistory:false,//是都显示搜索历史
				isShowWrapper:false,
				isModallayer:false,
				isFocus:false,
				priceLoading:true,
				windowHeight: '',
				showEmpty: false,
				scrollHeight: '',
				listData: [],
				showLoading: false,
				loadingNow: true,
				loadingText: '上拉加载更多',
				pullFlag: true,
				pageNum: 1,
				pageSize: 10,
				secondHandType:1,
				instrumentType:0,
				name:'',
				total:0
			}
		},
		onLoad(option) {
			this.getListFromServer()
		},
		computed: {
			...mapState(['hasLogin','userInfo'])
		},
		methods:{
			subMitSearch() {//搜索
				if (this.name == '') {
					this.$util.msg('请输入商品关键词',2000);
				}else{
					this.getListFromServer()
				}
			},
			scrolltolower() {
				if(this.total>this.listData.length && this.pullFlag) {
					this.getListFromServer(true);
				}
			},
			getListFromServer(loadMore) {
				this.showLoading = true;
				this.loadingNow = true;
				this.loadingText = '加载中';
				this.showEmpty = false;				
				if(loadMore) {
					this.pageNum += 1;
				}
				let params = {
						secondHandType:1,
						instrumentType:0,
						name:this.name,
						pageNum:this.pageNum,
						pageSize:this.pageSize
				}
				this.SecondService.SeconHandProductList(params).then(response =>{
					this.isShowWrapper = true
					const resData = response.data;
					const resList = resData.results;
					if(resList && resList.length > 0){
						this.total = resData.totalRecord;
						this.showEmpty = false;
						if(loadMore) {
							this.listData = [...this.listData,...resList];
						} else {
							this.listData = [...resList];
						}
						// 防上拉暴滑
						this.pullFlag = false;
						setTimeout(()=>{ this.pullFlag = true; },500)
						// 底部提示文案
						if(this.totalPage>this.listData.length) {
							this.loadingText = '上拉加载更多';
						} else {
							this.showLoading = true;
							this.loadingNow = false;
						}
					} else {
						if(!loadMore) {
							this.showEmpty = true;
						}
					}
				}).catch(error =>{
					this.$util.msg(error.msg,2000);
				})
			},
			delInputText () { //清除输入框内容
				this.name = ''
				this.isShowClose = false
				this.isShowWrapper = false
				this.getListFromServer()
			},
			keywordsClick (item) {//关键词搜索与历史搜索
				this.name = item;
				this.isShowClose = true;
				this.subMitSearch();
			},
			navToDetailPage(id) {
				this.isModallayer = true;
				this.$api.navigateTo(`/second/pages/product/product-details?id=${id}`);
				this.isModallayer = false;
			},
			setScrollHeight() {
				const {windowHeight, pixelRatio} = wx.getSystemInfoSync();
				this.windowHeight = windowHeight - 1;
				this.scrollHeight = windowHeight - 1;
			},
			toLoginPage() {
				let searchLoginType = 'search'
				uni.navigateTo({
					url:`/pages/login/login?type=${searchLoginType}`
				})
			},
			topBubble() {//显隐搜索项
				this.show = !this.show;
			},
		},
		onShow() {
			this.setScrollHeight();
		}
	}
</script>

<style lang="scss">
	@import "@/uni.scss";
	page{
		background-color:#fff;
	}
	.search-main{
		width: 100%;
		height: 88rpx;
		position: fixed;
		top: 0;
		left: 0;
		background: #FFFFFF;
		z-index: 1001;
		box-sizing: border-box;
		padding: 9rpx 0;
		.gosearch-btn{
			width: 702rpx;
			height: 100%;
			border-radius: 40rpx;
			background: #F0F0F0;
			margin: 0 auto;
			padding:0 20rpx;
			font-size: 28rpx;
			line-height: 70rpx;
			padding-left: 70rpx;
			color: #8A8A8A;
			background: #F7F7F7;
			position: relative;
			box-sizing: border-box;
			.icon-sousuo{
				width: 70rpx;
				height: 70rpx;
				line-height: 70rpx;
				text-align: center;
				display: block;
				position: absolute;
				left: 0;
				top: 0;
				font-size: 34rpx;
				color: #8A8A8A;
				z-index: 10;
			}
			.icon-shanchu1{
				font-size: 36rpx;
				color: #8A8A8A;
				position: absolute;
				right: 10rpx;
				top: 0;
				padding: 0 10rpx;
				z-index: 100;
			}
			.input{
				width: 400rpx;
				height: 100%;
				float: left;
				font-size: $font-size-24;
				box-sizing: border-box;
				padding-right: 66rpx;
			}
		}
		.search-btn{
			width: 120rpx;
			height: 70rpx;
			line-height: 70rpx;
			float: right;
			text-align: center;
			color: #666666;
			font-size: $font-size-24;
		}
	}
	.search-container{
		padding-top: 96rpx;
	}
	.s-block{
		background: #FFFFFF;
		.header{
			font-size: 32rpx;
			padding:40rpx 24rpx 22rpx 24rpx;
			line-height: 42rpx;
			font-size: 30rpx;
			font-weight: bold;
			position: relative;
			.icon-shanchu{
				font-size: 36rpx;
				color: #333333;
				float: right;
				padding: 0 10rpx;
				z-index: 10;
				font-weight: normal;
			}
		}
		.list{
			display: flex;
			flex-wrap: wrap;
			padding-bottom: 40rpx;
			view{
				color: #8A8A8A;
				font-size: 24rpx;
				box-sizing: border-box;
				text-align: center;
				height: 48rpx;
				line-height: 48rpx;
				border-radius: 24rpx;
				margin:12rpx;
				padding:.0 30rpx;
				overflow: hidden;
				white-space: nowrap;
				text-overflow: ellipsis;
				background-color: #F3F3F3;
			}
		}
	}
	.commodity-list-wrapper {
		scroll-view {
			height: 100%;
			overflow: scroll;
		}	
		.Listitem{
			height: 202rpx;
			padding: 20rpx;
			border-bottom: 2rpx solid #F0F2F4;
			&.activeImg {
				opacity: 0.5;
			}
			.itemImg {
				width: 180rpx;
				height: 180rpx;
				float: left;
				background: #000;
				position: relative;
				.yishou_bg{
					width: 90rpx;
					height: 90rpx;
					position: absolute;
					top: 50%;
					left: 50%;
					transform: translate(-50%, -50%)
				}
				.bigImg {
					width: 100%;
					height: 100%;
				}
			}
			.ItemInfo {
				float: right;
				width:500rpx;
				.infotag{
					font-size: 20rpx;
					padding: 5rpx 15rpx;
					background: #f94b4b;
					color: #fff;
					position: absolute;
					border-radius: 6rpx;
				}
				.productname{
					display: inline-block;
					font-size: 28rpx;
					color: #333333;
					vertical-align: top;
					text-overflow: -o-ellipsis-lastline;
					overflow: hidden;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-line-clamp: 2;
					line-clamp: 2;
					-webkit-box-orient: vertical;
					width: 500rpx;
				}
				.targetprice {
					font-size: 26rpx;
					color: #f94b4b;
					cursor: pointer;
					margin: 15rpx 0;
				}
				.shijian{
					font-size: 24rpx;
					color: #999999;
					margin-top: 10rpx;
					.icon-shijian{
						font-size: 30rpx;
						color: #999999;
						margin-right: 10rpx;
					}
				}
				.dizhi{
					font-size: 24rpx;
					color: #999999;
					margin-top: 10rpx;
					.dizhi_left{
						float: left;
						.icon-dizhi1{
							font-size: 30rpx;
							color: #999999;
							margin-right: 10rpx;
						}
					}
					.liulan_right{
						float: right;
						margin-right: 25rpx;
						.icon-kejian1{
							font-size: 30rpx;
							color: #999999;
							margin-right: 10rpx;
						}
					}
				}
				.shijian{
					font-size: 24rpx;
					color: #999999;
					margin-top: 10rpx;
				}
			}
		}
	}
	
</style>
