<template>
	<view class="container">
		<!-- 小程序头部兼容 -->
		<!-- #ifdef MP -->
		<view class="input-box flex" @click="clickSearch">
			<view class=" input-content flex">
				<view class="iconfont iconsearch"></view>
				<view class="input"><input type="text" disabled placeholder="请输入搜索内容" /></view>
			</view>
		</view>
		<view class="mp-height"></view>
		<!-- #endif -->
		<!-- 头部轮播 -->
		<view class="carousel-section">
			<!-- 标题栏和状态栏占位符 -->
			<view class="titleNview-placing"></view>
			<!-- 背景色区域 -->
			<view class="titleNview-background" :style="{ backgroundColor: '#5DBC7C' }"></view>
			<swiper class="carousel" autoplay="true" duration="400" interval="5000" @change="swiperChange">
				<swiper-item v-for="(item, index) in carouselList" :key="index" class="carousel-item" @click="bannerNavToUrl(item)"><image :src="item.pic" /></swiper-item>
			</swiper>
			<!-- 自定义swiper指示器 -->
			<view class="swiper-dots">
				<text class="num">{{ swiperCurrent + 1 }}</text>
				<text class="sign">/</text>
				<text class="num">{{ swiperLength }}</text>
			</view>
		</view>
		<!-- 分类 -->
		<view class="cate-section">
			<navigator url="/pages/product/newPeople">
			<view class="cate-item">
				<image src="/static/img/index-nav1.png"></image>
				<text>新人专区</text>
			</view>
			</navigator>
			<navigator url="/pages/product/exchange">
				<view class="cate-item">
				<image src="/static/img/index-nav2.png"></image>
					<text>兑换专区</text>
				</view>
			</navigator>
			<navigator url="/pages/product/store">
				<view class="cate-item">
				<image src="/static/img/index-nav3.png"></image>
					<text>附近门店</text>
				</view>
			</navigator>
			<navigator url="/pages/index/sign">
				<view class="cate-item">
				<image src="/static/img/index-nav4.png"></image>
					<text>邀请有礼</text>
				</view>
			</navigator>
		</view>
		<!-- 金豆专区 -->
		<golden-bean :data='bastList'></golden-bean>
		<!-- 批发专区 -->
		<wholesale></wholesale>
		<!-- 为你推荐 -->
		<recommend :data='bastList'></recommend>
	</view>
</template>

<script>
import goldenBean from './child/goldenBean.vue'
import wholesale from './child/wholesale.vue'
import recommend from './child/recommend.vue'
import seckill from '../../components/seckill/seckill.vue';
import { loadIndexs } from '@/api/index.js';
import { getUserInfo } from '@/api/user.js';
import { setCoupons } from '@/api/functionalUnit.js';
import { getBargainList } from '@/api/product.js';
import { interceptor } from '@/utils/loginUtils';
import { mapState } from 'vuex';

