<template>
	<view class="container">
		<!-- <image src="" mode=""></image> -->
		<!-- 顶部logo and 搜索 start-->
		<view class="topbox">
			<view class="index-top-wrap position-relative">
				<view class="status_bar position-relative"></view>
				<view class="top-search flex position-relative">
					<view class="search-box flex" @click="clickSearch()">
						<image class="search" src="../../static/icon/search-h.png" mode=""></image>
						<view class="search-font">输入关键词搜索</view>
					</view>
				</view>
				<!-- 顶部logo and 搜索 end-->
				<!-- <view class="jg fud" style="background-color: #fff;"></view> -->
				<!-- 轮播图 start -->
				<swiper class="top-swiper fud" 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>
				<!-- 轮播图 end -->
			</view>
			<!-- 分类 start -->
			<view class="cate-section flex">
				<view class="cate-item flex-center" @click="navto('/pages/category/category')">
					<image class="img-wrapper" src="../../static/icon/indextop1.png" mode=""></image>
					<view class="item-title">全部商品</view>
				</view>
				<view class="cate-item flex-center" @click="navto('/pages/index/message?type=2')">
					<image class="img-wrapper" src="../../static/icon/indextop2.png" mode=""></image>
					<view class="item-title">公司介绍</view>
				</view>
				<view class="cate-item flex-center" @click="openKf">
					<image class="img-wrapper" src="../../static/icon/indextop3.png" mode=""></image>
					<view class="item-title">联系客服</view>
				</view>
				<view class="cate-item flex-center" @click="navto('/pages/user/shareQrCode',1)">
					<image class="img-wrapper" src="../../static/icon/indextop4.png" mode=""></image>
					<view class="item-title">邀请好友</view>
				</view>
			</view>
			<!-- 分类 ed -->
			<view class="tongz">
				<view class="tongz-left">
					<!-- <image class="image-left" src="../../static/icon/shou-gonggao.png" mode=""></image> -->
					<u-notice-bar class="notice" mode="link" type="none" direction='column' :text="actext"
						@click="navto('/pages/index/message?type=1')"></u-notice-bar>
				</view>
			</view>
		</view>
		<view class="product-item">
			<!-- 商品列表 start -->
			<view class="product-box">
				<view class="product-tit flex">
					<view class="flex">
						<image class="leftTip" src="../../static/icon/indextip2.png" mode="widthFix"></image>
						<view class="tit">
							会员礼包
						</view>
					</view>
					<view class="flex" @click="navTo(`/pages/product/list?fid=&sid=4&tid=7`)">
						<view class="more">
						更多
						</view>
						<image class="rightTip" src="../../static/icon/back.png" mode="widthFix"></image>
					</view>
				</view>
			</view>
			<view class="good-list">
				<view class="good" v-for="item in goodList2" @click="navTo('/pages/product/product?id=' + item.id)">
					<image :src="item.image" mode="" class="good-image"></image>
					<view class="good-name ">
						<view class="clamp2">
							{{item.store_name}}
						</view>
					</view>
					<view class="good-price">
						<view class="price">
							￥{{item.price}}
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="product-item">
			<!-- 商品列表 start -->
			<view class="product-box">
				<view class="product-tit flex">
					<view class="flex" >
						<image class="leftTip" src="../../static/icon/indextip2.png" mode="widthFix"></image>
						<view class="tit">
							复购专区
						</view>
					</view>
					<view class="flex" @click="navTo(`/pages/product/list?fid=&sid=3&tid=8`)">
						<view class="more">
						更多
						</view>
						<image class="rightTip" src="../../static/icon/back.png" mode="widthFix"></image>
					</view>
				</view>
			</view>
			<view class="good-list">
				<view class="good" v-for="item in goodList3" @click="navTo('/pages/product/product?id=' + item.id)">
					<image :src="item.image" mode="" class="good-image"></image>
					<view class="good-name ">
						<view class="clamp2">
							{{item.store_name}}
						</view>
					</view>
					<view class="good-price">
						<view class="price">
							￥{{item.price}}
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="product-item">
			<!-- 商品列表 start -->
			<view class="product-box">
				<view class="product-tit flex">
					<view class="flex">
						<image class="leftTip" src="../../static/icon/indextip2.png" mode="widthFix"></image>
						<view class="tit">
							热销推荐
						</view>
					</view>
					<view class="flex" @click="navTo(`/pages/product/list?fid=&sid=5&tid=6`)">
						<view class="more">
						更多
						</view>
						<image class="rightTip" src="../../static/icon/back.png" mode="widthFix"></image>
					</view>
				</view>
			</view>
			<view class="good-list">
				<view class="good" v-for="item in goodList" @click="navTo('/pages/product/product?id=' + item.id)">
					<image :src="item.image" mode="" class="good-image"></image>
					<view class="good-name ">
						<view class="clamp2">
							{{item.store_name}}
						</view>
					</view>
					<view class="good-price">
						<view class="price">
							￥{{item.price}}
						</view>
					</view>
				</view>
			</view>
		</view>
	
		
		<!-- 商品列表 ed-->
		<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,
		article
	} from '@/api/index.js';
	import {
		getUserInfo,
	} from '@/api/user.js';
	import {
		getProducts,
	} 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 {
				text: 'shxsophina', //客服微信
				aclist: [],
				actext: [],
				titleNViewBackground: '',
				swiperCurrent: 0,
				carouselList: [], //轮播列表
				goodList: [], //热销推荐
				goodList2: [], //会员礼包
				goodList3: [], //复购专区
			};
		},

		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;
		},
		onLoad: function(option) {
			// #ifdef MP
			if (option.scene) {
				// 存储小程序邀请人
				uni.setStorage({
					key: 'spread_code',
					data: option.scene
				});
			}
			// #endif
		},
		onShow: function() {
			this.getGoodList()
			this.loadData();
		},
		methods: {
			getGoodList() {
				let obj = this
				getProducts({
					page: 1,
					limit: 3,
					sid: 6
				}).then(res => {
					obj.goodList = res.data;
				})
				getProducts({
					page: 1,
					limit: 3,
					sid: 7
				}).then(res => {
					obj.goodList2 = res.data;
				})
				getProducts({
					page: 1,
					limit: 3,
					sid: 8
				}).then(res => {
					obj.goodList3 = res.data;
				})
			},
			navto(url, type = 0) {
				console.log(url);
				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() {
				const that = this;
				article({}, 1).then(({
					data
				}) => {
					that.aclist = data.map((e) => {
						that.actext.push(e.title)
						return e
					})

				});
				loadIndexs({})
					.then(({
						data
					}) => {
						let goods = data.info;
						console.log(goods, '商品信息');
						that.carouselList = data.banner;
						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">
	.topbox{
		background-color: #FFFFFF;
		padding-bottom: 2rpx;
	}
	.product-item{
		background-color: #FFFFFF;
		padding: 30rpx;
		margin-top: 30rpx;
	}
	// 顶部搜索
	.top-search {
		height: 80rpx;
		padding: 0 20rpx;
		margin-bottom: 20rpx;
		// background-color: #fff;

		.top-logo {
			width: 50rpx;
			// height: 50rpx;
			margin-right: 10rpx;

			image {
				width: 48rpx;
			}

		}

		.search-box {
			justify-content: center;
			width: 698rpx;
			height: 60rpx;
			background: #EEEEEE;
			// box-shadow: 0px 10rpx 20rpx 0px rgba(4, 114, 69, 0.22);
			border-radius: 30rpx;

			.search {
				width: 34rpx;
				height: 34rpx;
			}

			.search-font {
				margin-left: 14rpx;
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #CBCBCB;
			}
		}
	}

	// 顶部轮播图
	.top-swiper {
		width: 100%;
		height: 365rpx;
		border-radius: 20rpx;
		overflow: hidden;

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


	.jg {
		height: 20rpx;
		background: #fff;
	}

	// 分类
	.cate-section {
		justify-content: space-around;
		background-color: #fff;
		padding: 0rpx 0 10rpx;
		margin-top: 20rpx;
		flex-wrap: wrap;

		.cate-item {
			flex-grow: 0;
			width: 25%;
			flex-direction: column;
			padding-bottom: 20rpx;

			.img-wrapper {
				width: 90rpx;
				height: 90rpx;
				border-radius: 20rpx;
				position: relative;
			}

			.item-title {
				margin-top: 15rpx;
				font-size: 26rpx;
				font-weight: 500;
				color: #666666;
			}
		}
	}

	.product-box {
		.product-tit {
			.leftTip{
				width:36rpx;
			}
			.tit {
				font-size: 32rpx;
				font-weight:bold;
				color: #4A2723;
				padding-left: 10rpx;
			}
			.more{
				color: #999999;
				font-size: 24rpx;
				padding-right: 10rpx;
			}
			.rightTip{
				width:12rpx;
			}
		}
	}


	.popup-box {
		width: 522rpx;
		height: 605rpx;
		background-color: #ffffff;
		border-radius: 20rpx;
		position: relative;

		.product-tit {
			justify-content: center;

			.hx {
				width: 120rpx;
				height: 1rpx;
				background: #989898;
			}

			.tit {
				margin: 0 70rpx;
				font-size: 30rpx;
				font-weight: 500;
				color: #4A2723;
			}
		}

		.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;
				}
			}
		}
	}

	.good-list {
		display: flex;
		justify-content: flex-start;
		flex-wrap: wrap;
		padding-top: 20rpx;
		.good {
			width: 207rpx;
			height: 330rpx;
			border-radius: 14rpx;
			position: relative;
			margin-right: 34rpx;
			&:nth-child(3n){
				margin-right: 0rpx;
			}
			.good-image {
				width: 100%;
				height: 207rpx;
				border-radius: 14rpx ;
			}

			.good-name {
				font-size: 26rpx;
				font-weight: bold;
				color: #333333;
			}

			.good-price {
				font-size: 24rpx;
				font-weight: bold;
				color: #FF1A1A;
				position: absolute;
				bottom: 10rpx;
			}
		}
	}

	.tongz {
		margin: 0 30rpx 30rpx 30rpx;
		border-radius: 20rpx;
		overflow: hidden;
	}
</style>