<template>
	<view class="customBox" :style="{background: pageInfo.background_color || '#F5F5F5'}">
		<scroll-view scroll-y="true" class="contentBox" @scrolltolower="scorllBottom" :scroll-top="topNum" @scroll="scrollView">
			<!-- 头图 -->
			<view class="bg" v-if="pageInfo.image">
				<image :src="pageInfo.image + '?x-oss-process=image/format,webp'" :webp="true" mode="widthFix" class="image" @load="loadPageImage"></image>
			</view>
			
			<!-- 分类 -->
			<view :style="{background: pageInfo.nav_bg_color || '#f3f3f3'}" class="tabsBox" :class="isFixed ? 'fixed' : ''" v-if="typeList.length">
				<up-tabs
					:list="typeList"
					:lineColor="pageInfo.nav_text_color"
					:inactiveStyle="{ color: pageInfo.nav_text_color }"
					:activeStyle="{ color: pageInfo.nav_text_color }"
					@click="changeTabs">
				</up-tabs>
			</view>
			
			<!-- 排序 -->
			<view class="sort_box">
				<view class="sort_item" v-for="(item, index) in sortList" :key="index" @click.stop="chooseSort(index)">
					<text :class="sortIndex === index ? 'choose' : ''">{{item}}</text>
					<view class="icon" v-if="index === 2">
						<up-icon name="arrow-up" size="10" :color="priceType && sortIndex === 2 ? Color : ''" />
						<up-icon name="arrow-down" size="10" :color="!priceType && sortIndex === 2 ? Color : ''" />
					</view>
				</view>
			</view>
			<view class="wholeBox">
				<view class="goodbox" :class="pageInfo.show_type === 2 ? 'two' : pageInfo.show_type === 3 ? 'three' : ''">
					<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>
			<view class="bottom" v-if="goodsList.length && page >= last_page && !loading1" :style="{color: modifyTextColor(pageInfo.background_color)}">
				— 没有更多了 —
			</view>
			<view v-if="!goodsList.length && !loading1" class="empty" :style="{color: modifyTextColor(pageInfo.background_color)}">暂无数据</view>
		</scroll-view>
		
		<!-- 返回顶部 -->
		<view @click="hanletop" v-if="showTop" class="top">
			<up-icon name="arrow-upward" color="#000" size="24" />
		</view>
	</view>
</template>

