<template>
	<view class="container commodity-list-wrapper" :style="{'overflow':(showSkeleton? 'hidden' : 'auto'),'height': (showSkeleton? windowHeight + 'px' : 'auto')}">
		<list-skeleton v-if="showSkeleton"></list-skeleton>
		<view class="product-container" v-if="!isShowEmpty" :style="{paddingTop:searchStatus?'112rpx':''}">
			<scroll-view :style="{'height':scrollHeight+'px'}" @scrolltolower="scrolltolower" scroll-y v-if="productList.length > 0">
				<view v-for="(item,index) in productList" :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">起订量：{{item.minBuyNumber}}</text>
						<view class="list-details-price" v-if="item.price1TextFlag == 1">
							<view class="list-none">
								<view class="price-small">该商品尚未公开价格</view>
								<view class="price-small">购买请致电：0755-22907771</view>
							</view>
						</view>
						<view class="list-details-price" v-else>
							<view class="list-shop">
								<view class="list-price" >
									<text class="price-view activity" v-if="item.isShowActFlg">活动价</text>
									<text class="price-view ladder" v-if="item.ladderPriceFlag == 1">阶梯价</text>
									<text class="price-larger">￥{{item.retailPrice.toFixed(2)}}</text>
								</view>
								<view class="list-price-none" v-if="item.repurchasePriceState">
									<text class="price-none">￥{{item.discountPrice}}</text>
									<text class="iconfont icon-wenhao" @click.stop="repurchModel"></text>
								</view>
							</view>
							<button class="add-cart-btn" @click.stop="operationHanld(item)">选择数量</button>
						</view>
					</view>
				</view>
				<view v-if="showLoading && productList.length > 4 && !showRegularBtn">
					<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>
		<view class="empty-container" v-else>
			<image class="empty-container-image" src="https://img.caimei365.com/group1/M00/03/71/Cmis2F3wna6AWdWzAAGlgAP0das422.png" mode="aspectFit"></image>
			<text class="error-text">您还没有购买过任何商品哟~</text>
			<button class="submit-btn toIndexPage" @click="toIndexPage">去逛逛</button>
		</view>
		<view class="cart-icon" v-if="!isShowEmpty" @click="toCartPage">
			<text v-if="cartQuantity > 0" class="uni-badge uni-badge-error uni-small uni-badge--small icon-num">
				{{cartQuantity >= 100 ? '99+' : cartQuantity}}
			</text>
			<image src='../../../static/icon-cart-active@3x.png' mode="widthFix"></image>
		</view>
		<!-- 透明模态层 -->
		<modal-layer v-if='isModallayer'></modal-layer>
	</view>
</template>

<script>
	import listSkeleton from '@/components/module/listTemplate/listSkeleton'
	import modalLayer from "@/components/modal-layer"
	import uniStars from '@/components/uni-stars/uni-stars.vue'
	import { queryAgaingoodslist } from "@/api/product.js"
	import { mapState,mapMutations } from 'vuex';
	export default{
		name:'productList',
		components:{
			listSkeleton,
			modalLayer,
			uniStars
		},
		data(){
			return{
				isShowClose:false,
				searchInputVal:'',
				isModallayer:false,
				windowHeight: '',
				showSkeleton: true,
				isShowEmpty: false,
				userID: '',
				scrollHeight: '',
				productList: [],
				showLoading: false,
				loadingNow: true,
				loadingText: '上拉加载更多',
				pageSize: 10,
				pageNum: 1,
				hasNextPage: false,
				pullFlag: true,
				fromRegularPurchasePage: false,
				cartQuantity: 0,
				showRegularBtn: false,
				isPrecedence:false
			}
		},
		created() {
			this.setScrollHeight();		
			this.$api.getStorage().then((resolve) =>{
				this.userID = resolve.userID
				this.getProductAgainInfo()
			})
		},
		computed: {
			...mapState(['hasLogin','userInfo'])
		},
		methods:{
			scrolltolower() {
				if(this.hasNextPage && this.pullFlag) {
					this.getProductAgainInfo(true);
				}
			},
			setScrollHeight() {
				const {windowHeight, pixelRatio} = wx.getSystemInfoSync();
				this.windowHeight = windowHeight - 1;
				this.scrollHeight = windowHeight - 1;
			},
			getProductAgainInfo(loadMore) {
				this.showLoading = true;
				this.loadingNow = true;
				this.loadingText = '加载中';
				this.isShowEmpty = false;				
				if(loadMore) {this.pageNum += 1;}
				let params = {userId:this.userID,pageNum:this.pageNum,pageSize:this.pageSize}
				queryAgaingoodslist(params).then(response =>{
					this.isShowWrapper = true
					this.cartQuantity = response.data.cartQuantity
					const responseData = response.data.pageDate;
					if(responseData.results && responseData.results.length > 0){
						this.hasNextPage = responseData.hasNextPage;
						this.isShowEmpty = false;
						if(loadMore) {
							this.productList = [...this.productList,...responseData.results];
						} else {
							this.productList = [...responseData.results];
							this.showSkeleton = false;
						}
						//价格显示处理
						let isActFlg,newProductList=[];
						this.productList.map((item, index)=> {
							if(item.actFlag == 1){
								isActFlg = true
							}else if(item.actFlag == 1 && item.ladderPriceFlag == 1){
								isActFlg = true
							}else{
								isActFlg = false
							}
							newProductList.push(Object.assign({},item,{isShowActFlg:isActFlg}))
						})
						this.productList = newProductList
						// 防上拉暴滑
						this.pullFlag = false;
						setTimeout(()=>{
							this.pullFlag = true;
						},500)
						// 底部提示文案
						if(this.hasNextPage) {
							this.loadingText = '上拉加载更多';
						} else {
							this.showLoading = true;
							this.loadingNow = false;
						}
					} else {
						if(!loadMore) {
							this.isShowEmpty = true;
						}
					}
				}).catch(response =>{
					this.$util.msg(response.msg,3000);
				})
			},
			operationHanld(prop){
				this.$emit('operationConfim',prop)
			},
			navToDetailPage(id) {
				this.isModallayer = true;
				this.$api.navigateTo(`/pages/goods/product?id=${id}`);
				this.isModallayer = false;
			},
			toCartPage() {
				uni.switchTab({
					url: '/pages/tabBar/cart/cart'
				})
			},
			toIndexPage() {
				uni.switchTab({
					url: '/pages/tabBar/home/home' 
				})
			},
			repurchModel(){
				this.$util.modal('','此商品成本有变化原来的购买价已不适用','知道了','',false,() =>{})
			},
			onShowClose () {//输入框失去焦点时触发
				if(this.searchInputVal != ''){
					this.isShowClose = true
				}else{					
					this.isShowClose = false
				}
			},
			delInputText(){//清除输入框内容
				this.searchInputVal = ''
				this.isShowClose = false
			}
		}
	}
