<template>
	<view class="app-container">
		<view class="search-container">
			<view class="search">
				<image src="/static/icon/search.png" class=""></image>
				<input type="text" confirm-type="search" placeholder="搜索商品名称或型号" v-model="keyword" @confirm="searchSubmit">
			</view>
		</view>
		
		<view class="list-container">
			<block v-for="(item, index) in goodsList" :key='index'>
				<view class="item" @tap="toGoodsDetail(item.goodsId)">
					<view class="image">
					  <image :src="item.imgUrl" mode="aspectFill" class="img"></image>
					  <image
					    :src="item.logo"
					    mode="aspectFill"
					    class="water"
					    v-if="item.isShowWater"
					  ></image>
					</view>
					<view class="content">
						<view class="title ellipsis-2">{{item.goodsName}}</view>
						<view class="bottom">
							<view class="price">
								<view class="price-1">¥{{item.goodsPrice | numToFixed}}</view>
								<view class="price-2">¥{{item.orgGoodsPrice | numToFixed}}</view>
							</view>
							<view class="btn"><image src="@/static/icon/cart.png" mode="aspectFill"></image></view>
						</view>
					</view>
				</view>
			</block>
		</view>
		<no-data v-if="!goodsList.length" :showText="'暂无商品'"></no-data>
		<loading-text v-if="goodsList.length"  :loading="loading" :noMore="noMore" ></loading-text>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				keyword: '',
				goodsList: [],
				pageNum: 1,
				pageSize: 8,
				noMore: false,
				loading: false,
			}
		},
		
		onLoad({keyword}) {
			this.keyword = keyword;
			this.getGoodsList();
		},
		
		// 下拉刷新
		onPullDownRefresh() {
			this.pageNum = 1;
			this.getGoodsList();
		},
		
		// 上拉加载
		onReachBottom() {
			this.getGoodsList(1);
		},
		
		methods: {
			// 获取商品列表
			getGoodsList(loadMore) {
				if(this.noMore && loadMore)return;
				this.noMore = false
				if(!loadMore){
					this.pageNum = 1;
				}else{
					this.loading = true;
				}
				this.$axios({
					url: '/goods/list/sort/page',
					method: 'get',
					params: {
						pageNum: this.pageNum,
						pageSize: this.pageSize,
						keyword: this.keyword,
					},
					isLoading: !loadMore
				}).then(res => {
					res.data.records.forEach((item) => {
					  if (item.logo && item.logoStartTime) {
					    item.isShowWater = this.$compareTime(
					      item.logoStartTime,
					      item.logoEndTime
					    );
					  } else {
					    item.isShowWater = false;
					  }
					});
					let _list = res.data.records;
					let pageTotal = res.data.pages;
					if(this.pageNum >= pageTotal){
						this.noMore = true;
					}
					if (_list.length) {
						this.pageNum += 1
					}
					if (loadMore) {
						this.goodsList = this.goodsList.concat(_list);
						this.loading = false;
					} else {
						this.goodsList = _list;
					}
					
					uni.stopPullDownRefresh();
				})
			},
			
			searchSubmit() {
				this.getGoodsList();
			},
			
			toGoodsDetail(id) {
				uni.navigateTo({
					url:'/packageGoods/pages/detail?id=' + id
				})
			}
		}
	}
</script>

<style lang="scss">
	.app-container {
		background: #F4F2F2;
		box-sizing: border-box;
		padding-top: 90rpx;
	}
	.search-container {
		background: #FFFFFF;
		padding: 12rpx 20rpx;
		border-bottom: 1px solid #F4F2F2;
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		box-sizing: border-box;
		.search {
			height: 64rpx;
			display: flex;
			align-items: center;
			background: #F0F0F0;
			border-radius: 64rpx;
			padding: 0 20rpx;
			image {
				width: 28rpx;
				height: 28rpx;
			}
			input {
				width: 100%;
				padding-left: 15rpx;
			}
		}
	}
	.list-container {
		display: flex;
		flex-wrap: wrap;
		padding: 20rpx;
		.item {
			width: 348rpx;
			background: #FFFFFF;
			margin-right: 14rpx;
			margin-bottom: 20rpx;
			border-radius: 20rpx;
			overflow: hidden;
			&:nth-child(2n) {
				margin-right: 0;
			}
			// image {
			// 	width: 348rpx;
			// 	height: 348rpx;
			// }
			.image {
			  width: 348rpx;
			  height: 348rpx;
			  flex-shrink: 0;
			  position: relative;
			  .img {
			    width: 348rpx;
			    height: 348rpx;
			    display: block;
			  }
			  .water {
			    width: 348rpx;
			    height: 348rpx;
			    display: block;
			    position: absolute;
			    left: 0;
			    top: 0;
			    z-index: 1;
			  }
			}
			.content {
				padding: 15rpx 20rpx;
				.title {
					font-size: 30rpx;
					color: #333333;
					line-height: 36rpx;
					font-weight: 600;
					height: 72rpx;
				}
				.bottom {
					display: flex;
					justify-content: space-between;
					align-items: center;
					margin-top: 10rpx;
					.price {
						display: flex;
						flex-direction: column;
					}
					.price-1 {
						font-size: 32rpx;
						color: #FF3F42;
						line-height: 36rpx;
					}
					.price-2 {
						font-size: 26rpx;
						color: #666666;
						line-height: 30rpx;
						text-decoration: line-through;
					}
					.btn {
						width: 60rpx;
						height: 60rpx;
						background: #FE781F;
						border-radius: 50%;
						display: flex;
						align-items: center;
						justify-content: center;
						image {
							width: 41rpx;
							height: 36rpx;
							display: block;
						}
					}
				}
			}
		}
	}
</style>