export default {
	components: {
		seckill,
		goldenBean,
		wholesale,
		recommend
	},
	watch: {
		//自适应swiper高度
		checkid(newValue, oldValue) {
			let obj = this;
			if (newValue == 0) {
				let bHeight = Math.ceil(obj.bastList.length / 2);
				obj.swiperHeight = Math.ceil(obj.pageProportion * 520 * bHeight);
			}
			if (newValue == 1) {
				let bHeight = Math.ceil(obj.goodsList.length / 2);
				obj.swiperHeight = Math.ceil(obj.pageProportion * 520 * bHeight);
			}
			if (newValue == 2) {
				let bHeight = Math.ceil(obj.bastBanner.length / 2);
				obj.swiperHeight = Math.ceil(obj.pageProportion * 520 * bHeight);
			}
		},
		// 初次加载页面高度时修改页面高度
		// bastList(newValue, oldValue) {
		// 	let obj = this;
		// 	let bHeight = Math.ceil(newValue.length / 2);
		// 	obj.$nextTick(function() {
		// 		uni.createSelectorQuery()
		// 			.select('#list-box')
		// 			.fields(
		// 				{
		// 					size: true
		// 				},
		// 				function(data) {
		// 					obj.pageProportion = data.width / 750;
		// 					obj.swiperHeight = Math.ceil(obj.pageProportion * 520 * bHeight);
		// 				}
		// 			)
		// 			.exec();
		// 	});
		// }
	},
	data() {
		return {
			shareShow: false, //分享海报
			pageProportion: 0, //保存页面基于750宽度的比例
			swiperHeight: 0,
			checkid: 0,
			titleNViewBackground: '',
			swiperCurrent: 0,
			swiperLength: 0,
			bargainlist: [], //砍价商品
			carouselList: [], //轮播列表
			goodsList: [], //最新商品列表
			bastList: [], //精品推荐列表
			bastBanner: [], //促销商品列表
			menusList: [], //头部菜单
			page: 1,
			limit: 5,
			userServant: [
				{
					image: ''
				}
			], //用户推广服务
			couponArray: [], //可领取优惠券
			recommend: [
				{
					id: 0,
					re_title: '精选',
					re_name: '为你推荐'
				},
				{
					id: 1,
					re_title: '最新',
					re_name: '最新好货优选'
				},
				{
					id: 2,
					re_title: '便宜好货',
					re_name: '90天便宜好货'
				}
			],
			actTime: new Date()
		};
	},
	computed: {
		...mapState(['loginInterceptor']),
		...mapState('user', ['hasLogin','userInfo'])
	},
	onLoad: function(option) {
		// #ifndef MP
		if (option.spread) {
			// 存储其他邀请人
			uni.setStorageSync('spread', option.spread);
		}
		// #endif
		// #ifdef MP
		if (option.scene) {
			// 存储小程序邀请人
			uni.setStorage({
				key: 'spread_code',
				data: option.scene
			});
		}
		// #endif
	},
	onShow: function() {
		// 判断是否强制登录
		if (this.loginInterceptor && !this.hasLogin) {
			// 登录拦截
			interceptor();
		}
		this.loadData();
		this.getBargainList();
	},
	//下拉刷新
	onPullDownRefresh() {
		this.loadData();
	},
	// #ifndef MP
	// 监听导航栏输入框点击事件
	onNavigationBarSearchInputClicked(e) {
		//跳转到搜索页面
		this.clickSearch();
	},
	//点击导航栏 buttons 时触发
	onNavigationBarButtonTap(e) {
		const index = e.index;
		if (index === 0) {
			this.$api.msg('点击了扫描');
		} else if (index === 1) {
			// #ifdef APP-PLUS
			const pages = getCurrentPages();
			const page = pages[pages.length - 1];
			const currentWebview = page.$getAppWebview();
			currentWebview.hideTitleNViewButtonRedDot({
				index
			});
			// #endif
			uni.navigateTo({
				url: '/pages/user/notice'
			});
		}
	},
	// #endif
	methods: {
		//砍价商品推荐详情页
		navToDetailPages(item) {
			let id = item.product_id;
			//let type = 2;
		
			uni.navigateTo({
				url: '/pages/product/product?id=' + id
			});
		},
		openSubscribe: function(e) {
			let page = e;
			// #ifndef MP
			uni.navigateTo({
				url: page
			});
			// #endif
			// #ifdef MP
			uni.showLoading({
				title: '正在加载'
			});
			openBargainSubscribe()
				.then(res => {
					uni.hideLoading();
					uni.navigateTo({
						url: page
					});
				})
				.catch(err => {
					uni.hideLoading();
				});
			// #endif
		},
		getBargainList() {
			let that = this;
		
			getBargainList({
				page: that.page,
				limit: that.limit
			})
				.then(function(res) {
					that.$set(that, 'bargainlist', res.data.slice(0, 2));
				})
				.catch(res => {
					console.log(res, 'getBargainList');
				});
		},
		Mask() {
			this.MaskShow = false;
			this.shareShow = true;
			uni.setStorage({
				key: 'FirstEntry',
				data: true,
				success: function() {
					console.log(uni.getStorageSync('FirstEntry'), 'Mask');
				}
			});
		},
		Toshare() {
			if (this.userInfo == '') {
				getUserInfo({})
					.then(({ data }) => {
						this.setUserInfo(data);
						this.userInfo = data;
					})
					.catch(e => {});
			} else {
				this.shareShow = false;
				uni.navigateTo({
					url: '/pages/user/shareQrCode?spread=' + this.userInfo.uid
				});
			}
		},
		Tocancel() {
			this.shareShow = false;
		},
		// 监听切换事件
		listChange(e) {
			this.checkid = e.detail.current;
		},
		// 點擊搜索框
		clickSearch() {
			uni.navigateTo({
				url: '/pages/product/search'
			});
		},
		// 点击触发领取优惠券
		setCoupons(item) {
			// 判断是否已经领取了优惠券
			let obj = this;
			uni.showModal({
				title: '领取提示',
				content: '是否领取优惠券',
				success(e) {
					if (e.confirm) {
						setCoupons({ couponId: item.id }).then(e => {
							item.is_use = true;
							uni.showToast({
								title: '领取成功',
								type: 'top',
								duration: 2000
							});
						});
					}
				}
			});
		},
		//商品种类切换
		change(item) {
			let id = item;
			this.checkid = id;
			if (this.checkid == 1) {
				// console.log(1);
				this.detail = this.selected_detail;
			} else if (this.checkid == 2) {
				// console.log(2);
				this.detail = this.new_product;
			} else {
				this.detail = this.cheap_good;
			}
		},
		// 监听图片加载完成
		onImageError(key, index) {
			this[key][index].image = '/static/error/errorImage.jpg';
		},
		// 请求载入数据
		async loadData() {
			loadIndexs({})
				.then(({ data }) => {
					let goods = data.info;
					this.carouselList = data.banner;
					this.swiperLength = this.carouselList.length;
					this.menusList = data.menus;
					this.goodsList = goods.firstList; //最新商品
					this.bastList = goods.bastList; //精品推荐
					this.bastBanner = data.benefit; //促销单品
					this.$set(this, 'couponArray', data.couponList); //保存卡包券

					uni.stopPullDownRefresh();
				})
				.catch(e => {
					uni.stopPullDownRefresh();
				});
		},
		//轮播图切换修改背景色
		swiperChange(e) {
			const index = e.detail.current;
			this.swiperCurrent = index;
			this.titleNViewBackground = this.carouselList[index].background;
		},
		//详情页
		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">
.Mask {
	width: 100%;
	height: 100vh;
	position: fixed;
	z-index: 99999;
	background-color: rgba(0, 0, 0, 0.7);
	top: 0;
	image {
		width: 100%;
		height: 100vh;
	}
}
/* #ifdef MP */
.mp-height {
	height: 44px;
}
.input-box {
	position: fixed;
	top: 0;
	left: 0;
	width: 100%;
	padding: 25rpx;
	background-color: #ffffff;
	z-index: 999;
	height: 44px;
	.iconsearch {
		font-size: 50rpx;
	}
	.input-content {
		border-radius: 99rpx;
		flex-grow: 1;
		padding: 10rpx 30rpx;
		background-color: rgba(231, 231, 231, 0.7);
		.input {
			flex-grow: 1;
			input {
				font-size: $font-lg;
			}
		}
	}
	.input-button {
		padding-left: 20rpx;
		font-size: $font-lg;
		height: 100%;
	}
}
page {
	.cate-section {
		position: relative;
		z-index: 5;
		border-radius: 16rpx 16rpx 0 0;
		margin-top: -20rpx;
	}
	.carousel-section {
		padding: 0;
		.titleNview-placing {
			padding-top: 0;
			height: 0;
		}
		.carousel {
			.carousel-item {
				padding: 0;
			}
		}
		.swiper-dots {
			left: 45rpx;
			bottom: 40rpx;
		}
	}
}
/* #endif */

page {
	background: #f7f8f7;
}
.m-t {
	margin-top: 16rpx;
}
/* 头部 轮播图 */
.carousel-section {
	position: relative;
	padding-top: 10px;
	overflow: hidden;

	.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: 100%;
		height: 240rpx;
		.carousel-item {
			width: 100%;
			height: 100%;
			padding: 0 28rpx;
			overflow: hidden;
		}

		image {
			width: 100%;
			height: 100%;
			border-radius: $border-radius-sm;
		}
	}
}
.swiper-dots {
	display: flex;
	position: absolute;
	left: 60rpx;
	bottom: 15rpx;
	width: 72rpx;
	height: 36rpx;
	background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAABkCAYAAADDhn8LAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyZpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNi1jMTMyIDc5LjE1OTI4NCwgMjAxNi8wNC8xOS0xMzoxMzo0MCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tbS8iIHhtbG5zOnN0UmVmPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvc1R5cGUvUmVzb3VyY2VSZWYjIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtcE1NOkRvY3VtZW50SUQ9InhtcC5kaWQ6OTk4MzlBNjE0NjU1MTFFOUExNjRFQ0I3RTQ0NEExQjMiIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5paWQ6OTk4MzlBNjA0NjU1MTFFOUExNj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TgE0c+QJZUUz1b2T4SBbIKmJW+3iMj2SBVBWz+leVfCQLpIqYbp8b85EskIxyfIOfK5Sf+wiCRJEsllQ+oqEkQfBxmD8BBgA5hVjXyrBNUQAAAABJRU5ErkJggg==);
	background-size: 100% 100%;
	.num {
		width: 36rpx;
		height: 36rpx;
		border-radius: 50px;
		font-size: 24rpx;
		color: #fff;
		text-align: center;
		line-height: 36rpx;
	}

	.sign {
		position: absolute;
		top: 0;
		left: 50%;
		line-height: 36rpx;
		font-size: 12rpx;
		color: #fff;
		transform: translateX(-50%);
	}
}
/* 分类 */
.cate-section {
	background-color: #fff;
	display: flex;
	justify-content: space-around;
	align-items: center;
	flex-wrap: wrap;
	padding: 30rpx 22rpx;
	.cate-item {
		display: flex;
		flex-direction: column;
		align-items: center;
		font-size: $font-sm + 2rpx;
		color: $font-color-dark;
	}
	/* 原图标颜色太深,不想改图了,所以加了透明度 */
	image {
		width: 140rpx;
		height: 140rpx;
		// margin-bottom: 5rpx;
		border-radius: 50%;
		opacity: 0.7;
	}
}
/*公用边框样式*/
%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;
}

</style>
