<template>
	<view class="container">
		<!-- 头部轮播 -->
		<view class="carousel-section">
			<!-- 标题栏和状态栏占位符 -->
			<view class="titleNview-placing"></view>
			<swiper class="carousel" autoplay="true" duration="400" interval="5000" >
				<swiper-item v-for="(item, index) in carouselList" :key="index" class="carousel-item" @click="bannerNavToUrl(item)"><image :src="item.pic" /></swiper-item>
			</swiper>
		</view>
		<u-tabs :list="typeList" active-color="#4E6CFF"   name='name' bg-color='transparent' :current="typeIndex" @change="changeType"></u-tabs>
		<swiper class="list-box" @change="listChange" :style="{ height: swiperHeight + 'px' }" :current="typeIndex" >
			<swiper-item v-for="ls in typeList">
				<view class="guess-section">
					<view v-for="(item, index) in ls.list" :key="index" class="guess-item" @click="navToDetailPage(item)">
						<view class="image-wrapper"><image :src="item.image" mode="scaleToFill"></image></view>
						<text class="title clamp2 margin-c-20">{{ item.store_name }}</text>
						<view class="cmy-hr"></view>
						<view class="price margin-c-20 flex">
							<view>
								<image class="iconJf" src="../../static/icon/iconU1.png" mode="widthFix"></image>
								{{ item.price }}
							</view>
							<view class="font-size-sm">
								<text class="font-color-gray">{{ item.sales }}人购买</text>
							</view>
						</view>
					</view>
				</view>
				<u-loadmore :status="ls.load" />
			</swiper-item>
		</swiper>
	</view>
</template>

<script>
import { loadIndexs } from '@/api/index.js';
import { getUserInfo } from '@/api/user.js';
import { interceptor } from '@/utils/loginUtils';
import { mapState } from 'vuex';
import { getCategoryList, getProducts} from '@/api/product.js';

