<template>
	<view class="container commodity-list-wrapper" :style="{'overflow':(showSkeleton? 'hidden' : 'auto'),'height': (showSkeleton? windowHeight + 'px' : 'auto')}">
		<list-skeleton v-if="showSkeleton" :listType='0'></list-skeleton>
		<scroll-view :style="{'height':scrollHeight+'px'}" @scrolltolower="toLower" scroll-y v-if="commodityList.length > 0">
			<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-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>
							<text v-else>￥<text class="price-larger">{{item.retailPrice?item.retailPrice.toFixed(2):'0.00'}}</text></text>
						</view>
						<button v-if="fromRegularPurchasePage" class="add-cart-btn" @click.stop="operationHanld(item)">选择数量</button>
					</view>
				</view>
			</view>
			<button class="show-more-btn" v-if="showRegularBtn" @click="getListFromServer(true)">查看更多</button>
			<view v-if="showLoading && commodityList.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 class="cart-icon" v-if="fromRegularPurchasePage" @click="toCartPage">
			<text v-if="cartNum > 0" class="uni-badge uni-badge-error uni-small uni-badge--small icon-num">
				{{cartNum}}
			</text>
			<image src='../../../static/icon-cart-active@3x.png' mode="widthFix"></image>
		</view>
		<view class="empty-container" v-if="showEmpty && !fromRegularPurchasePage">
			<image class="empty-container-image" src="https://img.caimei365.com/group1/M00/03/71/Cmis2F3wna6AY2ZjAABpmnBICH4247.png"></image>
			<text class="error-text">{{emptyText}}</text>
		</view>
		<view class="empty-container" v-if="showEmpty && fromRegularPurchasePage">
			<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>
		<!-- 透明模态层 -->
		<modal-layer v-if='isModallayer'></modal-layer>
	</view>
</template>

<script>
	import listSkeleton from '@/components/cm-module/listTemplate/listSkeleton'
	import modalLayer from "@/components/modal-layer"
	import uniStars from '@/components/uni-stars/uni-stars.vue'
	import { queryGoodslist } from "@/api/product.js"
	import { mapState,mapMutations } from 'vuex';
	export default{
		name:'CommodityList',
		components:{
			listSkeleton,
			modalLayer,
			uniStars
		},
		props: {
			emptyText: {
				type: String
			},
		},
		data(){
			return{
				isModallayer:false,
				windowHeight: '',
				showSkeleton: true,
				showEmpty: false,
				userID: '',
				scrollHeight: '',
				commodityList: [],
				showLoading: false,
				loadingNow: true,
				loadingText: '上拉加载更多',
				pageSize: 10,
				pageNum: 1,
				hasNextPage: false,
				totalPage: 1,
				pullFlag: true,
				fromRegularPurchasePage: false,
				cartNum: 0,
				showRegularBtn: false
			}
		},
		created() {		
			this.setScrollHeight();		
		},
		computed: {
			...mapState(['hasLogin','userInfo'])
		},
		methods:{
			toLoginPage() {
				const {lastPageType, lastPageVal} = this.$parent;
				uni.navigateTo({
					url:`/pages/login/login-accont?listType=${lastPageType}&listVal=${lastPageVal}`
				})
			},
			toLower() {
				// 第一次加载排除常用商品
				if(!this.showRegularBtn) {
					if(this.hasNextPage && this.pullFlag) {
						this.getListFromServer(true);
					}
				}
			},
			setScrollHeight() {
				const {windowHeight, pixelRatio} = wx.getSystemInfoSync();
				this.windowHeight = windowHeight - 1;
				this.scrollHeight = windowHeight - 1;
			},
			getListFromServer(loadMore) {
				this.$api.getStorage().then((resolve) =>{
					this.userID = resolve.userID ? resolve.userID : '';
					this.getQueryGoodslist(loadMore)
				}).catch(error =>{
					this.getQueryGoodslist(loadMore)
				})
			},
			getQueryGoodslist(loadMore){
				const thisServerurl = this.$parent.serverUrl;
				this.showLoading = true;
				this.loadingNow = true;
				this.loadingText = '加载中';
				if(loadMore) {
					this.pageNum += 1;
				}
				if(this.$parent.lastPageType === '再次购买') {
					this.fromRegularPurchasePage = true;
				}
				queryGoodslist(thisServerurl,{userId:this.userID,pageNum:this.pageNum,pageSize:this.pageSize}).then(response=>{
					const resData = this.fromRegularPurchasePage ?response.data.page :response.data;
					const resList = resData.results,
						  getCartNum = response.data.count;
					this.cartNum = getCartNum > 99 ?'99+' :getCartNum;
					if(!loadMore && this.fromRegularPurchasePage && resData.hasNextPage) {
						this.showRegularBtn = true;
					}
					if(resList && resList.length > 0){
						this.hasNextPage = resData.hasNextPage;
						this.totalPage = resData.totalPage;
						if(loadMore) {
							this.commodityList = [...this.commodityList,...resList];
							this.showRegularBtn = false;
						} else {
							this.commodityList = [...resList];
							this.showSkeleton = false;
						}
						// 防上拉暴滑
						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);
				})
			},
			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' 
				})
			}
				
		}
	}
</script>

<style lang="scss">
	.commodity-list-wrapper {
		scroll-view {
			height: 100%;
			border-top: 2rpx solid rgba(0,0,0,0.07);
		}
		.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 {
			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 {
				font-size: 32rpx;
				.txt{
					font-size: $font-size-24;
					display: inline-block;
					line-height: 30rpx;
					text-align: left;
				}
			}
		}
		.add-cart-btn {
			width: 156rpx;
			height: 64rpx;
			line-height: 64rpx;
			border-radius: 32rpx;
			color: #fff;
			font-size: 26rpx;
			margin-right: 0;
			background:linear-gradient(45deg,rgba(255,41,41,1) 0%,rgba(255,109,27,1) 100%);
		}
	}
	.cart-icon {
		width: 92rpx;
		height: 92rpx;
		border-radius: 50%;
		background: rgba(255, 147, 0, 0.5);
		position: fixed;
		right: 24rpx;
		bottom: 30%;
		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>
