<template>
	<view class="container">
		<view class="status_bar"></view>
		<image src="../../static/img/index/in-1.png" mode="" class="top-bg"></image>
		<view class="content-wrap">
			<view class="content-top">
				<!-- <image src="../../static/img/index/in-bg.png" mode="" class="in-bg"></image> -->
				<image src="../../static/img/index/in-tit.png" mode="" class="a-c in-tit" ></image>
				<image src="../../static/img/index/in-2.png" mode="" class="cate cate1" @click="navto('/pages/index/artDetail?id=1')"></image>
				<image src="../../static/img/index/in-3.png" mode="" class="cate cate2" @click="navto('/pages/index/artDetail?id=2')"> </image>
				<image src="../../static/img/index/in-4.png" mode="" class="cate cate3" @click="navto('/pages/user/shareQrCode',1)"></image>
				<image src="../../static/img/index/in-5.png" mode="" class="cate cate4" @click="openKf"></image>
			</view>
			<view class="goods-wrap" v-for="item in goodList">
				<image src="../../static/img/index/goods-tit.png" mode="" class="goods-tit"></image>
				<view class="good-wrap" >
					<image src="../../static/img/index/good-bg.png" mode="" class="good-bg"></image>
					<view class="good">
						<image :src="item.image" mode="" class="good-img"></image>
						<view class="good-tit clamp">
							{{item.store_name}}
						</view>
						<view class="good-key clamp">
							精品中草药 提升身体素质
						</view>
						<view class="good-price">
							<text class="price">{{item.price}}</text><text v-if="item.price-1 < item.ot_price*1" class="ot-price" >￥{{item.ot_price}}</text>
						</view>
						<view class="buy-btn" @click="navTo('/pages/product/product?id=' + item.id)">
							立即购买
						</view>
					</view>
				</view>
			</view>
			<view class="xccg">
				<image src="../../static/img/index/xccg.png" mode="" ></image>
				<view class="a-c btn" @click="navto('/pages/user/xccg',1)">
					
				</view>
			</view>
		</view>
		<uni-popup ref="popupkf" type="center">
			<view class="popup-box">
				<view class="img">
					<image src="../../static/img/img009.png" mode=""></image>
				</view>
				<view class="mian">
					<view class="delivery">
						<view class="title">已经为您定制专属客服</view>
						<image src="../../static/img/img010.png" mode=""></image>
					</view>
					<view class="nocancel">客服VX:{{ text }}</view>
					<view class="comfirm-box">
						<view class="cancel" @click="cancel">取消</view>
						<view class="comfirm" @click="comfirm(text)">复制微信</view>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		loadIndexs,
		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 {
		data() {
			return {
				bastList: [], //爆款推荐
				muted: false,
				text: '', //客服微信
				// page: 1,
				limitt: 20,
				loadingType: 'more',
				current: 0,
				shareShow: false, //分享海报
				pageProportion: 0, //保存页面基于750宽度的比例
				swiperHeight: 0,
				checkid: 0,
				titleNViewBackground: '',
				swiperCurrent: 0,
				swiperLength: 0,
				carouselList: [], //轮播列表
				page: 1,
				limit: 5,
				firstList: [],
				goodList: [], //商品列表
				shopList: [],
				period: 1,
				shareImage: '',
				fgList: '', //复购商品
			};
		},
		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 + '邀请您加入7131', // 默认是小程序的名称(可以写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: '7131', // 默认是小程序的名称(可以写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;
		},
		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.loadData();
		},
		onHide() {},
		methods: {
			gogogo(item) {
				if (!item.path) {
					uni.navigateTo({
						url: '/pages/index/dkf'
					})
				}
				if (item.path == 'kf') {
					this.$refs.popupkf.open()
				} else {

					uni.navigateTo({
						url: item.path + '&tit=' + item.tit,
						fail() {
							uni.navigateTo({
								url: item.path,
								fail() {
									uni.switchTab({
										url: item.path,
									})
								}
							})
						}
					})
				}
			},
			getGoodList() {
				let obj = this
				if (obj.loadingType == 'loading' || obj.loadingType == 'noMore') {
					return
				}
				obj.loadingType = 'loading'
				getProducts({
					is_pack: 1,
					page: 1,
					limit: 50,
				}).then(res => {
					obj.goodList = res.data
					if (obj.limit == res.data.length) {
						obj.loadingType = 'more'
					} else {
						obj.loadingType = 'noMore'
					}
				})
			},
			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.carouselList = data.banner;
						this.swiperLength = this.carouselList.length;
						this.bastList = data.likeInfo; //会员礼包
						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;
	}
	.a-c {
		position: absolute;
		left: 0;
		right: 0;
		margin: auto;
	}
	.top-bg {
		width: 750rpx;
		height: 728rpx;
		display: block;
	}
	.content-wrap {
		position: relative;
		background-image: url('../../static/img/index/in-bg.png');
		background-size: 100%;
		background-repeat: no-repeat;
		background-color: #2c838f;
		padding-bottom: 50rpx;
		.in-bg {
			position: relative;
			width: 100%;
			height: 1924rpx;
			display: block;
		}
		.content-top {
			height: 614rpx;
			width: 100%;
			position: relative;
			
			.in-tit {
				width: 523rpx;
				height: 132rpx;
				top: 104rpx;
			}
			.cate {
				position: absolute;
				width: 164rpx;
				height: 269rpx;
				
			}
			.cate1 {
				top: 228rpx;
				left: 25rpx;
			}
			.cate2 {
				top: 274rpx;
				left: 203rpx;
			}
			.cate3 {
				top: 274rpx;
				left: 382rpx;
			}
			.cate4 {
				top: 228rpx;
				left: 560rpx;
			}
		
		}
		.goods-wrap {
			position: relative;
			.goods-tit {
				width: 530rpx;
				height: 113rpx;
				display: block;
				margin: auto;
			}
			.good-wrap {
				margin: auto;
				width: 680rpx;
				padding-bottom: 50rpx;
				position: relative;
				min-height: 400rpx;
				margin-bottom: 20rpx;
				.good {
					width: 600rpx;
					
					background-color: #fff;
					margin: auto;
					position: relative;
					border-radius: 15rpx;
					padding: 15rpx 10rpx;
					.good-img {
						display: block;
						width: 574rpx;
						height: 280rpx;
						margin: auto;
						background-color: #eee;
					}
					.good-tit {
						font-size: 27rpx;
						font-weight: bold;
						color: #0D0D0D;
						padding:20rpx 16rpx 15rpx;
					}
					.good-key {
						font-size: 22rpx;
						font-weight: 500;
						color: #8C8C8C;
						padding: 0 16rpx 15rpx;
					}
					.good-price {
						padding-left: 15rpx;
						.price {
							font-size: 40rpx;
							font-weight: 800;
							color: #FF6F0F;
							&::before {
								content: '￥';
								font-size: 18rpx;
								color: #FF6F0F;
							}
						}
						.ot-price {
							font-size: 24rpx;
							font-weight: 500;
							text-decoration: line-through;
							color: #999999;
							padding-left: 10rpx;
						}
					}
					.buy-btn {
						width: 170rpx;
						height: 53rpx;
						background: linear-gradient(0deg, #6FCAAD, #2B9C88);
						border-radius: 27rpx;
						text-align: center;
						line-height: 50rpx;
						font-size: 28rpx;
						font-weight: bold;
						color: #FFFFFF;
						position: absolute;
						right: 28rpx;
						bottom: 28rpx;
					}
				}
				.good-bg {
					width: 680rpx;
					height: 346rpx;
					display: block;
					position: absolute;
					bottom: 0;
					
				}
			}
		}
		.xccg {
			width: 673rpx;
			height: 416rpx;
			margin: 80rpx auto 0;
			position: relative;
			image {
				display: block;
				width: 100%;
				height: 100%;
			}
			.btn {
				position: absolute;
				width: 600rpx;
				height: 230rpx;
				bottom: 50rpx;
			}
		}
		
	}

	.status_bar {
		height: var(--status-bar-height);
		width: 100%;
	}
	.popup-box {
		width: 522rpx;
		height: 605rpx;
		background-color: #ffffff;
		border-radius: 20rpx;
		position: relative;
	
		.img {
			position: relative;
			top: -56rpx;
			left: 0;
			width: 522rpx;
			height: 132rpx;
			display: flex;
			justify-content: center;
	
			image {
				border-radius: 20rpx 20rpx 0 0;
				width: 450rpx;
				height: 132rpx;
			}
		}
	
		.mian {
			margin-top: -44rpx;
			display: flex;
			flex-direction: column;
			align-items: center;
			// padding: 32rpx 32rpx;
			background-color: #ffffff;
			border-radius: 0 0 20rpx 20rpx;
			text-align: center;
	
			.delivery {
				font-size: 40rpx;
				color: #333333;
				display: flex;
				align-items: center;
				flex-direction: column;
	
				image {
					margin-top: 48rpx;
					width: 172rpx;
					height: 160rpx;
				}
			}
	
			.nocancel {
				font-size: 32rpx;
				color: #333333;
				margin-top: 14rpx;
			}
	
			.comfirm-box {
				margin-top: 52rpx;
				display: flex;
				.cancel {
					display: flex;
					align-items: center;
					justify-content: center;
					width: 197rpx;
					height: 74rpx;
					border: 1px solid #dcc786;
					border-radius: 38rpx;
	
					font-size: 32rpx;
					color: #605128;
				}
	
				.comfirm {
					margin-left: 32rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					width: 197rpx;
					height: 74rpx;
					background: linear-gradient(-90deg, #d1ba77 0%, #f7e8ad 100%);
					border-radius: 38px;
					font-size: 32rpx;
					color: #605128;
				}
			}
		}
	}
</style>