<template>
	<view class="container">
		<!-- 顶部logo and 搜索 start-->
		<!-- <view class="status_bar"></view> -->
		<!-- <view class="top-search flex">
			<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> -->
		<!-- 轮播图 start -->
		<swiper class="top-swiper" autoplay="true" duration="400" interval="5000" @change="swiperChange" indicator-dots
			indicator-active-color="#ea3855">
			<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="jg">
		</view>
		<view class="good-list">
			<view class="good" v-for="item in goodList"
				@click="navTo('/pages/product/product?id=' + item.id+'&isjf=1')">
				<image :src="item.image" mode=""></image>
			</view>
		</view>
		<view class="jg">

		</view>
		<view class="art-box flex" v-if="shopList.length !=0">
			<view class="art-item" v-for="item in shopList"
				@click="navTo('/pages/product/product?id=' + item.id+'&isjf=1')">
				<image :src="item.image" mode=""></image>
			</view>
		</view>
		<view class="jg" v-if="shopList.length !=0">
		</view>
		<view class="good-list">
			<view class="good">
				<image @click="navTo('/pages/index/artDetail?id=1')" src="https://xyl.liuniukj.com/computent.png">
				</image>
			</view>
			<view class="good">
				<image @click="navTo('/pages/index/artDetail?id=3')" src="https://xyl.liuniukj.com/product.png">
				</image>
			</view>

		</view>
	</view>
</template>

<script>
	import {
		loadIndexs,
		groom1
	} from '@/api/index.js';
	import {
		getUserInfo,
		spread,
		// getArtList
	} 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: '', //客服微信
				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 + '邀请您加入芯悦澜', // 默认是小程序的名称(可以写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() {
		// 	console.log('到底')
		// 	this.getGoodList()
		// },
		onLoad: function(option) {
			// #ifdef MP
			if (option.scene) {
				// 存储小程序邀请人
				uni.setStorage({
					key: 'spread_code',
					data: option.scene
				});
				uni.setStorage({
					key: 'spread',
					data: option.scene
				});
			}
			// #endif
			if (option.spread) {
				uni.setStorage({
					key: 'spread',
					data: option.spread
				});
			}
		},
		onShow: function() {
			this.getGoodList()
			this.loadData();
			this.groom1()
		},
		methods: {
			//获取复购商品
			groom1() {
				getProducts({
					page: 1,
					limit: 1000,
					type: 2
				}).then(res => {
					console.log(res, '123456')
					this.shopList = res.data
				})
			},
			getGoodList() {
				let obj = this
				// if (obj.loadingType == 'loading' || obj.loadingType == 'noMore') {
				// 	return
				// }
				// obj.loadingType = 'loading'
				getProducts({
					page: 1,
					limit: 1000,
					type: 1
				}).then(res => {
					console.log(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.giftInfo; //会员礼包
						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 {
		background: #ffff;
		min-height: 100%;
		height: auto;
	}

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

	// 顶部搜索
	.top-search {
		height: 80rpx;
		padding: 0 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: 750rpx;
		height: 348rpx;

		// margin: 20rpx 0 0;
		image {
			width: 750rpx;
			height: 348rpx;
		}
	}


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

	.art-box {
		width: 690rpx;
		margin: 0 auto 20rpx;
		flex-wrap: wrap;

		.art-item {
			margin-top: 20rpx;
			width: 330rpx;
			height: 370rpx;
			border-radius: 20rpx;

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

	.good-list {
		width: 750rpx;

		.good {
			margin: 20rpx auto;
			width: 690rpx;
			height: 370rpx;

			image {
				width: 100%;
				height: 100%;
				border-radius: 20rpx;
			}
		}
	}
</style>