<script>
	import { ref, reactive, toRefs, onMounted, watch } from 'vue'
	import { modifyTextColor } from '@/utils/index.js'
	import { Style } from '@/utils/list.js'
	import { get, post } from '@/Api/request.js'
	export default {
		options: {
			styleIsolation: 'shared'
		},
		props: {
			groupId: {
				type: Number,
				default: 0
			}
		},
		setup(props, context) {
			const data = reactive({
				topNum: 0,
				isFixed: false,
				showTop: false,
				goodsList: [],
				loading: false,
				pageInfo: {
					image: ''
				},
				page: 1,
				last_page: 0,
				shareUrl: '',
				typeId: 0,
				typeList: [],
				sortList: ['综合', '销量', '价格', '上新'],
				sortIndex: 0,
				priceType: false,
				sortFilter: {
					sales: '',
					price: '',
					score: 2,
					new: ''
				},
				loading1: false,
				Color: uni.getStorageSync('theme_color'),
				imgHeight: 0,
				priceColor: Style[uni.getStorageSync('theme_index') * 1].priceColor
			})
			
			async function getPageInfo() {
				data.id && await get('/api/v1/showcase/type/category/' + data.id).then((res) => {
					data.pageInfo = res
					context.emit('handleShareInfo', res.share_image + '&&' + res.share_title)
					if(res.recommend_shop_group.length) {
						data.typeId = res.recommend_shop_group[0]
						data.typeList.push({id: data.typeId, name: '推荐'})
						getFetchList()
						getCategoryList(res.recommend_shop_group[0])
					}
					data.loading = true
				})
			}
			
			async function getCategoryList(id) {
				await get('/api/app/group', { parent_id: id }).then((res) => {
					data.typeList = data.typeList.concat(res.data)
				})
			}
			
			async function getFetchList() {
				data.loading1 = true
				uni.showLoading({
					title: '加载中...',
					mask: true
				})
				let params = {
					page: data.page,
					pageSize: 24,
					sales: data.sortFilter.sales,
					price: data.sortFilter.price,
					score: data.sortFilter.score,
					new: data.sortFilter.new,
					keyword: '',
					shop_group_id: data.typeId
				}
				await get('/api/app/search', params).then((res) => {
					data.goodsList = data.goodsList.concat(res.data)
					data.last_page = res.meta.last_page
					data.loading1 = false
					uni.hideLoading()
				}).catch(() => {
					data.loading1 = false
				})
			}
			
			// 排序搜索
			function chooseSort(index) {
				data.sortIndex = index
				if (index === 2) {
					data.priceType = !data.priceType
					data.sortFilter.price = data.priceType ? 1 : 2
					data.sortFilter.new = ''
				} else {
					data.sortFilter.price = ''
				}
				data.sortFilter.sales = index === 1 ? 2 : ''
				data.sortFilter.score = index === 0 ? 2 : ''
				data.sortFilter.new = index === 3 ? 2 : ''
				data.page = 1
				data.goodsList = []
				getFetchList()
			}
			
			const changeTabs = (event) => {
				if (event.id == data.typeId) {
					return
				}
				data.typeId = event.id
				data.page = 1
				data.goodsList = []
				getFetchList()
			}
			
			function scorllBottom() {
				if (data.page < data.last_page) {
					data.page ++
					getFetchList()
				}
			}
			
			function scrollView(e) {
				data.isFixed = e.detail.scrollTop > data.imgHeight
			}
			
			function loadPageImage(e) {
				uni.getSystemInfo({
					success: (res) => {
						data.imgHeight = res.windowWidth * e.detail.height / e.detail.width
					}
				})
			}
			
			const toGroups = (id) => {
				uni.navigateTo({
					url: '/pages/groups/index?id=' + id + '&source_id=' + data.id + '&source_type=exclusive'
				})
			}
			
			const hanletop = () => {
				data.topNum = data.topNum == 0 ? 0.1 : 0
			}
			
			watch(props, (newProps) => {
				if(newProps.groupId) {
					data.id = newProps.groupId
					data.goodsList = []
					data.typeList = []
					getPageInfo()
				}
			})
			
			onMounted(() => {
				data.Color = uni.getStorageSync('theme_color')
				data.id = props.groupId
				getPageInfo()
			})
			
			return {
				modifyTextColor,
				...toRefs(data),
				getPageInfo,
				getFetchList,
				chooseSort,
				changeTabs,
				scorllBottom,
				hanletop,
				toGroups,
				scrollView,
				loadPageImage
			}
			
		},
		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;
		}
		.bg{
			width: 100%;
			box-sizing: border-box;
			.image{
				width: 100%;
				vertical-align: bottom;
			}
		}
		.tabsBox{
			width: 100%;
			&.fixed{
				position: fixed;
				top: 132rpx;
				z-index: 100;
			}
		}
		.sort_box {
			display: flex;
			background-color: #fff;
			height: 90rpx;
			align-items: center;
			justify-content: space-around;
			.sort_item {
				display: flex;
				align-items: center;
				justify-content: center;
				flex: 1;
				color: #666;
				font-size: 28rpx;
				.icon {
					display: flex;
					flex-direction: column;
					margin-left: 10rpx;
					.iconfont {
						font-size: 24rpx;
						line-height: 16rpx;
					}
				}
			}
			.choose {
				color: v-bind('Color');
				font-weight: bold;
			}
		}
		.goodbox{
			padding: 24rpx 24rpx 1rpx;
			display: flex;
			flex-wrap: wrap;
			box-sizing: border-box;
			.item{
				margin: 0 0 24rpx 0;
				display: flex;
				align-items: center;
				justify-content: space-between;
				background: #fff;
				padding: 24rpx;
				box-sizing: border-box;
				border-radius: 10rpx;
				width: 100%;
				.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: 100rpx;
							height: 100rpx;
						}
					}
				}
				.img{
					width: 200rpx;
					height: 200rpx;
					border-radius: 10rpx;
				}
				.box{
					display: flex;
					height: 200rpx;
					width: calc(100% - 220rpx);
					flex-direction: column;
					color: #333;
					justify-content: space-between;
					.tit{
						font-size: 28rpx;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 2;
						overflow: hidden;
						line-height: 40rpx;
					}
					.bot{
						display: flex;
						align-items: center;
						justify-content: space-between;
					}
					.text{
						color: v-bind('priceColor');
						.prefix{
							font-size: 24rpx;
						}
						text{
							font-size: 34rpx;
						}
					}
				}
			}
			&.two{
				.item{
					width: calc(50% - 14rpx);
					margin: 0 24rpx 24rpx 0;
					display: block;
					&:nth-child(2n+2){
						margin: 0 0 24rpx 0;
					}
					.img{
						width: 100%;
						height: 292rpx;
					}
					.box{
						width: 100%;
						height: auto;
						.tit{
							height: 80rpx;
							margin: 10rpx 0;
						}
						.txt1{
							font-size: 28rpx;
						}
					}
					.imgbox{
						.out{
							.out_img{
								width: 150rpx;
								height: 150rpx;
							}
						}
					}
				}
			}
			&.three{
				.item{
					width: calc(33.3% - 18rpx);
					margin: 0 24rpx 24rpx 0;
					display: block;
					padding: 18rpx;
					&:nth-child(3n+3){
						margin: 0 0 24rpx 0;
					}
					.img{
						width: 100%;
						height: 184rpx;
					}
					.box{
						width: 100%;
						height: auto;
						.text{
							text-overflow: ellipsis;
							overflow: hidden;
							white-space: nowrap;
						}
						.tit{
							height: 80rpx;
							margin: 10rpx 0;
							font-size: 24rpx;
						}
						.txt1{
							font-size: 26rpx;
						}
						.icon{
							display: none;
						}
					}
				}
			}
		}
		.empty{
			font-size: 24rpx;
			text-align: center;
			padding: 60rpx 0;
		}
		.bottom {
			text-align: center;
			font-size: 24rpx;
			color: #fff;
			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>
