<template>
	<view class="center">
		<view class="top">
			<image class="top-bg" src="../../static/img/top-bg.png" mode=""></image>
			<view class="title">
				新品
			</view>
		</view>
		<swiper class="carousel" autoplay="true" duration="400" interval="5000" @change="swiperChange">
			<swiper-item class="carousel-item" @click="bannerNavToUrl(item)">
				<image src="../../static/img/new.png" />
			</swiper-item>
		</swiper>
		<view class="hotgoods" v-if="firstList.length != 0">
			<view class="hotgoods-item" v-for="item in firstList" :key="item.id" @click="navToDetailPage(item)">
				<view class="image-wrapper">
					<image :src="item.image" mode="scaleToFill"></image>
				</view>
				<view class="title clamp margin-c-20">{{ item.store_name }}</view>
				<view class="hot-price">
					<view class="price">
						<text class="font-size-sm">￥</text>
						{{ item.price }}
					</view>
				</view>
			</view>
		</view>
		<u-tabbar activeColor="#f42b4e" v-model="current" :list="tabbar"></u-tabbar>
	</view>
</template>

<script>
	import {
		tabbar1
	} from '@/utils/tabbar.js';
	import {
		groomList
	} from '@/api/product.js';
	export default {
		data() {
			return {
				current: 3,
				tabbar: tabbar1,
				firstList: [],
				type: 2,
				page: 1,
				limit: 20,
				loaded: false,
				loadingType: 'more'
			}
		},
		onLoad() {
			this.loadData()
		},
		methods: {
			loadData() {
				let obj = this
				if (obj.loadingType == 'loading' || obj.loadingType == 'noMore') {
					return
				}
				obj.loadingType = 'loading'
				groomList({
						page: obj.page,
						limit: obj.limit
					}, this.type)
					.then(({
						data
					}) => {
						// 保存轮播图
						obj.bannerImg = data.banner;
						// 保存商品信息
						obj.firstList = this.firstList.concat(data.list)
						obj.loaded = true
						obj.page++
						if (obj.limit == data.list.length) {
							obj.loadingType = 'more'
						} else {
							obj.loadingType = 'noMore'
						}
					})
					.catch(e => {
						console.log(e);
					});
			},
			navToDetailPage(e) {
				uni.navigateTo({
					url: '/pages/product/product?id=' + e.id
				})
			}
		}
	}
</script>

<style lang="scss">
	page,
	.center {
		min-height: 100%;
		height: auto;
		background: #FFFFFF;
	}

	.top {
		position: relative;
		width: 100%;
		height: 400rpx;

		.top-bg {
			position: absolute;
			top: 0;
			left: 0;
			right: 0;
			width: 100%;
			height: 100%;
		}

		.title {
			position: relative;
			z-index: 2;
			padding: 30rpx 0;
			font-size: 34rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #FFFFFF;
			text-align: center;
		}
	}

	.carousel {
		width: 700rpx;
		height: 300rpx;
		margin: -280rpx auto 0;

		image {
			width: 700rpx;
			height: 300rpx;
		}
	}

	.hotgoods {
		margin-top: 38rpx;
		width: 100%;
		display: flex;
		flex-wrap: wrap;
		padding: 0 32rpx;

		.hotgoods-item {
			width: 48%;
			background-color: #ffffff;
			border-radius: 12rpx;
			margin-bottom: 24rpx;

			&:nth-child(2n + 1) {
				margin-right: 24rpx;
			}

			.image-wrapper {
				width: 100%;
				height: 330rpx;
				// background: red;
				border-radius: 3px;
				overflow: hidden;

				image {
					width: 100%;
					height: 100%;
					opacity: 1;
					border-radius: 12rpx 12rpx 0 0;
				}
			}

			.title {
				font-size: $font-base;
				color: $font-color-dark;
				font-weight: bold;
				line-height: 80rpx;
			}

			.hot-price {
				display: flex;
				justify-content: space-between;
				padding: 0 16rpx 12rpx;

				.price {
					font-size: 36rpx;
					font-weight: bold;
					color: #FD3B39;
				}

				.cart-icon {
					image {
						width: 44rpx;
						height: 44rpx;
					}
				}
			}
		}
	}
</style>
