<template>
	<view class="whole">
		<!-- 搜索 -->
		<search-input @searchBtn="searchBtn" :searchValue="keyword" :show="false" />
		
		<!-- 排序 -->
		<view class="sort_box">
			<view class="sort_item flex" 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">
				<view class="item flex1" @click="toGroups(item.id)" v-for="(item, index) in goodsList" :key="item.id">
					<view class="imgbox">
						<image :src="item.face_img + '?x-oss-process=image/format,webp'" :webp="true" mode="aspectFill" class="img"></image>
						<view class="out flex" v-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>
					
					<view class="box">
						<view class="tit">{{item.title}}</view>
						<view class="flex1">
							<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}}~{{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 && !loading">— 没有更多了 —</view>
		<view v-if="!goodsList.length && !loading" class="empty">
			<up-empty mode="data" icon="https://ct-upimg.yx090.com/g.ii090/images/sprite/empty/data.png" text="暂无数据"></up-empty>
		</view>
		<view v-if="loading" class="loadbox">
			<up-loading-icon text="加载中..." textSize="14"></up-loading-icon>
		</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 searchInput from '@/components/searchInput/index.vue'
	import { get, post } from '@/api/request.js'
	import { Style } from '@/utils/list.js'
	
	export default {
		components: {
			searchInput
		},
		setup() {
			const data = reactive({
				keyword: '',
				title: '',
				goodsList: [],
				loading: true,
				showTop: false,
				page: 1,
				last_page: 0,
				sortList: ['综合', '销量', '价格', '上新'],
				sortIndex: 0,
				priceType: false,
				sortFilter: {
					sales: '',
					price: '',
					score: 2,
					new: ''
				},
				Color: '',
				priceColor: ''
			})
			
			// 搜索
			async function searchBtn(e) {
				data.keyword = e
				data.page = 1
				data.goodsList = []
				await 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.loading = true
				let params = {
					page: data.page,
					pageSize: 20,
					sales: data.sortFilter.sales,
					price: data.sortFilter.price,
					score: data.sortFilter.score,
					new: data.sortFilter.new,
					keyword: data.keyword
				}
				await get('/api/app/remind', params).then((res) => {
					data.goodsList = data.goodsList.concat(res.data)
					data.last_page = res.meta.last_page
					data.loading = false
				}).catch(() => {
					data.loading = false
				})
			}
			
			const hanleTop = () => {
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 300
				})
			}
			
			const toGroups = (id) => {
				uni.navigateTo({
					url: '/pages/groups/index?id=' + id
				})
			}
			
			return {
				...toRefs(data),
				getFetchList,
				hanleTop,
				toGroups,
				chooseSort,
				searchBtn
			}
		},
		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.title = options.title || '新品预告'
			// await this.$onLaunched
			await this.getFetchList()
			this.Color = uni.getStorageSync('theme_color')
			this.priceColor = Style[uni.getStorageSync('theme_index') * 1].priceColor
			uni.setNavigationBarTitle({
				title: this.title
			})
		}
	}
</script>

<style lang="scss" scoped>
	.flex{
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.flex1{
		display: flex;
		align-items: center;
		justify-content: space-between;
	}
	.whole{
		min-height: 100vh;
		.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-bottom: 24rpx;
			background: #fff;
			padding: 24rpx;
			box-sizing: border-box;
			border-radius: 10rpx;
			width: 100%;
			.imgbox{
				position: relative;
				.out{
					position: absolute;
					width: 100%;
					height: 100%;
					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;
				}
				.text{
					color: v-bind('priceColor');
					.prefix{
						font-size: 24rpx;
					}
					text{
						font-size: 34rpx;
					}
				}
			}
		}
		&.two{
			.item{
				width: 340rpx;
				margin-right: calc(100% - 682rpx);
				display: block;
				&:nth-child(2n+2){
					margin-right: 0;
				}
				.img{
					width: 100%;
					height: 292rpx;
				}
				.box{
					width: 100%;
					height: auto;
					.tit{
						white-space: nowrap;
						text-overflow: ellipsis;
						overflow: hidden;
						display: block;
						padding: 10rpx 0;
					}
					.txt1{
						font-size: 28rpx;
					}
				}
				.imgbox{
					.out{
						.out_img{
							width: 150rpx;
							height: 150rpx;
						}
					}
				}
			}
		}
		&.three{
			.item{
				width: 220rpx;
				margin-right: calc(50% - 332rpx);
				display: block;
				padding: 18rpx;
				&:nth-child(3n+3){
					margin-right: 0;
				}
				.img{
					width: 100%;
					height: 184rpx;
				}
				.box{
					width: 100%;
					height: auto;
					.text{
						text-overflow: ellipsis;
						overflow: hidden;
						white-space: nowrap;
					}
					.tit{
						white-space: nowrap;
						text-overflow: ellipsis;
						overflow: hidden;
						padding: 10rpx 0;
						display: block;
						font-size: 24rpx;
					}
					.txt1{
						font-size: 26rpx;
					}
					.icon{
						display: none;
					}
				}
			}
		}
	}
	.empty{
		text-align: center;
		font-size: 28rpx;
		padding: 90rpx 0;
		color: #999;
	}
	.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: #999;
		margin: auto;
		padding: 0 0 60rpx;
		width: 100%;
	}
	.sort_box {
		display: flex;
		background-color: #fff;
		height: 90rpx;
		align-items: center;
		justify-content: space-around;
		.sort_item {
			flex: 1;
			color: #999;
			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;
		}
	}
	.loadbox{
		padding: 30rpx 0;
		text-align: center;
		color: #666;
	}
</style>