</script>

<style lang="scss">
	.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: 240rpx;
		padding: 24rpx;
		background: #fff;
		margin-bottom: 2rpx;
		display: flex;
		flex-direction: row;
		box-sizing: content-box;
		.list-img {
			width: 240rpx;
			height: 240rpx !important;
			margin-right: 26rpx;
			border-radius: 10rpx;
			border: 2rpx solid #f3f3f3;
		}
	}
	.list-details-info {
		width: 442rpx;
		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 {
			width: 100%;
			display: inline-block;
			margin-top: 8rpx;
			color: #999999;
		}
		.list-details-miniQuantity {
			width: 100%;
			display: inline-block;
			margin-top: 7rpx;
		}
	}
	.list-details-price {
		width: 100%;
		display: flex;
		flex-direction: row;
		justify-content: space-between;		
		.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-none{
			margin-top: 30rpx;
			.price-small{
				font-size:$font-size-24;
				line-height: 40rpx;
				color: #FF2A2A;
			}
		}
		
		.list-shop{
			width: 100%;
			height: auto;
			flex: 6;
			.list-price {
				width: 100%;
				color: #FF2A2A;
				float: left;
				line-height:36rpx ;
				align-items: center;
				justify-content: center;
				.price-larger {
					margin-top: 20rpx;
					font-size: $font-size-30;
					display: inline-block;
				}
				.price-view{
					display: inline-block;
					width: 78rpx;
					border-radius: 18rpx;
					font-size: $font-size-20;
					text-align: center;
					color: #FFFFFF;
					height: 36rpx;
					line-height: 36rpx;
					margin-right: 8rpx;
					&.ladder{
						background: linear-gradient(135deg,rgba(255,0,0,1) 0%,rgba(242,143,49,1) 100%);
					}
					&.activity{
						background: linear-gradient(135deg,rgba(128,0,255,1) 0%,rgba(242,49,153,1) 100%);
					}
				}
			}
			.list-price-none{
				width: 100%;
				.price-none{
					text-decoration: line-through;
					color: #999999;
					display: inline-block;
				}
				.icon-wenhao{
					font-size: $font-size-32;
					color: #0091FF;
					margin-left: 6rpx;
				}
			}
		}
		.add-cart-btn {
			flex: 4;
			width: 156rpx;
			height: 64rpx;
			line-height: 64rpx;
			border-radius: 32rpx;
			color: #fff;
			font-size: 26rpx;
			margin-right: 0;
			background:linear-gradient(135deg,rgba(242,143,49,1) 0%,rgba(225,86,22,1) 100%);
		}
	}
	.cart-icon {
		width: 92rpx;
		height: 92rpx;
		border-radius: 50%;
		background: rgba(255, 147, 0, 0.5);
		position: fixed;
		right: 24rpx;
		bottom: 28%;
		display: flex;
		align-items: center;
		justify-content: center;
		cursor: pointer;
		image {
			width: 58rpx;
			height: 58rpx;
		}
		text {
			font-size: 28rpx;
			position: absolute;
			top: -10rpx;
			right: 0;
		}
	}
</style>
