<template>
	<view class="container">
		<!-- 顶部logo and 搜索 start-->
		<view class="status_bar"></view>
		<view class="top-bg"></view>
		<view class="top-search flex">
			<view class="search-box flex" @click="clickSearch()">
				<image class="search" src="../../static/icon/search-w.png" mode=""></image>
				<view class="search-font">输入关键词搜索</view>
			</view>
		</view>
		<!-- 顶部logo and 搜索 end-->
		<view class="jg" style="background-color: #fff;"></view>
		<!-- 轮播图 start -->
		<swiper class="top-swiper" 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.image" />
			</swiper-item>
		</swiper>
		<!-- 轮播图 end -->
		<!-- 分类 start -->
		<!-- 分类 ed -->
		<view class="jg">
		</view>
		<view class="store-wrap">
			<image src="/static/img/index/store1.png" mode="" class="bn1"></image>
			<view class="store" v-for="(item,index) in storeList" :key="index" @click="goStore(item)">
				<view class="store-info flex">
					<image :src="item.logo" class="store-logo" mode=""></image>
					<view class="store-base">
						<view class="name clamp">
							{{item.name}}
						</view>
						<view class="address clamp2">
							{{item.address}}
						</view>
						<view class="time">
							<!-- 营业时间: 08:30~18:30 -->
							{{item.status_name}}
						</view>
					</view>
					<view class="store-dis">
						{{item.range}}km
					</view>
				</view>
				<view class="goods-wrap" v-if="item.goods.length > 0">
					<scroll-view scroll-x="true" style="width: 684rpx;">
						<view class="flex">
							<view class="good" v-for="(good,indeg) in item.goods" :key="indeg">
								<image :src="good.image" class="good-img" mode=""></image>
								<view class="good-name clamp">
									{{good.store_name}}
								</view>
								<view class="goood-price">
									￥{{good.price}}
								</view>
							</view>
						</view>

					</scroll-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 { getStoreList } from '@/api/store.js'
	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 {
				menu: [],
				storeList: [],
				bastList: [], //爆款推荐
				muted: false,
				cateList: [],
				text: '13105637866', //客服微信
				// 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,
				latitude: 0,
				longitude:0
			};
		},
		computed: {
			...mapState('user', ['hasLogin', 'userInfo']),
			...mapState('baseUrl'),
		},
		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: '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;
		},
		onReachBottom() {
			console.log('到底', this.baseUrl)
		},
		onLoad: function(option) {
			// #ifdef MP
			if (option.scene) {
				// 存储小程序邀请人
				uni.setStorage({
					key: 'spread_code',
					data: option.scene
				});
			}
			// #endif
			// #ifdef H5
			shareLoad()
			if (option.spread) {
				// 存储小程序邀请人
				uni.setStorage({
					key: 'spread',
					data: option.spread
				});
			}
			// #endif
		},
		onShow: function() {
			this.loadData();
			this.getLocation()
		},
		onHide() {},
		onReachBottom() {
			this.getStoreList()
		},
		methods: {
			goStore(item) {
				uni.setStorageSync('storeInfo',item)
				uni.navigateTo({
					url: '/pages/category/category?id=' + item.id
				})
			},
			getStoreList() {
				console.log(this.loadingType ,'this.loadingType ')
				if(this.loadingType == 'noMore' || this.loadingType == 'loading') return;
				this.loadingType = 'loading'
				try {
					getStoreList({
						store: 1,
						latitude: this.latitude,
						longitude: this.longitude,
						page: this.page,
						limit: this.limit
					}).then(res => {
						let arr = res.data.list.list
						this.storeList = this.storeList.concat(arr)
						if(arr.length == this.limit) {
							this.loadingType = 'more'
						}else {
							this.loadingType = 'noMore'
						}
						this.page++
						
					})
				} catch (error) {
					console.log(error,'errr');
					//TODO handle the exception
				}
				
			},
			getLocation() {
				let that = this
				uni.getLocation({
					type: 'gcj02',
					success(res) {
						that.latitude = res.latitude
						that.longitude = res.longitude
						uni.setStorageSync('lat',res.latitude)
						uni.setStorageSync('lng',res.longitude)
						console.log(res)
						that.getStoreList()
					},
					fail(err) {
						if (err.errMsg.includes('auth deny')) {
							// that.showAuthGuide().then(res => {
							// 	console.log(res);
							// 	that.getLocation()
							// })
							uni.showModal({
								title: '权限提示',
								content: '需要您授权地理位置权限以正常使用服务',
								confirmText: '去设置',
								complete(e) {
									if (e.comfirm) {
										wx.openSetting({
											success(settingRes) {
												if (settingRes.authSetting['scope.userLocation']) {
													// 用户重新授权后再次获取位置
													that.getLocation()
												} else {
													// uni.showToast({
													// 	title: '授权未开启',
													// 	icon: 'none'
													// })
													
												}
											}
										})
									}
								}
							})
						}
					}
				})
			},
			gogogo(item) {
				let url = item.link + '&tit=' + item.tit + '&isgift=0'
				if (url.indexOf('http') != -1) {
					// #ifdef H5
					window.location.href = url
					// #endif
				} else if (url == '' || url == '#' || url == '/' || url.length < 2) {
					uni.navigateTo({
						url: '/pages/index/dkf'
					})
				} else if (url == 'kf') {
					this.$refs.popupkf.open()
				} else if (url == 'gongzhonghao') {

				} else {
					uni.navigateTo({
						url,
						fail() {
							uni.switchTab({
								url,
							})
						}
					})
				}
			},
			getGoodList() {
				let obj = this
				if (obj.loadingType == 'loading' || obj.loadingType == 'noMore') {
					return
				}
				obj.loadingType = 'loading'
				getProducts({
					page: 1,
					limit: 50,
				}).then(res => {
					obj.goodList = res.data.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.$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
					}) => {
						if (data.to_url) {
							window.open(data.to_url)
						}
						let goods = data.info;
						this.carouselList = data.banner;
						this.menu = data.menu
						console.log(this.carouselList, '商品信息');
						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 //首页商品
						if (data.lovely.length > 0) {
							this.cateList = data.lovely
						}
						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,
					fail() {
						uni.switchTab({
							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" scoped>
	page {
		min-height: 100%;
		height: auto;
	}

	// 顶部搜索
	.top-search {
		height: 80rpx;
		padding: 0 20rpx;
		position: relative;

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

			image {
				width: 48rpx;
			}

		}

		.search-box {
			justify-content: center;
			width: 698rpx;
			height: 60rpx;
			background: rgba(255, 255, 255, 0.5);
			color: #fff;
			// 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-weight: 500;
				color: #fff;
			}
		}
	}

	// 顶部轮播图
	.top-swiper {
		background-color: #fff;
		width: 690rpx;
		height: 320rpx;
		margin: auto;

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

		// margin: 20rpx 0 0;

	}

	.swiper-btm {
		height: 60rpx;
		width: 750rpx;
		background-color: #fff;
		margin-bottom: 20rpx;
		font-size: 26rpx;
		font-weight: 500;
		color: #333333;

		.btm-item {
			flex-grow: 1;
			justify-content: center;

			image {
				width: 25rpx;
				height: 25rpx;
				margin-right: 14rpx;
			}
		}
	}

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

	// 分类
	.cate-section {
		justify-content: flex-start;
		background-color: #fff;
		padding: 0 0 30rpx;
		flex-wrap: wrap;

		.cate-item {
			padding-top: 30rpx;
			flex-grow: 0;
			width: 20%;
			flex-direction: column;
			text-align: center;
			align-items: center;
			justify-content: center;

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

				image {
					width: 100%;
					height: 100%;
					position: absolute;
					left: 50%;
					top: 50%;
					transform: translate(-50%, -50%);
				}
			}

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

	.hot-wrap {
		background-color: #fff;
		padding-top: 20rpx;

		.tit {
			display: block;
			height: 40rpx;
			margin: 20rpx auto 40rpx;
		}

		.hot-list {
			// margin-top: 38rpx;
			width: 100%;
			display: flex;
			flex-wrap: wrap;
			padding: 0 20rpx 30rpx;
			justify-content: space-between;

			.hotgoods-item {
				width: 345rpx;
				background-color: #ffffff;
				border-radius: 12rpx;
				box-shadow: 0 0 15rpx rgba(0, 0, 0, 0.2);
				margin-bottom: 15rpx;

				.image-wrapper {
					width: 345rpx;
					height: 345rpx;
					border-radius: 3px;
					overflow: hidden;
					position: relative;

					.image-bg {
						position: absolute;
						top: 0;
						left: 0;
						right: 0;
						bottom: 0;
						width: 100%;
						height: 100%;
						opacity: 1;
						border-radius: 12rpx 12rpx 0 0;
						z-index: 2;
					}

					.image {
						width: 100%;
						height: 100%;
						opacity: 1;
						border-radius: 12rpx 12rpx 0 0;
					}
				}

				.title {
					margin-top: 24rpx;
					padding: 0 20rpx;
					font-size: 32rpx;
					font-weight: 500;
				}

				.hot-price {
					display: flex;
					justify-content: flex-start;
					align-items: center;
					width: 100%;
					padding: 0 10rpx;
					// padding: 14rpx 0 30rpx;

					.hotPrice-box {
						padding: 2rpx 6rpx;

						background: linear-gradient(90deg, #c79a4c, #f9df7f);
						border-radius: 5rpx;
						text-align: center;
						line-height: 28rpx;
						font-size: 20rpx;
						font-family: Source Han Sans CN;
						font-weight: 400;
						color: #ffffff;
					}

					.price {
						margin-left: 10rpx;
						font-size: 36rpx;
						color: #ff0000;
						font-weight: 500;
						display: flex;
						width: 100%;
						justify-content: flex-start;
						align-items: center;

						.jf {
							font-size: 20rpx;
						}

						.give-jf {
							display: inline-block;
							padding: 8rpx;

							background: linear-gradient(90deg, #FF834D, #FF2600);
							border-radius: 12rpx 0px 12rpx 0px;

							font-size: 22rpx;
							font-weight: 500;
							color: #FFFFFF;
							margin-left: 22rpx;
						}

						.ot-pirce {
							margin-left: 7rpx;
							font-size: 26rpx;
							font-weight: 500;
							text-decoration: line-through;
							color: #999999;
							align-self: flex-end;
						}

					}

					.yuanPrice {
						margin-left: 10rpx;
						font-size: 20rpx;
						font-family: PingFang SC;
						font-weight: 500;
						text-decoration: line-through;
						color: #999999;
					}

					.cart-icon {
						image {
							width: 44rpx;
							height: 44rpx;
						}
					}
				}
			}
		}
	}

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

	.top-bg {
		height: 450rpx;
		//#f53934
		background-image: linear-gradient(to bottom, $base-color, #fff);
		position: absolute;
		top: 0;
		width: 100%;
	}

	.gsjs {
		width: 100%;
	}

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

	.gzh {
		width: 522rpx;
		background-color: #fff;
		border-radius: 20rpx;
		padding: 35rpx 20rpx;

		image {
			display: block;
			margin: auto;
			width: 400rpx;
			height: 400rpx;
		}

		text-align: center;
		font-size: 32rpx;
	}


	.store-wrap {
		padding: 20rpx 0;
		background-color: #fff;

		.bn1 {
			display: block;
			width: 374rpx;
			height: 32rpx;
			margin: 20rpx auto;
		}

		.store {
			padding: 35rpx 33rpx;

			.store-info {
				align-items: flex-start;

				.store-logo {
					width: 145rpx;
					height: 145rpx;
					margin-right: 15rpx;
					background-color: #eee;
					border-radius: 10rpx;
				}

				.store-base {
					height: 145rpx;
					max-width: 354rpx;
					margin-right: auto;
					position: relative;

					.name {
						font-weight: bold;
						font-size: 28rpx;
						color: #000419;
						margin-bottom: 5rpx;
					}

					.address {
						font-size: 22rpx;
						color: #666666;
					}

					.time {
						display: inline-block;
						padding: 7rpx 11rpx 6rpx 8rpx;
						background: #ffede6;
						border-radius: 5rpx;
						color: #FF6B2E;
						font-size: 20rpx;
						position: absolute;
						bottom: 0;
					}
				}

				.store-dis {
					font-size: 22rpx;
					color: #273551;
				}
			}

			.goods-wrap {
				margin-top: 20rpx;

				.good {
					width: 145rpx;
					margin-right: 10rpx;

					.good-img {
						width: 145rpx;
						height: 145rpx;
						color: #000217;
						background-color: #eee;
						border-radius: 5rpx;
					}

					.good-name {
						font-size: 22rpx;
						color: #000217;
					}

					.goood-price {
						font-weight: bold;
						font-size: 26rpx;
						color: #FF591F;
					}
				}
			}
		}
	}
</style>