<template>
	<view class="content">
		<!-- 顶部logo and 搜索 start-->
		<!-- #ifdef APP -->
		<view class="status_bar"></view>
		<!-- #endif -->
		<swiper class="top-swiper" 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>
		<!-- 轮播图 end -->
		<view class="tip flex" v-if="auth == 1 && prod.length> 0">
			<image src="../../static/index/hg.png" mode=""></image>
			<view class="">
				<text>/</text>礼包商品
			</view>
		</view>
		<view class="goods-list flex" v-if="auth == 1">
			<view class="goods" v-for="item in prod" @click="navTo('/pages/product/product?id=' + item.id)">
				<image :src="item.image" mode="" class="logo"></image>
				<view class="price flex">
					<view class="val">
						￥{{item.price}}
					</view>
					<view class="more">
						立即加入>
					</view>
				</view>
			</view>
		</view>
		<template v-if="auth == 0">
			<view class="tip flex" >
				<image src="../../static/index/hg.png" mode=""></image>
				<view class="">
					<text>/</text>会员联盟
				</view>
			</view>
			<view class="goods-list flex" >
				<view class="goods" v-for="item in miniList" @click="navTo('/pages/product/product?id=' + item.id)">
					<image :src="item.image" mode="" class="logo"></image>
					<view class="price flex">
						<view class="val">
							￥{{item.price}}
						</view>
						<view class="more">
							立即加入>
						</view>
					</view>
				</view>
			</view>
			<view class="big-goods-list">
				<view class="big-good" v-for="item in bigList" @click="navTo('/pages/product/product?id=' + item.id)">
					<image :src="item.image" mode="" class="logo"></image>
					<view class="price flex">
						<view class="val">
							￥{{item.price}}
						</view>
						<view class="more">
							立即加入>
						</view>
					</view>
				</view>
			</view>
			<image v-if="list.length> 0" :src="list[0].image_input[0]" mode="widthFix" style="display: block;margin: 38rpx 38rpx;width: 671rpx;"></image>
		</template>
	</view>
</template>

