<template>
	<view class="search-container">
		<view class="search-main">
			<view class="search-input">
				<text class="iconfont icon-iconfonticonfontsousuo1"></text>
				<input maxlength="20" focus type="text" value="" confirm-type="search" @focus="onFocus" @input="onShowClose" @confirm="subMitSearch()" placeholder="请输入商品关键词" v-model.trim="searchInputVal"/>
				<text class="iconfont icon-shanchu1" v-if="isShowClose" @click="delInputText()"></text>
			</view>
			<view class="search-btn" @click="subMitSearch()">搜索</view>
		</view>
		<view class="search-container-history" v-if="!isShowWrapper">
			<view :class="'s-' + themeClass" v-if="serachRecordList.length>0">
				<view class="header">
					搜索历史
					<text class="iconfont icon-shanchu" @click="confirmDetele"></text>
				</view>
				<view class="list">
					<view v-for="(item,index) in serachRecordList" :key="index" @click="keywordsClick(item.searchWord)">{{item.searchWord}}</view>
				</view>
			</view>
		</view>
		<!-- <go-search :theme="themeClass" @subMitSearch="subMitSearch"></go-search> -->
		<view v-else class="commodity-list-wrapper"  :style="{'overflow':'auto','height':(commodityList.length>4? windowHeight + 'px' : 'auto')}">
			<scroll-view :style="{'height':(commodityList.length>4? scrollHeight+'px' : 'auto')}" @scrolltolower="scrolltolower" scroll-y v-if="!showEmpty">
				<view v-for="(item,index) in commodityList" :key="index" :id="item.id" class="all-type-list-content commodity-list" @click.stop="navToDetailPage(item.productID)">
					<image mode='widthFix' :src="item.mainImage"  class="list-img" alt="list-img"></image>
					<view class="list-details-info">
						<text class="list-details-title">{{item.name}}</text>
						<text class="list-details-specs">规格：{{item.unit}}</text>
						<text class="list-details-miniQuantity" v-if="fromRegularPurchasePage">起订量：{{item.minBuyNumber}}</text>
						<view class="list-details-specs" v-if="item.productCode!=''&&item.productCode!=null">
							<view>商品编码：{{item.productCode}}</view>
						</view>
						<view class="list-details-price">
							<view v-if="!hasLogin" class="list-login-now">
								<text class="p-no">价格:</text>
								<uni-stars :stars="parseInt(item.price1Grade)" :font-size='36' :width-info="180"></uni-stars>
							</view>
							<view class="list-price" v-else>
								<view class="price-larger" v-if="item.price1TextFlag == '1'">
									<text class="txt">该未公开价格</text>
								</view>
								<view class="price-larger" v-if="item.price1TextFlag == '2'">
									<text class="txt">价格仅会员可见</text>
									<text class="btn" @click.stop="this.$api.navigateTo(`/pages/login/apply?clubStatus=${clubStatus}`)">去升级</text>
								</view>
								<text v-if="item.price1TextFlag == '0'">￥<text class="price-larger">{{item.retailPrice?item.retailPrice.toFixed(2):'0.00'}}</text></text>
							</view>
						</view>
					</view>
				</view>
				<view v-if="showLoading && commodityList.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 goSearch from '@/components/uni-search/go-search.vue'
	import modalLayer from "@/components/modal-layer"
	import uniStars from '@/components/uni-stars/uni-stars.vue'
	import authorize from '@/common/config/authorize.js'
	import { querySearchProduct , querySearchHistory,clearSearchHistory } from "@/api/product.js" 
	
	export default {
		components: {
			// goSearch,
			modalLayer,
			uniStars
		},
		data() {
			return {
				userID:'',
				themeClass: 'block',
				searchInputVal:'',							//搜索关键词
				isShowClose:false,							//是否显示清空输入框图标
				isSearchHistory:false,						//是都显示搜索历史
				serachRecordList:[],						//历史搜索记录
				isShowWrapper:false,
				isModallayer:false,
				windowHeight: '',
				showEmpty: false,
				scrollHeight: '',
				commodityList: [],
				showLoading: false,
				loadingNow: true,
				loadingText: '上拉加载更多',
				pageSize: 10,
				pageNum: 1,
				hasNextPage: false,
				totalPage: 1,
				pullFlag: true,
			}
		},
		onLoad() {
			this.initGetSerachRecord()			
		},
		computed: {
			...mapState(['hasLogin','userInfo'])
		},
		methods:{
			initGetSerachRecord(){
				this.$api.getStorage().then((resolve) =>{
					this.userID = resolve.userID ? resolve.userID : '';
				    querySearchHistory({userId:this.userID}).then(response =>{
						if(response.code == 0){
							this.serachRecordList = response.data
						}
				    })
				})	
			},
			subMitSearch() {
				if (this.searchInputVal == '') {
					this.$util.msg('请输入商品关键词',2000);
				}else{
					this.commodityList =[]
					this.getListFromServer(false)
				}
			},
			scrolltolower() {
				if(this.hasNextPage && 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 = {userId:this.userID,searchWord:this.searchInputVal,pageNum:this.pageNum,pageSize:this.pageSize}
				querySearchProduct(params).then(response =>{
					this.isShowWrapper = true
					const resData = response.data;
					const resList = resData.results;
					if(resList && resList.length > 0){
						this.hasNextPage = resData.hasNextPage;
						this.totalPage = resData.totalPage;
						this.showEmpty = false;
						if(loadMore) {
							this.commodityList = [...this.commodityList,...resList];
						} else {
							this.commodityList = [...resList];
						}
						// 防上拉暴滑
						this.pullFlag = false;
						setTimeout(()=>{
							this.pullFlag = true;
						},500)
						// 底部提示文案
						if(this.hasNextPage) {
							this.loadingText = '上拉加载更多';
						} else {
							this.showLoading = true;
							this.loadingNow = false;
						}
					} else {
						if(!loadMore) {
							this.showEmpty = true;
						}
					}
				}).catch(error =>{
					this.$util.msg(error.msg,2000);
				})
			},
			onShowClose () {//输入框失去焦点时触发
				this.inputEmpty(this.searchInputVal)
			},
			onFocus () { //输入框获取焦点时触发
				this.inputEmpty(this.searchInputVal)
				this.initGetSerachRecord()
			},
			delInputText () { //清除输入框内容
				this.searchInputVal = ''
				this.isShowClose = false
				this.isShowWrapper = false
				this.inputEmpty(this.searchInputVal)
				this.initGetSerachRecord()
			},
			keywordsClick (item) {//关键词搜索与历史搜索
				this.searchInputVal = item;
				this.isShowClose = true;
				this.subMitSearch();
			},
			confirmDetele() {//清空历史记录
				this.$util.modal('提示','确定删除历史记录？','确定','取消',true,() =>{
					clearSearchHistory({userId:this.userID}).then(response =>{
						this.$util.msg('删除成功',2000,true,'success')
						this.serachRecordList=[];
					}).catch(error =>{
						this.$util.msg(error.msg,2000)
					})
				})
			},
			inputEmpty(val){
				this.isShowWrapper = false
				if(val != ''){
					this.isShowClose = true
				}else{					
					this.isShowClose = false
				}
			},
			navToDetailPage(id) {
				this.isModallayer = true;
				this.$api.navigateTo(`/pages/goods/product?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}`
				})
			}
		},
		onShow() {
			this.setScrollHeight();
		}
	}
