<!-- 自定义分类推荐位 -->
<template>
	<view class="whole" v-if="!loading" :style="{background: pageInfo.background_color || '#F5F5F5'}">
		<view class="bg">
			<image :src="pageInfo.image + '?x-oss-process=image/format,webp'" mode="widthFix" class="image"></image>
		</view>
		
		<view :style="{background: pageInfo.nav_bg_color || '#f3f3f3'}" class="tabsBox" v-if="pageInfo.nav_direction == 'top'">
			<up-tabs
				:current="classifyId"
				:list="classifyList"
				:lineColor="Color"
				:inactiveStyle="{ color: pageInfo.nav_text_color }"
				:activeStyle="{ color: pageInfo.nav_text_color }"
				@click="changeTabs">
			</up-tabs>
		</view>
		
		<view class="content">
			<view class="leftBox top0" :style="{background: pageInfo.nav_bg_color || '#F6F7FA'}" v-if="pageInfo.nav_direction != 'top'">
				<view class="item"
					v-for="(item, index) in classifyList"
					:key="item.id"
					:style="{background: pageInfo.nav_bg_color, color: pageInfo.nav_text_color}"
					:class="classifyId === index ? 'active' : ''"
					@click="scrollIntoView(index)">{{item.name}}</view>
			</view>
			
			<view class="rightBox" :class="pageInfo.nav_direction == 'top' ? 'wid100' : ''">
				<view class="wholeBox" v-for="(item, index) in classifyList" :key="item.id" :class="'item' + index" :id="'item' + index">
					<view class="title" :style="{color: pageInfo.nav_title_color, fontSize: pageInfo.nav_title_size + 'px'}">
						<image v-if="item.image" :src="item.image" mode="widthFix" class="img"></image>
						<text v-else>{{item.name}}</text>
					</view>
					<view class="goodbox" :class="pageInfo.nav_direction == 'left' ? 'two' : pageInfo.show_type === 2 ? 'two' : pageInfo.show_type === 3 ? 'three' : ''">
						<template v-for="it in item.items" :key="it.id">
							<view v-if="!Array.isArray(it)" class="item" @click="toGroups(it.path)">
								<div class="imgbox">
									<image :src="it.image + '?x-oss-process=image/format,webp'" :webp="true" mode="aspectFill" class="img"></image>
									<view class="out" v-if="it.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="it.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="it.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">
										<view class="kuajing" v-if="it.has_cross_border">跨境包税</view>
										<view class="kuajing" v-else-if="it.overseas_direct_mail">海外直邮</view>{{it.title}}
									</view>
									<view class="ation" v-if="!it.has_cross_border">
										<template v-if="it.red_amount * 1 != 0">
											<text class="quan" v-if="it.red_type == 2">新人再减{{it.red_amount * 1}}元</text>
											<text class="quan" v-else="it.red_type == 0">券{{it.red_amount * 1}}元</text>
										</template>
										
										<template v-if="it.actives_title">
											<text class="man" v-for="(itm, i) in it.actives_title" :key="i">{{itm}}</text>
										</template>
									</view>
									<view class="bot">
										<view class="text">
											<text class="prefix">￥</text>
											<text class="txt1" v-if="parseFloat(it.min_price) == parseFloat(it.max_price)">{{it.min_price}}</text>
											<text class="txt1" v-else>{{it.min_price * 1}}~{{it.max_price}}</text>
										</view>
									</view>
								</view>
							</view>
						</template>
					</view>
				</view>
				<view :style="{height: botHg + 'px', width: '100%'}">
					<view class="bottom" :style="{color: modifyTextColor(pageInfo.background_color)}">
						— 没有更多了 —
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { ref, reactive, toRefs, nextTick } from 'vue'
	import { get, post } from '@/api/request.js'
	import { modifyTextColor } from '@/utils/index.js'
	import { Style } from '@/utils/list.js'
	
	export default {
		setup() {
			const data = reactive({
				id: '',
				goodsList: [],
				loading: true,
				pageInfo: {
					image: ''
				},
				actionId: 0,
				actionList: [],
				Color: '',
				priceColor: '',
				bgColor: '',
				classifyList: [],
				classifyId: 0,
				topHeight: 0,
				botHg: 40,
				screenHg: 0
			})
			
			async function getFetchList() {
				uni.showLoading({
					mask: true,
					title: '加载中'
				})
				await get('/api/app/showcase/type/category/' + data.id).then((res) => {
					data.pageInfo = res
					let list = JSON.parse(JSON.stringify(res.showcase_categories)) || []
					if(data.actionId == 0) {
						data.classifyList = list
					} else {
						data.classifyList = []
						list.forEach((item) => {
							let arr = []
							item.items.forEach((it) => {
								if(it.active_id == data.actionId) {
									arr.push(it)
								}
							})
							item.items = arr
						})
						data.classifyList = list
					}
					data.actionList = res.actives || []
					if(data.actionList.length != 0) {
						data.actionList.unshift({id: 0, name: '全部'})
					}
					data.loading = false
					uni.getSystemInfo({
						success: function(res){
							data.screenHg = res.windowHeight
						}
					})
					const query = uni.createSelectorQuery()
					const _this = this
					setTimeout(() => {
						data.classifyList.map((itm, index) => {
							query.select("#item" + index).boundingClientRect(function (rect) {
								itm.top = rect ? rect.top : 0
								if(index == data.classifyList.length - 1) {
									data.botHg = (rect ? rect.height : 0) >= data.screenHg ? 40 : (data.screenHg - (rect ? rect.height : 0) + 40).toFixed(2)
								}
							}).exec()
						})
						
						if(data.topHeight == 0) {
							uni.createSelectorQuery().in(_this).select('.tabsBox').boundingClientRect((req) => {
								data.topHeight += req ? req.height : 0
							}).exec()
						}
					}, 1500)
					uni.hideLoading()
				})
			}
			
			const toGroups = (path) => {
				uni.navigateTo({
					url: path + '&source_id=' + data.id + '&source_type=exclusive'
				})
			}
			
			const changeTabs = (event) => {
				if (event.index == data.actionId) {
					return
				}
				data.actionId = event.index
				scrollIntoView(data.actionId)
			}
			
			const scrollIntoView = (index) => {
				uni.pageScrollTo({
					selector: '#item' + index,
					offsetTop: -44,
					duration: 0
				})
			}
			
			return {
				...toRefs(data),
				getFetchList,
				toGroups,
				modifyTextColor,
				changeTabs,
				scrollIntoView
			}
		},
		async onLoad(options) {
			this.id = options.id
			// await this.$onLaunched
			await this.getFetchList()
			this.title = options.title || this.pageInfo.title
			this.Color = uni.getStorageSync('theme_color')
			this.priceColor = Style[uni.getStorageSync('theme_index') * 1].priceColor
			this.bgColor = Style[uni.getStorageSync('theme_index') * 1].bgColor
			uni.setNavigationBarTitle({
				title: this.title
			})
		},
		onPageScroll(e) {
			this.classifyList.map((itm, idx) => {
			    if (idx == 0 && (e.scrollTop < this.classifyList[idx + 1].top - this.topHeight)) {
					this.classifyId = idx
			        return false
			    }
			    if (e.scrollTop > this.classifyList[idx].top - this.topHeight) {
			        this.classifyId = idx
			    }
			})
		}
	}