<script>
	import {
		xzwNotoce
	} from '@/components/xzw-notice/xzw-notice.vue'
	import {
		loadIndexs,
		getArtList,
		groom1,
		
	} from '@/api/index.js';
	import {
		getUserInfo,
		spread
	} from '@/api/user.js';
	import {
		getBargainList,
		getProducts,
		goodsDetail,
		poster
	} from '@/api/product.js';
	import {
		saveUrl,
		interceptor
	} from '@/utils/loginUtils';
	import {
		mapState,
		mapMutations
	} from 'vuex';
	// #ifdef H5
	import {
		weixindata,
		shareLoad
	} from '@/utils/wxAuthorized';
	// #endif
	export default {
		components: {
			xzwNotoce
		},
		data() {
			return {
				miniList: [],
				bigList: [],
				prod: [],
				auth: 1,
				list: [],
				muted: false,
				// page: 1,
				limitt: 20,
				loadingType: 'more',
				current: 0,
				shareShow: false, //分享海报
				titleNViewBackground: '',
				swiperCurrent: 0,
				swiperLength: 0,
				carouselList: [], //轮播列表
				page: 1,
				limit: 5,
				firstList: [],
				goodList: [], //商品列表
				shopList: [],
				period: 1,
				shareImage: '',
				fgList: '', //复购商品
				bastList:[],
				levelList:[],
			};
		},
		computed: {
			...mapState('user', ['hasLogin', 'userInfo'])
		},
		onShareAppMessage(options) {
			// 设置菜单中的转发按钮触发转发事件时的转发内容
			let pages = getCurrentPages(); //获取加载的页面
			let currentPage = pages[pages.length - 1]; //获取当前页面的对象
			let url = currentPage.route; //当前页面url
			let item = currentPage.options; //如果要获取url中所带的参数可以查看options
			let shareObj = {}
			if (this.userInfo.uid) {
				shareObj = {
					title: this.userInfo.nickname + '邀请您加入阿格莱亚-馨美研', // 默认是小程序的名称(可以写slogan等)
					path: url + '?scene=' + this.userInfo.uid, // 默认是当前页面，必须是以‘/’开头的完整路径
					imageUrl: '',
					success: function(res) {
						// 转发成功之后的回调
						if (res.errMsg == 'shareAppMessage:ok') {}
					},
					fail: function() {
						// 转发失败之后的回调
						if (res.errMsg == 'shareAppMessage:fail cancel') {
							// 用户取消转发
						} else if (res.errMsg == 'shareAppMessage:fail') {
							// 转发失败，其中 detail message 为详细失败信息
						}
					}
				};
			} else {
				shareObj = {
					title: '阿格莱亚-馨美研', // 默认是小程序的名称(可以写slogan等)
					path: url, // 默认是当前页面，必须是以‘/’开头的完整路径
					imageUrl: '',
					success: function(res) {
						// 转发成功之后的回调
						if (res.errMsg == 'shareAppMessage:ok') {}
					},
					fail: function() {
						// 转发失败之后的回调
						if (res.errMsg == 'shareAppMessage:fail cancel') {
							// 用户取消转发
						} else if (res.errMsg == 'shareAppMessage:fail') {
							// 转发失败，其中 detail message 为详细失败信息
						}
					}
				};
			}

			return shareObj;
		},
		onReachBottom() {
		},
		onLoad: function(option) {
			// #ifdef MP
			if (option.scene) {
				// 存储小程序邀请人
				uni.setStorage({
					key: 'spread_code',
					data: option.scene
				});
			}
			// #endif
			// #ifdef H5
			if (option.spread) {
				// 存储小程序邀请人
				uni.setStorage({
					key: 'spread',
					data: option.spread
				});
			}
			// #endif
		},
		onShow: function() {
			this.getGoodList()
			// this.getLevelList()
			this.loadData();
			this.getArtList();
			this.getProduct()
		},
		methods: {
			getProduct() {
				getProducts().then(res => {
					this.prod = res.data
				})
			},
			onload(e) {
				console.log("onload--广告加载完成");

			},
			onclose(e) {
				console.log("广告关闭: " + e);
			},
			onerror(e) {
				console.log('广告error：', e);
			},
			getArtList() {
				getArtList({
					page: 1,
					limit: 100
				}, 1).then(({
					data
				}) => {
					this.list = data;
				});
			},
			goItem(e) {
				console.log('bbbbbbbbbb', e);
				uni.navigateTo({
					url: '/pages/index/artDetail?id=' + e.id
				})
			},
			goMore() {
				console.log('dddddddddd');
				uni.navigateTo({
					url: '/pages/index/artList?id=2'
				})
			},
			gogogo(item) {
				if (!item.path) {
					uni.navigateTo({
						url: '/pages/index/dkf'
					})
				} else {

					uni.navigateTo({
						url: item.path + '&tit=' + item.tit,
						fail() {
							uni.navigateTo({
								url: item.path
							})
						}
					})
				}
			},
			getGoodList() {
				let obj = this
				getProducts({
					is_pack: 1,
					page: 1,
					limit: 50,
				}).then(res => {
					obj.goodList = res.data
					getProducts({
						is_level: 1,
						page: 1,
						limit: 50,
					}).then(res => {
						obj.goodList = obj.goodList.concat(res.data)
						obj.miniList = obj.goodList.slice(0,2)
						obj.bigList = obj.goodList.slice(2)
						console.log(obj.miniList,'obj.miniList')
						console.log(obj.bigList)
					})
				})
			},
			getLevelList() {
				let obj = this
				getProducts({
					is_level: 1,
					page: 1,
					limit: 50,
				}).then(res => {
					obj.levelList = res.data.sort((a, b) => a.price - b.price);
				})
			},
			navto(url, type = 0) {
				if (type == 1) {
					if (!this.hasLogin) {
						// 保存地址
						saveUrl();
						// 登录拦截
						interceptor();
					} else {
						uni.navigateTo({
							url,
							fail() {
								uni.switchTab({
									url
								})
							}
						})
					}
				} else {
					uni.navigateTo({
						url,
						fail() {
							uni.switchTab({
								url
							})
						}
					})
				}

			},
			navTo(url) {
				if (url == '') {
					this.$api.msg('暂未开通，敬请期待');
				} else {
					this.navto(url)
				}
			},
			// 點擊搜索框
			clickSearch() {
				uni.navigateTo({
					url: '/pages/product/search'
				});
			},
			// 监听图片加载完成
			onImageError(key, index) {
				this[key][index].image = '/static/error/errorImage.jpg';
			},
			// 请求载入数据
			async loadData() {
				loadIndexs({})
					.then(({
						data
					}) => {
						let goods = data.info;
						console.log(goods, '商品信息');
						this.auth = data.auth;
						this.carouselList = data.banner;
						this.swiperLength = this.carouselList.length;
						this.bastList = data.benefit;
						data.info.firstList.forEach(e => {
							e.isVip = e.store_type ? "3" : "0"
						})
						this.firstList = data.info.firstList //首页商品
						uni.stopPullDownRefresh();
					})
					.catch(e => {
						uni.stopPullDownRefresh();
					});
			},
			//轮播图切换修改背景色
			swiperChange(e) {
				const index = e.detail.current;
				this.swiperCurrent = index;
				this.titleNViewBackground = this.carouselList[index].background;
			},
			// 轮播图跳转
			bannerNavToUrl(item) {
				// #ifdef H5
				console.log(item.wap_url.indexOf('http'), 'banner');
				if (item.wap_url.indexOf('http') >= 0) {
					window.location.href = item.wap_url;
				}
				// uni.navigateTo({
				// 	url: '/pages/zero/order'
				// })
				// #endif
				//测试数据没有写id，用title代替
				uni.navigateTo({
					url: item.wap_url
				});
			},
			comfirm(text) {
				console.log(text);
				const result = this.uniCopy(text);
				if (result === false) {
					uni.showToast({
						title: '不支持'
					});
				} else {
					uni.showToast({
						title: '复制成功',
						icon: 'none'
					});
				}
				this.$refs.popupkf.close();
			},
			uniCopy(content) {
				/**
				 * 小程序端 和 app端的复制逻辑
				 */
				//#ifndef H5
				uni.setClipboardData({
					data: content,
					success: function() {
						console.log('success');
						return true;
					}
				});
				//#endif

				/**
				 * H5端的复制逻辑
				 */
				// #ifdef H5
				if (!document.queryCommandSupported('copy')) {
					//为了兼容有些浏览器 queryCommandSupported 的判断
					// 不支持
					return false;
				}
				let textarea = document.createElement('textarea');
				textarea.value = content;
				textarea.readOnly = 'readOnly';
				document.body.appendChild(textarea);
				textarea.select(); // 选择对象
				textarea.setSelectionRange(0, content.length); //核心
				let result = document.execCommand('copy'); // 执行浏览器复制命令
				textarea.remove();
				return result;
				// #endif
			},
			// 打开客服
			openKf() {
				this.$refs.popupkf.open();
			},
			// 关闭客服
			cancel() {
				this.$refs.popupkf.close();
			},
		}
	};
