<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
			class="product-container"
			@scrolltolower="scrolltolower"
			scroll-y
			v-if="!isShowEmpty && productList.length > 0"
		>
			<view
				v-for="(item, index) in productList"
				:key="index"
				:id="item.id"
				class="all-type-list-content commodity-list goods-item"
				@click.stop="navToDetailPage(item.productId)"
			>
				<image
					mode="widthFix"
					:src="item.mainImage"
					class="list-img"
					alt="list-img"
					lazy-load
				></image>
				<view class="list-details-info">
					<!-- 商品名称 -->
					<text class="list-details-title">{{ item.productName }}</text>
					
					<!-- <text class="list-details-specs">商品编码：{{ item.productCode != null ? item.productCode : '' }}</text> -->
					<!-- 价格 -->
					<view class="price-box">
						<view class="list-details-price prom">
							<view class="floor-item-act" v-if="item.actStatus == 1">
								<view class="floor-tags" v-if="PromotionsFormat(item.promotion)">
									{{ item.promotion.name}}
									<text>:￥{{ item.retailPrice | NumFormat }}</text>
								</view>
								<view class="floor-tags" v-else>{{ item.promotion.name }}</view>
							</view>
							<view class="floor-item-act" v-if="item.actStatus == 0 && item.ladderPriceFlag == 1">
								<view class="floor-tags">阶梯价格</view>
							</view>
						</view>
						<text class="list-details-miniQuantity">起订量：{{ item.minBuyNumber }}</text>
						<text class="list-details-specs">规格：{{ item.unit != null ? item.unit : '' }}</text>
						<view class="list-details-price">
							<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.promotion) ? 'none' : ''">
										￥{{(PromotionsFormat(item.promotion) ? item.price : item.retailPrice) | NumFormat }}
									</text>
								</view>
							</view>
							<button class="add-cart-btn" @click.stop="operationHanld(item)">
								购买
							</button>
						</view>
					</view>
				</view>
			</view>
			<!--加载loadding-->
			<tui-loadmore :visible="loadingNow" :index="3" text="加载中..."></tui-loadmore>
			<tui-nomore :visible="!loadingNow" :text="hasNextPage?'上拉加载更多':'没有更多了'"></tui-nomore>
			<!--加载loadding END-->
		</scroll-view>
		<!-- 商品列表为空时 -->
		<view class="empty-container" v-else>
			<image
				class="empty-container-image"
				src="https://static.caimei365.com/app/wisa/img/icon/icon-product-empty.png"
				mode="widthFix"
			></image>
			<text class="error-text">商品列表空空如也~</text>
		</view>
		<!-- 可拖动悬浮按钮 -->
		<cm-drag
			: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/cm-module/modal-layer/modal-layer'
