<template>
	<div style="background: #f5f5f5; padding-bottom: 1rpx;">
		<view class="tabbox">
			<up-tabs
				:current="active"
				keyName="name"
				:list="list"
				:lineColor="Color"
				:activeStyle="{ color: Color }"
				@click="onClickTab">
			</up-tabs>
		</view>
		<template v-if="topList.length != 0">
			<div class="box" v-for="(item, index) in topList" :key="index" @click="hanlejump(item.id)">
				<div class="top" v-if="index == 0">
					<image src="https://ct-upimg.yx090.com/ju8hn6/shop/image/2022/06/15/pIUXxlbHScMtNzIY3JBUeZ24aUyEpRgUL7LvS63R.png" alt="" />
				</div>
				<div class="top" v-if="index == 1">
					<image src="https://ct-upimg.yx090.com/ju8hn6/shop/image/2022/06/15/tgO5hRzXcYYmswSb1N2M8JWKcWPMxhkfdVsDXd95.png" alt="" />
				</div>
				<div class="top" v-if="index == 2">
					<image src="https://ct-upimg.yx090.com/ju8hn6/shop/image/2022/06/15/I9Pz4UKUUwHvu9xHCMGPboZuE6OhjhsqfcYXF3kk.png" alt="" />
				</div>
				<div class="img">
					<image :src="item.face_img + '?x-oss-process=image/format,webp'" :webp="true" mode="aspectFill" alt="" />
				</div>
				<div class="head">
					<div class="h1">{{item.title}}</div>
					<div class="buttom">
						<div class="num">
							<text class="icon">￥</text>
							<text v-if="item.price > 0">{{item.price}}</text>
							<block v-else>
								<text v-if="item.max_price === item.min_price">{{item.min_price}}</text>
								<text v-else>{{item.min_price * 1}}~{{item.max_price}}</text>
							</block>
						</div>
						<div class="icon">
							<up-icon name="shopping-cart" :color="Color" size="25" />
						</div>
					</div>
				</div>
			</div>
		</template>
		
		<view v-if="topList.length && page >= last_page && !loading">
			<up-loadmore status="nomore" />
		</view>
		<view v-if="!topList.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-loadmore status="loading" />
		</view>

	</div>
</template>

<script>
	import { ref, reactive, toRefs, onMounted } from 'vue'
	import { get, post } from '@/api/request.js'
	import { Style } from '@/utils/list.js'
	
	export default {
		setup() {
			const data = reactive({
				active: 0,
				topList: [],
				last_page: 0,
				page: 1,
				list: [],
				num: '',
				id: '',
				type: '',
				Color: '',
				priceColor: '',
				loading: false
			})
			
			const hanlejump = (id) => {
				if (data.type == 1) {
					uni.navigateTo({
						url: '/pages/productDetails/index?id=' + id
					})
				} else {
					uni.navigateTo({
						url: '/pages/groups/index?id=' + id
					})
				}
			}
			
			const onClickTab = (e) => {
				data.active = e.index
				data.list.forEach((item, index) => {
					if (index === data.active) {
						data.num = item.id
					}
				})
				data.topList = []
				data.page = 1
				gettop()
			}
			
			async function getList() {
				let list = []
				let res
				if (data.type == 1) {
					res = await get('/api/app/category')
					res.data.forEach((item) => {
						list.push(item)
					})
				} else {
					res = await get('/api/app/index', {
						shop_id: 1,
						from: 2
					})
					res.data.groups.forEach((item) => {
						if (item.is_more === 0) {
							list.push(item)
						}
					})
				}

				list.unshift({
					id: '',
					name: '全部商品'
				})
				data.list = list
			}

			const gettop = async () => {
				data.loading = true
				uni.showLoading({
					title: '加载中...'
				})
				let res
				if (data.type == 1) {
					res = await get('/api/app/special/goods/top', {
						page: data.page,
						pageSize: 20,
						category_id: data.num
					})
				} else {
					res = await get('/api/app/special/top', {
						page: data.page,
						pageSize: 20,
						shop_group_id: data.num
					})
				}

				data.topList = data.topList.concat(res.data)
				data.last_page = res.meta.last_page
				uni.hideLoading()
				data.loading = false
			}
			
			return {
				...toRefs(data),
				gettop,
				onClickTab,
				hanlejump,
				getList
			}
		},
		onReachBottom() {
			if (this.page < this.last_page) {
				this.page++
				this.gettop()
			}
		},
		async onLoad(options) {
			// await this.$onLaunched
			this.Color = uni.getStorageSync('theme_color')
			this.priceColor = Style[uni.getStorageSync('theme_index') * 1].priceColor
			this.type = options.type
			this.gettop()
			this.getList()
		}
	}
</script>

<style lang="scss" scoped>
	.tabbox{
		background-color: #fff;
		position: sticky;
		top: 0;
	}
	.box {
		position: relative;
		display: flex;
		justify-content: space-between;
		background: #fff;
		padding: 25rpx 18rpx;
		box-sizing: border-box;
		margin: 20rpx 10rpx;
		border-radius: 10px;
		.top {
			position: absolute;
			top: 0;
			left: 3%;
			width: 75rpx;
			height: 96rpx;
			z-index: 2;
			image {
				width: 100%;
				height: 100%;
			}
		}
		.img {
			width: 30%;
			image {
				width: 200rpx;
				height: 200rpx;
			}
		}

		.head {
			width: 65%;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			.h1 {
				font-size: 30rpx;
				word-break: break-all;
				text-overflow: ellipsis;
				display: -webkit-box;
				-webkit-box-orient: vertical;
				-webkit-line-clamp: 2;
				overflow: hidden;
			}
			.buttom {
				display: flex;
				justify-content: space-between;
				margin-top: 30rpx;
				.num {
					color: v-bind('priceColor');
					font-size: 30rpx;
				}
			}
		}
	}
	.empty{
		width: 100%;
		height: calc(100vh - 100rpx);
	}
</style>
