<template>
	<view class="search-container">
		<go-search :theme="themeClass" @getSearchText="getSearchText"></go-search>
		<view class="container commodity-list-wrapper" v-if="isShowWrapper" :style="{'overflow':'auto','height': 'auto'}">
			<scroll-view :style="{'height':scrollHeight+'px'}" @scrolltolower="toLower" 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.id)">
					<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-price">
							<view v-if="!loginStatus" class="list-login-now">
								<text @click.stop="toLoginPage" class="login-now">登录查看价格<text class="iconfont icon-xiayibu" style="font-size: 22rpx;margin: 0 6rpx;"></text></text>
							</view>
							<view class="list-price" v-else>
								<text>￥<text class="price-larger">{{item.retailPrice.toFixed(2)}}</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>
			<!-- 透明模态层 -->
			<modal-layer v-if='isModallayer'></modal-layer>
		</view>
	</view>
</template>

<script>
	import goSearch from '@/components/uni-search/go-search.vue'
	import modalLayer from "@/components/modal-layer"
	import authorize from '@/config/authorize.js'
	export default {
		components: {
			goSearch,
			modalLayer
		},
		data() {
			return {
				themeClass: 'block',
				searchInputVal:'',
				isShowWrapper:false,
				isModallayer:false,
				windowHeight: '',
				showSkeleton: true,
				showEmpty: false,
				userID: '',
				loginStatus: true,
				scrollHeight: '',
				commodityList: [],
				showLoading: false,
				loadingNow: true,
				loadingText: '上拉加载更多',
				pageSize: 10,
				pageNum: 1,
				hasNextPage: false,
				totalPage: 1,
				pullFlag: true,
			}
		},
		created() {
			let self = this;
			self.$api.getStorage().then((resolve) =>{
				self.userID = resolve.userID
			})
			self.setScrollHeight();
			self.$api.loginStatus().then((resolveData) => {
				self.loginStatus = resolveData;
			});
		},
		onLoad() {
			
		},
		methods:{
			getSearchText(e) {
				this.searchInputVal = e
				this.getListFromServer(false)
			},
			toLoginPage() {
				const {lastPageType, lastPageVal} = this.$parent;
				uni.navigateTo({
					url:`/pages/login/login?listType=${lastPageType}&listVal=${lastPageVal}`
				})
			},
			toLower() {
				// 第一次加载排除常用商品
				if(this.hasNextPage && this.pullFlag) {
					this.getListFromServer(true);
				}
			},
			setScrollHeight() {
				const {windowHeight, pixelRatio} = wx.getSystemInfoSync();
				this.windowHeight = windowHeight - 1;
				this.scrollHeight = windowHeight - 1;
			},
			getListFromServer(loadMore) {
				let self = this;
				self.showLoading = true;
				self.loadingNow = true;
				self.loadingText = '加载中';
				self.showEmpty = false;				
				if(loadMore) {
					self.pageNum += 1;
				}
				authorize.getCode('weixin').then(wechatcode =>{
					let params = {code:wechatcode,searchWord:self.searchInputVal,index:self.pageNum,pageSize:self.pageSize,organizeID:self.userOrganizeID}
					self.$api.lodingGet('/search/product',params,
						response => {
							if(response.code == '1') {
								this.isShowWrapper = true
								const resData = response.data;
								const resList = resData.results;
								if(resList && resList.length > 0){
									self.hasNextPage = resData.hasNextPage;
									self.totalPage = resData.totalPage;
									self.showEmpty = false;
									if(loadMore) {
										self.commodityList = [...self.commodityList,...resList];
									} else {
										self.commodityList =[];
										self.commodityList = [...resList];
										self.showSkeleton = false;
									}
									console.log(self.commodityList)
									// 防上拉暴滑
									self.pullFlag = false;
									setTimeout(()=>{
										self.pullFlag = true;
									},500)
									// 底部提示文案
									if(self.hasNextPage) {
										self.loadingText = '上拉加载更多';
									} else {
										self.showLoading = true;
										self.loadingNow = false;
									}
								} else {
									if(!loadMore) {
										self.showEmpty = true;
									}
								}
							} else {
								this.$util.msg(response.msg,3000);
							}
						}
					)
				})
			},
			navToDetailPage(id) {
				this.isModallayer = true;
				this.$api.navigateTo(`/pages/goods/product?id=${id}`);
				this.isModallayer = false;
			}
		}
	}
</script>

<style lang="scss">
	page{
		background-color: #F7F7F7 !important;
	}
	.commodity-list-wrapper {
		scroll-view {
			height: 100%;
		}
		.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: #999999;
		}
		.list-details-miniQuantity {
			margin-top: 7rpx;
		}
	}
	.list-details-price {
		width: 100%;
		display: flex;
		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 {
			color: #F8C499;
			position: absolute;
			bottom: 0;
		}
		.login-now {
			padding: 10rpx 10rpx 10rpx 0;
		}
		.list-price {
			color: #FF2A2A;
			.price-larger {
				font-size: 32rpx;
			}
		}
	}
</style>
