<template>
	<view class="container">
		<!-- 顶部logo and 搜索 start-->
		<view class="status_bar"></view>
		<view class="bg">
			<image src="../../static/img/index-bg.png" mode="widthFix"></image>
		</view>
		<view class="index-title">
			<image class="index-title-bg" src="../../static/img/index-title.png" mode="widthFix"></image>
		</view>
		<!-- 分类start -->
		<view class="care">
			<view class="title">
				<image class="title-icon" src="../../static/img/index-icon.png" mode="widthFix"></image>
				<view class="title-font">傣方美颜 · 解锁福利</view>
				<view class="title-font title-tip">走进滇润堂</view>
			</view>
			<view class="care-main flex">
				<view class="care-item shang" @click="navto('/pages/category/category')">
					<image src="../../static/img/index1.png" mode="widthFix"></image>
				</view>
				<view class="care-item" @click="navto('/pages/index/artDetail?id=1')">
					<image src="../../static/img/index2.png" mode="widthFix"></image>
				</view>
				<view class="care-item" @click="navto('/pages/user/shareQrCode',1)">
					<image src="../../static/img/index3.png" mode="widthFix"></image>
				</view>
				<view class="care-item shang" @click="openKf()">
					<image src="../../static/img/index4.png" mode="widthFix"></image>
				</view>
			</view>
		</view>
		<!-- 分类end -->
		<!-- 明星商品start -->
		<view class="gift-box">
			<view class="title">
				<image class="title-icon" src="../../static/img/index-icon.png" mode="widthFix"></image>
				<view class="title-font">古方养颜 · 傣族美学</view>
				<view class="title-font title-tip">明星礼包套餐</view>
			</view>
			<view class="gift">
				<view class="gift-item" v-for="(item,index) in goodList"
					@click="navto('/pages/product/product?id=' + item.id)">
					<view class="gift-bg">
						<image src="../../static/img/gift-bg.png" mode=""></image>
					</view>
					<view class="gift-image">
						<image class="gift-image-bg" :src="item.image" mode=""></image>
						<view class="gift-image-title">
							<view class="gift-title-bg">
								<image src="../../static/img/gift-title.png" mode=""></image>
							</view>
							<!-- <view class="gift-title-font">
								爆款
								直降
							</view> -->
						</view>
					</view>
					<view class="gift-info">
						<view class="gift-title">明星产品 清润养肤</view>
						<view class="gift-title name clamp">{{ item.store_name }}</view>
						<view class="gift-btn-box">
							<view class="gift-price">
								钜惠价: <text class="gift-num">￥{{ item.price*1 }}</text>
							</view>
							<view class="gift-btn">
								立即抢购 >
							</view>
						</view>
					</view>

				</view>
			</view>
		</view>
		<!-- 明星商品end -->
		<!-- 普通商品start -->
		<view class="shop-box">
			<view class="title">
				<image class="title-icon" src="../../static/img/index-icon.png" mode="widthFix"></image>
				<view class="title-font">古方养颜 · 傣族美学</view>
				<view class="title-font title-tip">爆款商品为您推荐</view>
			</view>
			<view class="shop-main flex">
				<view class="shop-item" v-for="(item,index) in firstList"
					@click="navto('/pages/product/product?id=' + item.id)">
					<view class="shop-bg">
						<image src="../../static/img/shop-bg.png" mode=""></image>
					</view>
					<view class="shop-image">
						<image class="shop-image-bg" :src="item.image" mode=""></image>
						<!-- <view class="shop-image-title">
							<view class="shop-title-bg">
								<image src="../../static/img/shop-title.png" mode=""></image>
							</view>
						</view> -->
					</view>
					<view class="shop-info">
						<view class="shop-title">花容清透 养肤美颜</view>
						<view class="shop-title shop-name clamp">{{ item.store_name }}</view>
						<view class="shop-btn-box">
							<view class="shop-price">
								<!-- 钜惠价: --> <text class="shop-num">￥{{ item.price*1 }}</text>
							</view>
							<view class="shop-btn">
								立即抢购 >
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 普通商品end -->
		<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">客服1VX:{{ text1 }}</view>
					<view class="nocancel">客服2VX:{{ text2 }}</view>
					<view class="comfirm-box">
						<view class="cancel" @click="comfirm(text1)">复制客服1</view>
						<view class="comfirm" @click="comfirm(text2)">复制客服2</view>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		loadIndexs,
		groom
	} 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 {
				text1: 'yangqianqian131419', //客服1微信
				text2: 'AHFM134', //客服2微信
				page: 1,
				limit: 10,
				loadingType: 'more',
				swiperHeight: 0,
				checkid: 0,
				titleNViewBackground: '',
				swiperCurrent: 0,
				swiperLength: 0,
				carouselList: [], //轮播列表
				firstList: [],
				goodList: [], //商品列表
				shopList: []
			};
		},
		computed: {
			...mapState(['loginInterceptor', 'baseURL', 'weichatObj']),
			...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, // 默认是当前页面，必须是以‘/’开头的完整路径
					desc: this.userInfo.nickname + '邀请您加入滇润堂',
					imgUrl: '../../static/img/logo.png',
					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等)
					desc: '滇润堂',
					path: url, // 默认是当前页面，必须是以‘/’开头的完整路径
					imgUrl: '../../static/img/logo.png',
					success: function(res) {
						// 转发成功之后的回调
						if (res.errMsg == 'shareAppMessage:ok') {}
					},
					fail: function() {
						// 转发失败之后的回调
						if (res.errMsg == 'shareAppMessage:fail cancel') {
							// 用户取消转发
						} else if (res.errMsg == 'shareAppMessage:fail') {
							// 转发失败，其中 detail message 为详细失败信息
						}
					}
				};
			}
			console.log(shareObj);

			return shareObj;
		},
		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
				});
				uni.setStorage({
					key: 'spread',
					data: option.scene
				});
			}
			// #endif
			// #ifdef H5
			this.IndexShare();
			//#endif
		},
		onShow: function() {
			this.getGoodList();
			this.loadData();
		},
		methods: {
			...mapMutations('user', ['setUserInfo']),
			// #ifdef H5
			IndexShare() {
				let obj = this;
				let pages = getCurrentPages();
				// 获取当前页面
				let page = pages[pages.length - 1];
				let path = '#/' + page.route + '?';
				// 保存传值
				for (let i in page.options) {
					path += i + '=' + page.options[i] + '&';
				}
				console.log(obj.Path);
				// 保存邀请人
				path += 'spread=' + this.userInfo.uid;
				let data = {
					link: this.baseURL + '/index/' + path,
					title: this.userInfo.nickname + '邀请您进入滇润堂',
					desc: '欢迎加入滇润堂',
					imgUrl: 'https://drt.liuniukj.com/logo.png'
				};
				console.log(data, '分享数据');
				shareLoad(data);
			},
			// #endif
			getGoodList() {
				let obj = this;
				if (obj.loadingType == 'loading' || obj.loadingType == 'noMore') {
					return;
				}
				obj.loadingType = 'loading';
				getProducts({
					page: 1,
					limit: 1000,
					set_vip: 1
				}).then(res => {
					obj.goodList = res.data;
					if (obj.limit == res.data.length) {
						obj.page++;
						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;
						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;
				}
				// #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: #c3e3e0;
	}

	.bg {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		width: 750rpx;

		image {
			width: 100%;
		}
	}

	.index-title {
		position: relative;
		z-index: 2;
		width: 582rpx;
		height: 585rpx;
		padding-top: 100rpx;
		margin: 0 auto;

		.index-title-bg {
			width: 100%;
		}
	}

	.title {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;

		.title-icon {
			width: 30rpx;
		}

		.title-font {
			margin-top: 20rpx;
			position: relative;
			z-index: 2;
			font-size: 42rpx;
			font-family: Source Han Serif SC;
			font-weight: bold;
			color: #2E6D5C;
			// text-shadow: 0rpx 0rpx 0rpx #FFFFFF;

			background: linear-gradient(-84deg, #235144 0%, #3a8a74 100%);
			-webkit-background-clip: text;
			-webkit-text-fill-color: transparent;
		}

		.title-tip {
			font-size: 26rpx;
		}
	}

	.care {
		margin-top: 54rpx;

		.care-main {
			justify-content: space-around;
			margin-top: 25rpx;
			padding: 0 20rpx;

			.care-item {
				position: relative;
				z-index: 2;
				width: 180rpx;

				image {
					width: 100%;
				}
			}

			.shang {
				top: -50rpx;
			}
		}
	}

	.gift-box {
		margin-top: 40rpx;

		.gift {
			margin-top: 36rpx;

			.gift-item {
				position: relative;
				width: 690rpx;
				margin: 0 auto 60rpx;
				display: flex;
				align-items: flex-start;
				padding: 50rpx 38rpx 56rpx;

				.gift-bg {
					position: absolute;
					top: 0;
					left: 0;
					right: 0;
					width: 100%;
					height: 100%;

					image {
						width: 100%;
						height: 100%;
					}
				}

				.gift-image {
					position: relative;
					z-index: 2;
					width: 233rpx;
					height: 233rpx;
					background: #FFFFFF;
					border-radius: 13rpx;

					.gift-image-bg {
						width: 233rpx;
						height: 233rpx;
						border-radius: 13rpx;
					}

					.gift-image-title {
						position: absolute;
						top: -30rpx;
						right: -20rpx;
						width: 90rpx;
						height: 90rpx;

						.gift-title-bg {
							position: absolute;
							top: 0;
							right: 0;
							left: 0;
							width: 90rpx;
							height: 90rpx;

							image {
								width: 100%;
								height: 100%;
							}
						}

						.gift-title-font {
							text-align: center;
							position: relative;
							z-index: 2;
							font-size: 20rpx;
							font-family: Source Han Serif SC;
							font-weight: bold;
							color: #FFF3E4;

						}
					}
				}

				.gift-info {
					width: 60%;
					margin-left: 32rpx;
					position: relative;

					.gift-title {
						font-size: 38rpx;
						font-family: Source Han Serif SC;
						font-weight: bold;
						color: #2E6D5C;

						background: linear-gradient(-84deg, #235144 0%, #3a8a74 100%);
						-webkit-background-clip: text;
						-webkit-text-fill-color: transparent;
					}

					.name {
						margin-top: 20rpx;
						font-size: 24rpx;
					}

					.gift-btn-box {
						width: auto;
						margin-top: 90rpx;
						display: flex;
						justify-content: space-between;
						align-items: center;
						background: #FFFFFF;
						border: 1px solid #F26150;
						border-radius: 34rpx;
						height: 50rpx;

						.gift-price {
							padding-left: 10rpx;
							font-size: 18rpx;
							font-family: FZYanSongS-DB-GB;
							font-weight: 400;
							color: #E36847;

							.gift-num {
								font-size: 30rpx;
							}
						}

						.gift-btn {
							padding: 0 20rpx;
							height: 50rpx;
							margin-left: 20rpx;
							background: #E36847;
							border-radius: 34rpx;
							display: flex;
							justify-content: center;
							align-items: center;
							font-size: 17rpx;
							font-family: PingFang SC;
							font-weight: bold;
							color: #FFFFFF;
						}
					}
				}
			}
		}
	}

	.shop-box {
		margin-top: 40rpx;

		.shop-main {
			margin: 36rpx 0 0 36rpx;
			flex-wrap: wrap;
			justify-content: flex-start;

			.shop-item {
				position: relative;
				width: 356rpx;
				margin: 0 0 60rpx;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				padding: 44rpx 50rpx 10rpx 40rpx;

				.shop-bg {
					position: absolute;
					top: 0;
					left: 0;
					right: 0;
					width: 100%;
					height: 100%;

					image {
						width: 100%;
						height: 100%;
					}
				}

				.shop-image {
					position: relative;
					z-index: 2;
					width: 230rpx;
					height: 230rpx;
					background: #FFFFFF;
					border-radius: 13rpx;

					.shop-image-bg {
						width: 233rpx;
						height: 233rpx;
						border-radius: 13rpx;
					}

					.shop-image-title {
						position: absolute;
						top: -30rpx;
						right: -20rpx;
						width: 72rpx;
						height: 72rpx;

						.shop-title-bg {
							position: absolute;
							top: 0;
							right: 0;
							left: 0;
							width: 72rpx;
							height: 72rpx;

							image {
								width: 100%;
								height: 100%;
							}
						}

						.shop-title-font {
							text-align: center;
							position: relative;
							z-index: 2;
							font-size: 20rpx;
							font-family: Source Han Serif SC;
							font-weight: bold;
							color: #FFF3E4;

						}
					}
				}

				.shop-info {
					position: relative;
					z-index: 2;
					width: 100%;
				}

				.shop-title {
					margin-top: 20rpx;
					font-size: 32rpx;
					font-family: Source Han Serif SC;
					font-weight: bold;
					color: #2E6D5C;

					background: linear-gradient(-84deg, #235144 0%, #3a8a74 100%);
					-webkit-background-clip: text;
					-webkit-text-fill-color: transparent;
				}

				.shop-name {
					text-align: center;
					margin-top: 20rpx;
					font-size: 20rpx;
				}

				.shop-btn-box {
					margin-top: 12rpx;
					display: flex;
					justify-content: space-between;
					align-items: center;
					background: #FFFFFF;
					border: 1px solid #F26150;
					border-radius: 34rpx;
					height: 50rpx;

					.shop-price {
						padding-left: 10rpx;
						font-size: 12rpx;
						font-family: FZYanSongS-DB-GB;
						font-weight: 400;
						color: red;

						.shop-num {
							font-size: 30rpx;
						}
					}

					.shop-btn {
						padding: 0 20rpx;
						height: 50rpx;
						margin-left: 20rpx;
						background: #E36847;
						border-radius: 34rpx;
						display: flex;
						justify-content: center;
						align-items: center;
						font-size: 14rpx;
						font-family: PingFang SC;
						font-weight: bold;
						color: #FFFFFF;
					}
				}
			}
		}
	}

	.popup-box {
		width: 522rpx;
		height: 625rpx;
		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;
				// margin-bottom: 32rpx;

				// justify-content: space-around;
				.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>