export default {
	data() {
		return {
			shareShow: false, //分享海报
			pageProportion: 0, //保存页面基于750宽度的比例
			swiperHeight: 0,
			swiperLength:0,
			carouselList: [], //轮播列表
			page: 1,
			limit: 5,
			// 商品分类数据
			typeList:[{
				
			}],
			//保存当前选中的分类
			typeIndex:0
		};
	},
	computed: {
		...mapState('user', ['hasLogin','userInfo'])
	},
	onLoad: function(option) {
		// #ifndef MP
		if (option.spread) {
			// 存储其他邀请人
			uni.setStorageSync('spread', option.spread);
		}
		// #endif
		this.init();
	},
	//下拉刷新
	onPullDownRefresh() {
		this.init();
	},
	// #ifndef MP
	// 监听导航栏输入框点击事件
	onNavigationBarSearchInputClicked(e) {
		//跳转到搜索页面
		this.clickSearch();
	},
	// #endif
	onReachBottom(){
		this.getProducts(this.typeList[this.typeIndex])
	},
	methods: {
		// 初始化页面数据
		init(){
			this.loadData();
			// 获取分类
			this.getType();
			// 获取页面初始比例
			this.getBl();
		},
		// 获取初始化页面比例
		getBl(){
			let obj = this;
			obj.$nextTick(function() {
				uni.createSelectorQuery()
					.select('.container')
					.fields(
						{
							size: true
						},
						function(data) {
							obj.pageProportion = data.width / 750;
						}
					)
					.exec();
			});
		},
		// 设置切换高度
		getlistHeight(item){
			let obj = this;
			let bHeight = Math.ceil(item.length / 2);
			obj.swiperHeight = Math.ceil(obj.pageProportion * 520 * bHeight+30);
		},
		// tabs切换
		changeType(e){
			console.log(e,'dianji');
			this.typeIndex = e;
			// 判断是否已经加载过数据
			const item = this.typeList[this.typeIndex]
			if(!item.loding){
				this.getProducts(item)
			}
		},
		// sw切换
		listChange(e){
			if(e.detail.current!=this.typeIndex){
				this.typeIndex=e.detail.current;
				// 判断是否已经加载过数据
				const item = this.typeList[this.typeIndex]
				if(!item.loding){
					this.getProducts(item)
				}
			}
		},
		//商品详情页
		navToDetailPages(item) {
			let id = item.product_id;
			//let type = 2;
			uni.navigateTo({
				url: '/pages/product/product?id=' + id
			});
		},
		// 获取商品分类
		getType(){
			const obj = this;
			getCategoryList({})
				.then(({ data }) => {
					obj.typeList = data.map(function(s) {
						return {
							name:s.cate_name,
							id:s.id,
							page:1,
							limit:10,
							list:[],
							load:'loadmore',//loadmore/loading / nomore
							isLoding:false//用于判断是否已经载入过分类
						};
					});
					// 加载商品
					obj.getProducts(this.typeList[this.typeIndex])
				})
				.catch(err => {
					console.log(err);
				});
		},
		// 获取商品信息
		getProducts(item){
			// 判断是否已经没有数据
			if(item.load=='nomore'){
				return ;
			}
			// 判断是否加载中
			if(item.load=='loading'){
				return ;
			}
			// 判断是否为初次加载
			if(!item.isLoding){
				item.isLoding = true;
			}
			// 设置开始加载
			item.load = 'loading';
			getProducts({
				cid:item.id,
				page:item.page,
				limit:item.limit
			}).then((e) => {
				item.list =item.list.concat( e.data);
				if(item.list.length==item.limit){
					item.page++
					item.load = 'loadmore';
				}else{
					item.load = 'nomore';
				}
				// 初始化高度
				this.getlistHeight(item.list)
			}).catch((e) => {
				console.log(e);
			})
		},
		// 點擊搜索框
		clickSearch() {
			uni.navigateTo({
				url: '/pages/product/search'
			});
		},
		// 请求载入数据
		async loadData() {
			loadIndexs({})
				.then(({ data }) => {
					let goods = data.info;
					this.carouselList = data.banner;
					this.swiperLength = this.carouselList.length;
					this.menusList = data.menus;
					uni.stopPullDownRefresh();
				})
				.catch(e => {
					uni.stopPullDownRefresh();
				});
		},
		//详情页
		navToDetailPage(item) {
			let id = item.id;
			uni.navigateTo({
				url: '/pages/product/product?id=' + id
			});
		},
		// 轮播图跳转
		bannerNavToUrl(item) {
			// #ifdef H5
			if (item.wap_url.indexOf('http') > 0) {
				window.location.href = item.wap_url;
			}
			// #endif
			//测试数据没有写id，用title代替
			uni.navigateTo({
				url: item.wap_url
			});
		}
	}
};
</script>