</script>

<style lang="scss" scoped>
	.whole{
		min-height: 100vh;
		.bg{
			width: 100%;
			.image{
				width: 100%;
				vertical-align: bottom;
			}
		}
		.tabsBox{
			position: sticky;
			top: -1px;
			z-index: 10;
		}
	}
	.content{
		width: 100%;
		display: flex;
		box-sizing: border-box;
		.leftBox{
			height: calc(100vh - 44px);
			overflow: auto;
			width: 120rpx;
			background-color: #F6F7FA;
			position: sticky;
			top: 44px;
			&.top0{
				top: -1px;
				height: 100vh
			}
			.item{
				position: relative;
				font-size: 28rpx;
				color: #666;
				display: flex;
				align-items: center;
				padding: 20rpx 30rpx;
				box-sizing: border-box;
				line-height: 36rpx;
				&.active{
					color: #222;
					background: #fff;
					&:before{
						content: '';
						position: absolute;
						left: 8rpx;
						height: 20rpx;
						top: 50%;
						margin-top: -10rpx;
						background: v-bind('Color');
						width: 8rpx;
						border-radius: 8rpx;
					}
				}
			}
		}
		.rightBox{
			width: calc(100% - 120rpx);
			&.wid100{
				width: 100%;
			}
		}
	}
	.wholeBox{
		.title{
			font-size: 30rpx;
			padding: 30rpx 20rpx 0 20rpx;
			font-weight: bold;
			text-align: center;
			.img{
				width: 100%;
			}
		}
	}
	.goodbox{
		padding: 20rpx;
		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;
				}
				.ation{
					font-size: 22rpx;
					text{
						margin: 0 8rpx 8rpx 0;
						border-radius: 6rpx;
						padding: 6rpx 10rpx;
						line-height: 1;
						color: v-bind('Color');
						display: inline-block;
					}
					.quan{
						border: 1px solid v-bind('Color');
					}
					.man{
						background-color: v-bind('bgColor');
						border: 1px solid v-bind('bgColor');
					}
				}
				.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: 236rpx;
				}
				.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);
				display: block;
				padding: 18rpx;
				margin: 0 24rpx 24rpx 0;
				&:nth-child(3n+3){
					margin: 0 0 24rpx 0;
				}
				.img{
					width: 100%;
					height: 184rpx;
				}
				.box{
					width: 100%;
					height: auto;
					.tit{
						height: 80rpx;
						margin: 10rpx 0;
					}
					.txt1{
						font-size: 26rpx;
					}
					.icon{
						display: none;
					}
				}
			}
		}
	}
	.empty{
		width: 100%;
		height: calc(100vh - 100rpx);
		position: relative;
		z-index: 1;
		color: #fff;
	}
	.bottom {
		text-align: center;
		font-size: 24rpx;
		color: #fff;
		margin: auto;
		line-height: 80rpx;
		width: 100%;
	}
</style>
