<!-- 团购分组推荐位 -->
<template>
	<view class="whole" v-if="loading" :style="{background: pageInfo.background_color || '#F5F5F5'}">
		<!-- 搜索 -->
		<view class="searchBox">
			<view class="box">
				<up-icon name="search" color="#656565" size="18" />
				<input class="input"
				   type="text"
				   :value="keyword"
				   placeholder="搜索商品"
				   @input="watchValue"
				   @confirm="onSearch" />
			</view>
			<view class="btn" @click="onSearch">搜索</view>
		</view>
		<!-- 头图 -->
		<view class="bg">
			<image :src="pageInfo.image + '?x-oss-process=image/format,webp'" :webp="true" mode="widthFix" class="image"></image>
		</view>
		
		<up-sticky :offset-top="0">
			<!-- 分类 -->
			<view :style="{background: pageInfo.nav_bg_color || '#f3f3f3'}" class="tabsBox" v-if="typeList.length">
				<up-tabs
					:current="activeIndex"
					keyName="name"
					: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>
		</up-sticky>
		
		<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="20" /></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>
		
		<!-- 返回顶部 -->
		<div @click="hanleTop" v-show="showTop" class="top">
			<up-icon name="arrow-upward" color="#000" size="24" />
		</div>

	</view>
</template>

<script>
	import { ref, reactive, toRefs } from 'vue'
	import { userBind } from '@/components/common.js'
	import { modifyTextColor } from '@/utils/index.js'
	import { get, post } from '@/api/request.js'
	import { Style } from '@/utils/list.js'
	
	export default {
		setup() {
			const data = reactive({
				activeIndex: 0,
				keyword: '',
				id: '',
				goodsList: [],
				loading: false,
				showTop: false,
				pageInfo: {
					image: ''
				},
				page: 1,
				last_page: 0,
				typeId: 0,
				typeList: [],
				sortList: ['综合', '销量', '价格', '上新'],
				sortIndex: 0,
				priceType: false,
				sortFilter: {
					sales: '',
					price: '',
					score: 2,
					new: ''
				},
				loading1: false,
				Color: '',
				priceColor: ''
			})
			
			async function getPageInfo() {
				await get('/api/app/showcase/type/category/' + data.id).then((res) => {
					data.pageInfo = res
					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
				})
			}
			
			function watchValue(e) {
				data.keyword = e.detail.value
			}
			
			// 搜索
			function onSearch() {
				data.page = 1
				data.goodsList = []
				getFetchList()
			}
			
			// 排序搜索
			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()
			}
			
			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: data.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
				})
			}
			const hanleTop = () => {
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 300
				})
			}
			const toGroups = (id) => {
				uni.navigateTo({
					url: '/pages/groups/index?id=' + id + '&source_id=' + data.id + '&source_type=exclusive'
				})
			}
			
			const changeTabs = (event) => {
				if (event.id == data.typeId) {
					return
				}
				data.activeIndex = event.index
				data.typeId = event.id
				data.page = 1
				data.goodsList = []
				getFetchList()
			}
			
			async function getCategoryList(id) {
				await get('/api/app/group', { parent_id: id }).then((res) => {
					data.typeList = data.typeList.concat(res.data)
				})
			}
			
			return {
				modifyTextColor,
				...toRefs(data),
				getFetchList,
				hanleTop,
				toGroups,
				changeTabs,
				onSearch,
				watchValue,
				chooseSort,
				getCategoryList,
				getPageInfo
			}
		},
		onReachBottom() {
			if (this.page < this.last_page) {
				this.page ++
				this.getFetchList()
			}
		},
		onPageScroll(e) {
			if (e.scrollTop > 300) {
				this.showTop = true
			} else {
				this.showTop = false
			}
		},
		async onLoad(options) {
			this.id = options.id
			// await this.$onLaunched
			await this.getPageInfo()
			this.Color = uni.getStorageSync('theme_color')
			this.priceColor = Style[uni.getStorageSync('theme_index') * 1].priceColor
			this.title = options.title || this.pageInfo.title
			uni.setNavigationBarTitle({
				title: this.title
			})
		}
	}
</script>

<style lang="scss" scoped>
	.whole{
		min-height: 100vh;
		.searchBox{
			padding: 24rpx 20rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			box-sizing: border-box;
			font-size: 28rpx;
			.box{
				height: 60rpx;
				display: flex;
				align-items: center;
				box-sizing: border-box;
				width: calc(100% - 164rpx);
				background-color: #F7F8FA;
				border-radius: 60rpx;
				padding-left: 20rpx;
				.input{
					width: calc(100% - 40rpx);
					margin-left: 10rpx;
				}
			}
			.btn{
				width: 140rpx;
				height: 60rpx;
				background: v-bind('Color');
				border-radius: 60rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				color: #fff;
				font-size: 30rpx;
			}
		}
		.bg{
			width: 100%;
			box-sizing: border-box;
			.image{
				width: 100%;
				vertical-align: bottom;
			}
		}
	}
	.goodbox{
		padding: 24rpx;
		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;
	}
	.top {
		position: fixed;
		bottom: 15%;
		right: 5%;
		background: #fff;
		width: 80rpx;
		height: 80rpx;
		line-height: 107rpx;
		border-radius: 50%;
		text-align: center;
		box-shadow: 0 2px 10px 0 rgba(0, 0, 0, 0.2);
		z-index: 10;
	}
	.bottom {
		text-align: center;
		font-size: 24rpx;
		color: #fff;
		margin: auto;
		padding: 30rpx 0 60rpx;
		width: 100%;
	}
	.tabsBox{
		
	}
	.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;
		}
	}
</style>