</script>

<style lang="scss">
	page {
		min-height: 100%;
		height: auto;
		background-color: #ebebeb;
	}


	// 顶部轮播图
	.top-swiper {
		width: 750rpx;
		height: 644rpx;
		margin: auto;

		.carousel-item {
			image {
				width: 100%;
				height: 100%;
				border-radius: 20rpx;
			}
		}

	}


	
	.status_bar {
		height: var(--status-bar-height);
		width: 100%;
	}

	.goods-list {
		padding: 0 38rpx;
		flex-wrap: wrap;

		.goods {
			width: 321rpx;
			height: 223rpx;
			border-radius: 20rpx;
			background-color: #fff;
			margin-bottom: 20rpx;

			.logo {
				display: block;
				width: 100%;
				height: 167rpx;
				border-radius: 20rpx;
			}

			.price {
				height: 66rpx;
				padding: 0 10rpx;

				.val {
					font-weight: bold;
					font-size: 32rpx;
					color: #7E0000;
				}

				.more {
					font-weight: bold;
					font-size: 18rpx;
					color: #000000;
				}
			}
		}
	}

	.tip {
		justify-content: flex-start;
		font-weight: bold;
		font-size: 36rpx;
		color: #000000;
		// margin-left: ;
		padding-left: 37rpx;
		margin: 38rpx 0;

		image {
			width: 44rpx;
			height: 40rpx;
		}

		text {
			color: #7E0000;
			padding: 0 8rpx;
		}

	}
	.goods-list {
		padding: 0 35rpx;
		flex-wrap: wrap;
	
		.good {
			width: 324rpx;
			height: 460rpx;
			background: #FFFFFF;
			border-radius: 25rpx;
			margin-bottom: 20rpx;
			position: relative;
			image {
				width: 324rpx;
				height: 324rpx;
			}
			&:last-of-type {
				margin-bottom: 0;
			}
			.pic {
				width: 324rpx;
				height: 324rpx;
				border-radius: 25rpx;
			}
	
			.store-name {
				width: 285rpx;
				margin: auto;
				font-weight: bold;
				color: #A42527;
				margin: 10rpx 0;
				padding-left: 20rpx;
			}
			.key {
				font-weight: 500;
				font-size: 17rpx;
				color: #654427;
				text-align: center;
				padding:20rpx  0 ;
			}
			.price {
				width: 268rpx;
				height: 52rpx;
				background-size: 100% 100%;
				position: absolute;
				left: 0;
				right: 0;
				bottom: 30rpx;
				// margin: auto;
				padding:0 16rpx;
				.val {
					font-weight: bold;
					font-size: 34rpx;
					color: #7E0000;
					text {
						padding-left: 10rpx;
						font-weight: 500;
						font-size: 24rpx;
						color: #9A9A9A;
						text-decoration-line: line-through;
					}
				}
				
			}
		}
	}
	.big-goods-list {
		.big-good {
			width: 674rpx;
			height: 395rpx;
			margin: 20rpx auto;
			border-radius: 25rpx;
			background-color: #fff;
		}
		.logo {
			width: 674rpx;
			height: 313rpx;
		}
		.price {
			padding:10rpx 20rpx;
			.val {
				font-weight: bold;
				font-size: 39rpx;
				color: #7E0000;
			}
			.more {
				font-weight: bold;
				font-size: 22rpx;
				color: #000000;
			}
		}
	}
</style>