import cmDrag from '@/components/cm-module/cm-drag/cm-drag'
import { mapState, mapMutations } from 'vuex'
export default {
	name: 'productList',
	components: {
		listSkeleton,
		modalLayer,
		cmDrag,
	},
	props: {
		emptyText: {
			type: String
		}
	},
	data() {
		return {
			isModallayer: false,
			windowHeight: '',
			// 是否显示骨架
			showSkeleton: false,
			isShowEmpty: false,
			scrollHeight: '',
			productList: [],
			loadingNow: false,
			pageSize: 10,
			pageNum: 1,
			hasNextPage: false,
			pullFlag: true,
			cartQuantity: 0,
			showRegularBtn: true,
			isPrecedence: false,
			searchVal: '', //搜索框
			organizeId: '',
			userId: ''
		}
	},
	computed: {
		...mapState(['hasLogin', 'userInfo', 'identity'])
	},
	async created() {
		// 获取采购员组织id
		const userInfo = uni.getStorageSync('userInfo')
		this.organizeId = userInfo.organizeId
		// 获取商品列表
		this.productList = await this.getGoodsList()		
	},
	filters: {
		//处理金额
		NumFormat: function(text) {
			return Number(text).toFixed(2)
		}
	},
	methods: {		
		// 上拉加载更多
		async scrolltolower() {
			if (!this.hasNextPage) {
				// 没有更多了
				this.loadingNow = false
				return
			}
			if (this.pullFlag) {
				this.loadingNow = true
				// 页码加1
				this.pageNum += 1
				const addGoods = await this.getGoodsList()
				this.productList = [...this.productList, ...addGoods]
				this.pullFlag = false
				setTimeout(() => {
					this.pullFlag = true
				}, 1500)
			}
		},
		// 获取商品列表
		async getGoodsList() {
			// 参数
			const params = {
				organizeId: this.organizeId,
				pageNum: this.pageNum,
				pageSize: this.pageSize,
				productName: this.searchVal
			}
			// 获取商品数据
			const { code, data: result } = await this.SellerService.GoodList(params).catch(
				error => {
					console.log(error)
				}
			)
			// 如果获取失败 返回上一页
			if (code !== 0) {
				uni.navigateBack({
					delta: 1
				})
				return
			}
			// 是否还有下一页
			this.hasNextPage = result.hasNextPage
			this.loadingNow = false
			// 返回列表
			return result.list
		},
		// 关键字搜索
		async searchKeyWords() {
			this.pageNum = 1
			this.productList = await this.getGoodsList()
		},
		searchValChange(val) {
			this.searchVal = val
		},
		searchValBlur() {
			this.searchKeyWords()
		},
		async searchValClear() {
			this.searchVal = ''
			this.searchKeyWords()
		},
		// 搜索按钮点击事件
		async searchValHandle(val) {
			// 如果输入框内容为空
			if (val.trim().length <= 0) {
				this.searchVal = ''
				return
			}
			this.searchKeyWords()
		},
		operationHanld(prop) {
			this.$emit('operationConfim', prop)
		},
		// 跳转详情
		navToDetailPage(id) {
			this.isModallayer = true
			this.$api.navigateTo(`/pages/goods/product?id=${id}`)
			this.isModallayer = false
		},
		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/user/cart/cart')
		},
		btnTouchstart() {
			// console.log('btnTouchstart');
		},
		btnTouchend() {
			// console.log('btnTouchend');
		}
	}
}
</script>

<style lang="scss">
.commodity-list-wrapper {
	.empty-container-image {
		width: 200rpx;
		height: 200rpx;
		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%;
		font-size: 20rpx;
		display: inline-block;
		color: #999999;
	}
	.list-details-miniQuantity {
		width: 100%;
		display: inline-block;
		font-size: 20rpx;
		color: #999999;
	}
}
.price-box{
		width: 100%;
		position: absolute;
		bottom: 0;
	}
.list-details-price {
	width: 100%;
	// line-height: 54rpx;
	// margin-top: 10rpx;
	// height: 54rpx;
	float: left;
	.floor-item-act{
		width: 100%;
		// height: 56rpx;
		text-align: center;
		box-sizing: border-box;
		float: left;
		.floor-tags{
			height: 28rpx;
			border-radius: 6rpx;
			background-color: #FFFFFF;
			line-height: 28rpx;
			color: #ff457b;
			text-align: center;
			display: inline-block;
			padding:0 5rpx;
			font-size: $font-size-20;
			border: 1px solid #ff457b;
			float: left;
		}
	}
	.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: #ff457b;
		}
	}
	.list-shop {
		height: auto;
		float: left;
		.list-price {
			width: 100%;
			color: #ff457b;
			float: left;
			line-height: 54rpx;
			align-items: center;
			justify-content: center;
			.price-larger {
				font-size: $font-size-30;
				display: inline-block;
				font-weight: bold;
				&.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;
	}
}
.list-details-tags {
	display: flex;
	justify-content: flex-start;
	margin: 14rpx 0;
	height: 34rpx;
	.tag {
		margin-right: 5rpx;
	}
}
.product-container {
	width: 100%;
	height: 100vh;
	overflow: hidden;
	padding-top: 100rpx;
	box-sizing: border-box;
}
</style>
