<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>
		<view class="product-container" v-if="!isShowEmpty">
			<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 !=null ? item.unit : ''}}</text>
						<text class="list-details-miniQuantity">起订量：{{ item.ladderPriceFlag == '1' ? item.maxBuyNumber : item.minBuyNumber}}</text>
						<view class="list-details-price" v-if="item.price1TextFlag == 1">
							<view class="list-none"><view class="price-small">未公开价格</view></view>
						</view>
						<view class="list-details-price" v-else>
							<view class="list-shop">
								<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 class="list-price" v-else>
									<text class="price-larger" :class="PromotionsFormat(item.promotions) ? 'none' : ''">
										￥{{ (PromotionsFormat(item.promotions) ? item.price1 : item.retailPrice) | NumFormat }}
									</text>
								</view>
							</view>
							<button class="add-cart-btn" @click.stop="operationHanld(item)">数量</button>
						</view>
						<view class="list-details-price" v-if="item.actStatus==1">
							<view class="floor-item-act">
								<view class="floor-tags" v-if="PromotionsFormat(item.promotions)">
									{{item.promotions.name}}<text v-if="item.price1TextFlag != 1">:￥{{ item.retailPrice | NumFormat }}</text>
								</view>
								<view class="floor-tags" v-else>{{item.promotions.name}}</view>	
							</view>
							<view class="floor-item-act" v-if="item.actStatus ==0  &&  item.ladderPriceFlag==1">
								<view class="floor-tags">阶梯价格</view>	
							</view>
						</view>	
					</view>
				</view>
				<button class="show-more-btn" v-if="showRegularBtn" @click="getListFromServer(true)">查看更多</button>
				<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>
		<!-- 可拖动悬浮按钮 -->
		<cm-drag v-if="!isShowEmpty"
				 :cartNum="cartQuantity" 
				 :isDock="true"
				 :existTabBar="true" 
				 @btnClick="btnClick" 
				 @btnTouchstart="btnTouchstart" 
				 @btnTouchend="btnTouchend">
		</cm-drag>		
		<!-- 透明模态层 -->
		<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 cmDrag from '@/components/cm-custom/cm-drag.vue'
	import { queryAgaingoodslist } from "@/api/product.js"
	import { mapState,mapMutations } from 'vuex';
	export default{
		name:'productList',
		components:{
			listSkeleton,
			modalLayer,
			uniStars,
			cmDrag
		},
		props: {
			emptyText: {
				type: String
			},
		},
		data(){
			return{
				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()
			})
		},
		filters: {
			NumFormat:function(text) {//处理金额
				return Number(text).toFixed(2);
			},
		},
		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.actStatus == 1){
								isActFlg = true
							}else if(item.actStatus == 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,2000);
				})
			},
			operationHanld(prop){
				this.$emit('operationConfim',prop)
			},
			navToDetailPage(id) {
				this.isModallayer = true;
				this.$api.navigateTo(`/pages/goods/product?id=${id}`);
				this.isModallayer = false;
			},
			toIndexPage() {
				uni.switchTab({
					url: '/pages/tabBar/home/index' 
				})
			},
			repurchModel(){
				this.$util.modal('','此商品的价格有变化,原来的购买价已不适用','知道了','',false,() =>{})
			},
			PromotionsFormat(promo){//促销活动类型数据处理
				if(promo!=null){
					if(promo.type == 1 && promo.mode == 1){
						return true
					}else{
						return false
					}
				}
				return false
			},
			btnClick() {
				this.$api.navigateTo('/pages/goods/cart')
			},
			btnTouchstart() {
				// console.log('btnTouchstart');
			},
			btnTouchend() {
				// console.log('btnTouchend');
			}
		}
	}
</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: auto;
		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%;
		line-height: 54rpx;
		float: left;
		.floor-item-act{
			height: 54rpx;
			text-align: center;
			box-sizing: border-box;
			float: left;
			.floor-tags{
				float: left;
				height: 36rpx;
				border-radius: 4rpx;
				background-color: rgba(225, 86, 22, 0.1);
				line-height: 36rpx;
				color: $color-system;
				text-align: center;
				display: inline-block;
				padding:0 16rpx;
				margin-top: 9rpx;
				font-size: $font-size-20;
			}
		}
		.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{
			height: auto;
			float: left;
			.list-price {
				width: 100%;
				color: #FF2A2A;
				float: left;
				line-height:54rpx ;
				align-items: center;
				justify-content: center;
				.price-larger {
					font-size: $font-size-30;
					display: inline-block;
					&.none{
						text-decoration: line-through;
						color: #999999;
					}
				}
			}
			.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 {
			float: right;
			width: 140rpx;
			height: 54rpx;
			line-height: 54rpx;
			border-radius: 27rpx;
			color: #fff;
			font-size: 24rpx;
			margin-right: 0;
			background:#FFFFFF;
			border: 1px solid #C9C9C9;
			color: $text-color;
		}
	}
</style>
