<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>
		<view class="tongz flex">
			<view>
				<view class="tongz-bg"><image src="../../static/img/xtxx.png" mode=""></image></view>
				<view class="tongz-left flex">
					<image class="image-left" src="../../static/icon/ld.png" mode=""></image>
					<u-notice-bar
						style="width: 100%;"
						mode="vertical"
						type="none"
						:volume-icon="false"
						:more-icon="true"
						:list="showTitle"
						@click="goDetails"
						@getMore="nav('/pages/user/addmen?id=2')"
					></u-notice-bar>
				</view>
			</view>
		</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> -->
				<view class="good-wrapper">
					<view class="good" v-for="(item, index) in ls.list" :key="index" @click="navToDetailPage(item)">
						<view class="image-wrapper"><image :src="item.image" mode="scaleToFill"></image></view>
						<view class="good-info">
							<view class="info-title clamp2">
								{{item.store_name}}
							</view>
							<view class="info-price">
								<image class="jff" src="../../static/icon/iconU1.png" mode="widthFix"></image>{{item.price}}
							</view>
						</view>
					</view>
				</view>
				<u-loadmore :status="ls.load" />
			</swiper-item>
		</swiper>
	</view>
</template>

<script>
import { loadIndexs, article } 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,
			showList: [],
			showTitle: []
		};
	},
	computed: {
		...mapState('user', ['hasLogin', 'userInfo'])
	},
	onLoad: function(option) {
		// #ifndef MP
		if (option.spread) {
			// 存储其他邀请人
			uni.setStorageSync('spread', option.spread);
		}
		// #endif
		this.init();
		this.getShowList()
	},
	//下拉刷新
	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');
			console.log(this.typeList[e])
			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
			});
		},
		//获取系统消息
		getShowList() {
			article({
				page: 1,
				limit: 3
			},2).then( ({data}) => {
				this.showList = data
				this.showTitle = data.map( item => {
					return item.title
				})
			})
		},
		//系统消息跳转详情
		goDetails(e) {
			console.log(e)
			uni.navigateTo({
				url: '/pages/user/addmenDetail?id=' + this.showList[e].id
			});
		},
		nav(url) {
			// console.log('dddddddddddddddddd')
			uni.navigateTo({
				url: 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;
	}
}
.xtxx {
	width: 100%;
	height: 70rpx;
	position: relative;
	margin: 22rpx 0 20rpx;
	.bg {
		display: inline-block;
		width: 690rpx;
		position: absolute;
		right: 0;
		left: 0;
		margin: 0 auto;
	}
	.ld {
		width: 28rpx;
		height: 34rpx;
		position: absolute;
		top: 18rpx;
		left: 55rpx;
	}
}
.tongz {
	width: 690rpx;
	height: 70rpx;
	margin: 48rpx auto 0;
	padding: 18rpx 30rpx 18rpx 24rpx;
	align-items: center;
	position: relative;
	.tongz-bg {
		position: absolute;
		top: 0;
		right: 0;
		left: 0;
		width: 690rpx;
		height: 70rpx;
		image {
			width: 100%;
			height: 100%;
		}
	}
	.tongz-left {
		width: 640rpx;
		.image-left {
			width: 28rpx;
			height: 34rpx;
		}
		.tongz-font {
			margin-left: 22rpx;
			font-size: 28rpx;
			font-family: Source Han Sans CN;
			font-weight: 400;
			color: #0f253a;
		}
	}
	.tongz-right {
		position: relative;
		z-index: 11;
		width: 12rpx;
		height: 26rpx;
		image {
			width: 100%;
			height: 100%;
		}
	}
}
.good-wrapper {
	padding: 0 30rpx;
	.good {
		width: 681rpx;
		height: 257rpx;
		background: #FFFFFF;
		box-shadow: 0px 0px 15rpx 0px rgba(10, 13, 47, 0.14);
		border-radius: 14rpx;
		margin: 0 auto 20rpx;
		padding: 18rpx 19rpx 17rpx;
		display: flex;
		
		.image-wrapper {
			width: 222rpx;
			height: 222rpx;
			border-radius: 14rpx;
			flex-shrink: 0;
			// background-color: red;
			image {
				width: 100%;
				height: 100%;
				opacity: 1;
				border-radius: 14rpx;
			}
		}
		.good-info {
			padding-left: 22rpx;
			position: relative;
			.info-title {
				padding-top: 20rpx;
				font-size: 32rpx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #333333;
			}
			.info-price {
				font-size: 36rpx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #FFC000;
				line-height: 35px;
				position: absolute;
				bottom: 0;
				left: 22rpx;
				display: flex;
				align-items: center;
			}
		}
		
	}
}
</style>