<style lang="scss">
page {
	background: #f7f8f7;
}
.m-t {
	margin-top: 16rpx;
}
.list-box{
	margin-top: 30rpx;
}
/* 头部 轮播图 */
.carousel-section {
	position: relative;
	padding-top: 10px;
	overflow: hidden;
	background: url(../../static/img/indexBg.png) no-repeat;
	background-size: 100% 100%;
	.titleNview-placing {
		height: var(--status-bar-height);
		padding-top: 44px;
		box-sizing: content-box;
	}

	.titleNview-background {
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		height: 426rpx;
		transition: 0.4s;
	}
	.carousel {
		width: 680rpx;
		height: 340rpx;
		margin: 0 auto;
		.carousel-item {
			width: 100%;
			height: 100%;
			overflow: hidden;
		}

		image {
			width: 100%;
			height: 100%;
			border-radius: $border-radius-sm;
		}
	}
}
/*公用边框样式*/
%icon {
	margin-right: 10rpx;
	display: inline-block;
	padding: 2rpx 10rpx;
	border: 1rpx solid $color-yellow;
	color: $color-yellow;
	line-height: 1;
	font-size: $font-base;
	border-radius: 10rpx;
}
.f-header {
	display: flex;
	align-items: center;
	height: 92rpx;
	padding: 6rpx 30rpx 8rpx;
	image {
		flex-shrink: 0;
		width: 80rpx;
		height: 80rpx;
		margin-right: 20rpx;
	}
	.tit-box {
		@extend %font-title-box;
	}
	.tit {
		@extend %font-title;
	}
	.iconenter {
		font-size: $font-lg + 2rpx;
		color: $font-color-light;
	}
	.f-left-icon {
		@extend %f-left-icon;
	}
	.iconfont {
		font-size: $font-sm - 2rpx;
	}
}
/* 商品列表 */
.guess-section {
	display: flex;
	flex-wrap: wrap;
	padding: 0 30rpx;
	.guess-item {
		overflow: hidden;
		display: flex;
		flex-direction: column;
		width: 48%;
		margin-bottom: 4%;
		border-radius: $border-radius-sm;
		background-color: white;
		box-shadow: $box-shadow;
		&:nth-child(2n + 1) {
			margin-right: 4%;
		}
	}
	.image-wrapper {
		width: 100%;
		height: 330rpx;
		border-radius: 3px;
		overflow: hidden;
		image {
			width: 100%;
			height: 100%;
			opacity: 1;
		}
	}
	.title {
		padding-top: 10rpx;
		font-size: $font-base;
		color: $font-color-dark;
		font-weight: bold;
		line-height: 1.5;
	}
	.price {
		font-size: $font-lg;
		color: $base-color;
		font-weight: bold;
		line-height: 1;
		line-height: 80rpx;
		.iconJf{
			width: 23rpx;
			margin-right: 5rpx;
		}
	}

	.icon {
		@extend %icon;
	}

	.detail {
		line-height: 1;
	}
	.tip {
		color: white;
		background-color: $color-yellow;
		line-height: 1.5;
		font-size: $font-sm;
		padding-left: 20rpx;
	}
}
// 列表
.list-box-h {
	height: 1550rpx;
}
.row {
	border-radius: 15rpx;
	margin: 25rpx;
	height: 155rpx;
	// width: 552rpx;
	overflow: hidden;
	background-color: #ffffff;
	padding-right: 25rpx;
	.list-interval {
		border: 1px dashed $border-color-light;
		height: 100%;
		.top,
		.bottom {
			border-radius: 100rpx;
			width: 30rpx;
			height: 30rpx;
			position: absolute;
			background-color: $page-color-base;
			right: -15rpx;
		}
		.top {
			top: -18rpx;
		}
		.bottom {
			bottom: -18rpx;
		}
	}
	.list-money {
		height: 100%;
		min-width: 155rpx;
		text-align: center;
		image {
			height: 100%;
			width: 20rpx;
		}
		.list-money-text {
			flex-grow: 1;
			padding: 0 25rpx;
			.tit {
				text-align: center;
				padding: 15rpx 0rpx;
				font-size: 55rpx;
				color: $color-red;
				font-weight: bold;
				&.noAction {
					color: $font-color-light;
				}
			}
			.price {
				padding-bottom: 25rpx;
				color: $font-color-light;
			}
		}
	}
	.row_list_right {
		// flex-grow: 1;
		min-width: 200rpx;
		padding-left: 25rpx;
		line-height: 1;
		.right_time {
			padding: 10rpx 0rpx;
			color: $font-color-light;
			font-size: $font-sm;
		}
		.right_top {
			margin: 15rpx 0;
			.right_name {
				font-size: $font-base;
				color: #bc253a;
				font-weight: bold;
			}
			.right_title {
				font-size: $font-base;
				color: $font-base;
				font-weight: bold;
				&.noAction {
					color: $font-color-light;
				}
			}
		}
	}
	.right_use {
		margin: 15rpx 0;
		padding: 10rpx;
		width: 160rpx;
		text-align: center;
		color: #fff;
		background-color: #bc253a;
		border-radius: 50rpx;
		font-size: $font-sm;
		&.noAction {
			background-color: $font-color-light;
		}
	}
	.iconlocation {
		font-size: 36rpx;
		color: $font-color-light;
	}
}
</style>