</script>

<style lang="scss">
	@import "@/uni.scss";
	page{
		background-color: #F7F7F7 !important;
	}
	.search-main{
		width: 702rpx;
		height: 70rpx;
		padding: 12rpx 24rpx;
		border-bottom: 1px solid #F0F0F0;
		position: fixed;
		top: 0;
		left: 0;
		background: #FFFFFF;
		z-index: 1001;
		.search-input{
			width: 448rpx;
			height: 70rpx;
			padding: 0 68rpx;
			line-height: 70rpx;
			border-radius: 40rpx;
			position: relative;
			background: #F0F0F0;
			float: left;
			.icon-iconfonticonfontsousuo1{
				font-size: 36rpx;
				color: #8A8A8A;
				position: absolute;
				left: 24rpx;
				z-index: 10;
			}
			.icon-shanchu1{
				font-size: 36rpx;
				color: #8A8A8A;
				position: absolute;
				right: 24rpx;
				top: 0;
				padding: 0 10rpx;
				z-index: 10;
			}
			input{
				width: 448rpx;
				height: 70rpx;
				background-color: #F0F0F0;
				font-size: 26rpx;
			}
		}
		.search-btn{
			width: 118rpx;
			height: 70rpx;
			line-height: 70rpx;
			color: $color-system;
			font-size: 30rpx;
			text-align: center;
			float: left;
		}
		.voice-icon{
			width: 36rpx;
			height: 36rpx;
			padding: 16rpx 20rpx 16rpx 0;
			position: absolute;
			left: 16rpx;
			top: 4rpx;
			z-index: 10;
		}
		
	}
	.search-container{
		padding-top: 106rpx;
	}
	.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;
			}
		}
	}
	.s-circle{
		margin-top: 30rpx;
		.header{
			font-size: 32rpx;
			padding: 30rpx;
			border-bottom: 2rpx solid #F9F9F9;
			position: relative;
			image{
				width: 36rpx;
				height: 36rpx;
				padding: 10rpx;
				position: absolute;
				right: 40rpx;
				top: 24rpx;
			}
		}
		.list{
			display: flex;
			flex-wrap: wrap;
			padding: 0 30rpx 20rpx;
			view{
				padding: 8rpx 30rpx;
				margin: 20rpx 30rpx 0 0;
				font-size: 28rpx;
				color: #8A8A8A;
				background-color: #F7F7F7;
				box-sizing: border-box;
				text-align: center;
				border-radius: 20rpx;
			}
		}
	}
	.wanted-block{
		margin-top: 30rpx;
		.header{
			font-size: 32rpx;
			padding: 30rpx;
		}
		.list{
			display: flex;
			flex-wrap: wrap;
			view{
				width: 50%;
				color: #8A8A8A;
				font-size: 28rpx;
				box-sizing: border-box;
				text-align: center;
				padding: 20rpx 0;
				border-top: 2rpx solid #FFF;
				border-left: 2rpx solid #FFF;
				background-color: #F7F7F7;
				overflow: hidden;
				white-space: nowrap;
				text-overflow: ellipsis;
			}
		}
	}
	.wanted-circle{
		margin-top: 30rpx;
		.header{
			font-size: 32rpx;
			padding: 30rpx;
		}
		.list{
			display: flex;
			flex-wrap: wrap;
			padding: 0 30rpx 20rpx;
			view{
				padding: 8rpx 30rpx;
				margin: 20rpx 30rpx 0 0;
				font-size: 28rpx;
				color: #8A8A8A;
				background-color: #F7F7F7;
				box-sizing: border-box;
				text-align: center;
				border-radius: 20rpx;
			}
		}
	}
	.commodity-list-wrapper {
		scroll-view {
			height: 100%;
			overflow: scroll;
		}	
		.empty-container-image {
			margin-top: -300rpx;
		}
		.toIndexPage {
			bottom: 390rpx;
		}
		.show-more-btn {
			width: 276rpx;
			height: 52rpx;
			line-height: 52rpx;
			border: 2rpx solid #D8D8D8;
			background: #F7F7F7;
			font-size: 26rpx;
			margin: 26rpx 0;
			position: absolute;
			left: 50%;
			margin-left: -138rpx;
		}
	}
	.all-type-list-content {
		height: 216rpx;
		padding: 24rpx;
		background: #fff;
		margin-bottom: 2rpx;
		display: flex;
		flex-direction: row;
		box-sizing: content-box;
		.list-img {
			width: 210rpx;
			height: 218rpx !important;
			margin-right: 26rpx;
			border-radius: 10rpx;
			border: 2rpx solid #f3f3f3;
		}
	}
	.list-details-info {
		width: 466rpx;
		display: flex;
		flex-direction: column;
		font-size: 26rpx;
		position: relative;
		.list-details-title {
			line-height: 38rpx;
			text-overflow: ellipsis;
			overflow: hidden;
			display: -webkit-box;
			-webkit-line-clamp: 2;
			line-clamp: 2;
			-webkit-box-orient: vertical;
		}
		.list-details-specs {
			margin-top: 8rpx;
			color: #666666;
		}
		.list-details-miniQuantity {
			margin-top: 7rpx;
		}
	}
	.list-details-price {
		width: 100%;
		flex-direction: row;
		justify-content: space-between;
		position: absolute;
		bottom: 0;
		right: 0;
		.price-icon {
			width: 22rpx;
			height: 28rpx;
			vertical-align: middle;
			margin-right: 10rpx;
		}
		.price-icon + text {
			font-size: 25rpx;
			vertical-align: middle;
		}
		.list-login-now {
			width: 375rpx;
			color: #F8C499;
			position: absolute;
			bottom: 0;
			.p-no{
				float: left;
				font-size: $font-size-24;
				color: $color-system;
				margin-right: 10rpx;
			}
		}
		.login-now {
			padding: 10rpx 10rpx 10rpx 0;
		}
		.list-price {
			color: #FF2A2A;
			.price-larger {
				width: 100%;
				font-size: 32rpx;
				.txt{
					font-size: $font-size-24;
					display: inline-block;
					line-height: 44rpx;
					text-align: left;
					float: left;
				}
				.btn{
					width: 112rpx;
					height: 44rpx;
					display: inline-block;
					float: right;
					background: $btn-confirm;
					line-height: 44rpx;
					text-align: center;
					color: #FFFFFF;
					font-size: $font-size-24;
					border-radius: 22rpx;
				}
			}
		}
	}
</style>
