<template>
	<view class="customBox">
		<scroll-view scroll-y="true" class="contentBox" @scrolltolower="scorllBottom" :scroll-top="topNum">
			<view class="goodbox">
				<view class="item" @click="toGroups(item.id)" v-for="(item, index) in goodsList" :key="item.id">
					<div class="imgbox">
						<image :src="item.face_img + '?x-oss-process=image/format,webp'" :webp="true" mode="aspectFill" class="img"></image>
						<view class="out" v-if="item.sold_status === 2">
							<img src="https://ct-upimg.yx090.com/ju8hn6/shop/image/2024/01/16/Cr6cRRhhqYNhScigxIyumyV9hXXMI3vKvsE1jgt0.png" class="out_img" />
						</view>
						<view class="out" v-else-if="item.sold_status === 0">
							<img src="https://ct-upimg.yx090.com/ju8hn6/shop/image/2024/08/21/QBhUheeYWSJ3OGM27fkAufJAlFSA8GBhWjpY5oNy.png" class="out_img" />
						</view>
						<view class="out" v-else-if="item.total_stock === 0">
							<img src="https://ct-upimg.yx090.com/ju8hn6/shop/image/2024/08/21/pMTv6QiZZEpSqkJmk7hMcbEzIuNzMyp7YVBbe42H.png" class="out_img" />
						</view>
					</div>
					
					<view class="box">
						<view class="tit">{{item.title}}</view>
						<view class="bot">
							<view class="text">
								<text class="prefix">￥</text>
								<text class="txt1" v-if="parseFloat(item.min_price) == parseFloat(item.max_price)">{{item.min_price}}</text>
								<text class="txt1" v-else>{{item.min_price * 1}}~{{item.max_price}}</text>
							</view>
							<view class="icon"><up-icon name="shopping-cart" :color="Color" size="22" /></view>
						</view>
					</view>
				</view>
			</view>
			<view class="bottom" v-if="goodsList.length && page >= last_page && !loading1">
				— 没有更多了 —
			</view>
			<view v-if="!goodsList.length && !loading1" class="empty">暂无数据</view>
		</scroll-view>
		
		<!-- 返回顶部 -->
		<view @click="handleTop" v-if="showTop" class="top">
			<up-icon name="arrow-upward" color="#000" size="24" />
		</view>
	</view>
</template>

<script>
	import { ref, reactive, toRefs, onMounted } from 'vue'
	import { get, post } from '@/Api/request.js'
	import { Style } from '@/utils/list.js'
	
	export default {
		options: {
			styleIsolation: 'shared'
		},
		props: {
		},
		setup(props, context) {
			const data = reactive({
				topNum: 0,
				showTop: false,
				goodsList: [],
				page: 1,
				last_page: 0,
				shareUrl: '',
				loading1: false,
				Color: uni.getStorageSync('theme_color'),
				priceColor: Style[uni.getStorageSync('theme_index') * 1].priceColor,
				imgHeight: 0
			})
			
			async function getFetchList(val = 1) {
				data.loading1 = true
				uni.showLoading({
					title: '加载中...',
					mask: true
				})
				let params = {
					page: data.page,
					pageSize: 20
				}
				await get('/api/app/groupGoods/all', params).then((res) => {
					data.goodsList = val == 1 ? data.goodsList.concat(res.data) : res.data
					data.last_page = res.meta.last_page
					data.loading1 = false
					uni.hideLoading()
				}).catch(() => {
					data.loading1 = false
				})
			}
			
			function scorllBottom() {
				if (data.page < data.last_page) {
					data.page ++
					getFetchList()
				}
			}
			
			const toGroups = (id) => {
				uni.navigateTo({
					url: '/pages/groups/index?id=' + id
				})
			}
			
			const handleTop = () => {
				data.topNum = data.topNum == 0 ? 0.1 : 0
			}
			
			onMounted(() => {
				data.Color = uni.getStorageSync('theme_color')
				getFetchList(0)
			})
			
			return {
				...toRefs(data),
				getFetchList,
				scorllBottom,
				handleTop,
				toGroups
			}
			
		},
		onPageScroll(e) {
			if (e.scrollTop > 300) {
				this.showTop = true
			} else {
				this.showTop = false
			}
		},
	}
</script>

<style lang="scss" scoped>
	.customBox{
		padding: 0 0 1rpx;
		height: calc(100vh - 242rpx);
		box-sizing: border-box;
		.contentBox{
			height: 100%;
			overflow-y: auto;
			position: relative;
		}
		.goodbox{
			padding: 24rpx 24rpx 1rpx;
			display: flex;
			flex-wrap: wrap;
			box-sizing: border-box;
			.item{
				background: #fff;
				padding: 24rpx;
				box-sizing: border-box;
				border-radius: 10rpx;
				width: calc(50% - 14rpx);
				margin: 0 24rpx 24rpx 0;
				&:nth-child(2n+2){
					margin: 0 0 24rpx 0;
				}
				.imgbox{
					position: relative;
					.out{
						position: absolute;
						width: 100%;
						height: 100%;
						display: flex;
						align-items: center;
						justify-content: center;
						top: 0;
						left: 0;
						.out_img{
							width: 150rpx;
							height: 150rpx;
						}
					}
				}
				.img{
					border-radius: 10rpx;
					width: 100%;
					height: 292rpx;
				}
				.box{
					display: flex;
					flex-direction: column;
					color: #333;
					justify-content: space-between;
					width: 100%;
					height: auto;
					.tit{
						font-size: 28rpx;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 2;
						overflow: hidden;
						line-height: 40rpx;
						height: 80rpx;
						margin: 10rpx 0;
					}
					.txt1{
						font-size: 28rpx;
					}
					.bot{
						display: flex;
						align-items: center;
						justify-content: space-between;
					}
					.text{
						color: v-bind('priceColor');
						.prefix{
							font-size: 24rpx;
						}
						text{
							font-size: 30rpx;
						}
					}
				}
			}
		}
		.empty{
			font-size: 24rpx;
			text-align: center;
			padding: 60rpx 0;
		}
		.bottom {
			text-align: center;
			font-size: 24rpx;
			color: #999;
			margin: auto;
			padding: 0 0 60rpx;
			width: 100%;
		}
		.top {
			position: fixed;
			bottom: 15%;
			right: 5%;
			background: #fff;
			width: 80rpx;
			height: 80rpx;
			border-radius: 50%;
			display: flex;
			align-items: center;
			justify-content: center;
			box-shadow: 0 2px 10px 0 rgba(0, 0, 0, 0.2);
			z-index: 2;
		}
	}
</style>
