<template>
	<view class="container">
		<!-- 顶部logo and 搜索 start-->
		<view class="status_bar"></view>
		<view class="index-bg">
			<image src="../../static/icon/index-bg.png" mode=""></image>
		</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>
		<!-- 顶部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.pic" />
			</swiper-item>
		</swiper>
		<!-- 分类 start -->
		<view class="cate-section flex">
			<view class="cate-item flex" @click="navto('/pages/category/category')">
				<view class="img-wrapper flex">
					<image src="../../static/icon/in1.png" mode=""></image>
				</view>
				<view class="item-title">产品中心</view>
			</view>
			<view class="cate-item flex" @click="navto('/pages/index/artDetail?id=1')">
				<view class="img-wrapper flex">
					<image src="../../static/icon/in2.png" mode=""></image>
				</view>
				<view class="item-title">公司简介</view>
			</view>
			<view class="cate-item flex" @click="openKf()">
				<view class="img-wrapper flex">
					<image src="../../static/icon/in3.png" mode=""></image>
				</view>
				<view class="item-title">联系客服</view>
			</view>
			<view class="cate-item flex" @click="navto('/pages/user/shareQrCode',1)">
				<view class="img-wrapper flex">
					<image src="../../static/icon/in4.png" mode=""></image>
				</view>
				<view class="item-title">邀请好友</view>
			</view>
		</view>
		<view class="tongz" v-if="text1.length !=0">
			<view class="tongz-left flex">
				<view class="left">
					<image class="image-left" src="../../static/img/tong.png" mode=""></image>
					<view class="tongz-title">系统公告</view>
				</view>
				<u-notice-bar style="width: 100%;" mode="vertical" type="none" :volume-icon="false" :more-icon="true"
					:list="text1" @click="goDetails" @getMore="navTo('/pages/index/artlist?type=1')"></u-notice-bar>

			</view>
		</view>
		<!-- 分类 ed -->
		<view class="jg"></view>
		<!-- 商品列表 start -->
		<view class="product-box" v-if="firstList.length > 0">
			<view class="product-tit flex">
				<view class="hx"></view>
				<view class="tit">爆款推荐</view>
				<view class="hx"></view>
			</view>
		</view>
		<view class="good-list" v-if="firstList.length > 0">
			<view class="good flex" v-for="item in firstList" @click="navto('/pages/product/product?id=' + item.id)">
				<view class="good-img">
					<image :src="item.image" mode=""></image>
				</view>
				<view class="good-info flex">
					<view class="good-name clamp2">{{ item.store_name }}</view>
					<view class="good-price">
						<view class="old-price" v-if="item.ot_price * 1 > item.price * 1">
							<text class="old-left">¥{{ item.ot_price }}</text>
							<image src="../../static/icon/down.png" mode="widthFix"></image>
							<text class="old-right">直降{{ item.ot_price * 1 - item.price * 1 }}元</text>
						</view>
						<view class="new-price flex">
							<view class="">¥{{ item.price }}</view>
							<view class="good-tip">立即购买</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 商品列表 ed-->
		<view class="jg" v-if="goodList.length > 0"></view>
		<!-- 会员商品 start -->
		<view class="vip-box" v-if="goodList.length > 0">
			<view class="vip-title flex">
				<view class="vip-left">
					<image class="vip-left-icon" src="../../static/img/fire.png" mode=""></image>
					<view class="vip-left-font">
						礼包专区
					</view>
				</view>
				<view class="vip-right" @click="navTo('/pages/product/classify?type=5')">
					<view class="vip-right-font">
						更多
					</view>
					<image class="vip-right-icon" src="../../static/img/back.png" mode=""></image>
				</view>
			</view>
		</view>
		<view class="product-box" v-if="goodList.length > 0">
			<view class="hotgoods">
				<view class="hotgoods-item" v-for="item in goodList" :key="item.id"
					@click="navto('/pages/product/product?id=' + item.id+'&isvip=1')" style="height: 520rpx;">
					<view class="image-wrapper">
						<image class="image" :src="item.image" mode="scaleToFill"></image>
						<view class="give-jf">
							赠{{item.give_money *1}}消费金
						</view>
					</view>
					<view class="flex"
						style="flex-direction: column;justify-content: space-between;align-items: flex-start;height: 170rpx;">
						<view class="title clamp2">{{item.store_name}}</view>
						<view class="hot-price">
							<view class="price">
								<text class="font-size-sm"></text>
								<text>￥{{ item.price * 1 }}</text>
								<!-- <text class="give-jf" v-if="item.give_integral*1 > 0">赠{{item.give_integral*1}}积分</text> -->
							</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">客服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
	} from '@/api/index.js';
	import {
		getUserInfo,
		spread,
		article
	} from '@/api/user.js';
	import {
		getProducts,
		groomList
	} 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: [],
				article: [],
				text: '', //客服微信
				page: 1,
				limit: 10,
				loadingType: 'more',
				swiperHeight: 0,
				checkid: 0,
				titleNViewBackground: '',
				swiperCurrent: 0,
				swiperLength: 0,
				carouselList: [], //轮播列表
				firstList: [],
				goodList: [], //商品列表
			};
		},
		computed: {
			...mapState('user', ['hasLogin', 'userInfo'])
		},
		onReachBottom() {
			console.log('到底');
			this.getGoodList();
		},
		onLoad: function(option) {
			// #ifdef MP
			if (option.scene) {
				// 存储小程序邀请人
				uni.setStorage({
					key: 'spread_code',
					data: option.scene
				});
			}
			// #endif
			// #ifdef H5
			uni.setStorage({
				key: 'spread',
				data: option.spread
			});
			// #endif
			article({}, 1).then(({
				data
			}) => {
				data.forEach(e => {
					this.text1.push(e.synopsis);
				});
				this.article = data;
			});
		},
		onShow: function() {
			this.getGoodList();
			this.loadData();
		},
		methods: {
			getGoodList() {
				let obj = this;
				if (obj.loadingType == 'loading' || obj.loadingType == 'noMore') {
					return;
				}
				obj.loadingType = 'loading';
				groomList({
					page: obj.page,
					limit: obj.limit,
				}, 6).then(res => {
					obj.goodList = obj.goodList.concat(res.data.list);
					if (obj.limit == res.data.length) {
						obj.page++;
						obj.loadingType = 'more';
					} else {
						obj.loadingType = 'noMore';
					}
				});
			},
			// 打开客服
			openKf() {
				this.$refs.popupkf.open();
			},
			// 关闭客服
			cancel() {
				this.$refs.popupkf.close();
			},

			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);
				}
			},
			goDetails(e) {
				uni.navigateTo({
					url: '/pages/index/artDetail?id=' + this.article[e].id
				});
			},
			// 點擊搜索框
			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.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;
	}

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

		.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: 690rpx;
		height: 360rpx;
		border-radius: 20rpx;
		margin: 20rpx auto 0;

		image {
			width: 690rpx;
			height: 360rpx;
			border-radius: 20rpx;
		}
	}

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

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

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

	.jg {
		margin-top: 20rpx;
		height: 20rpx;
		background: #f8f8f8;
	}

	.vip-box {
		padding: 30rpx 20rpx 0;

		.vip-title {
			line-height: 1;
		}

		.vip-left {
			display: flex;
			align-items: center;

			.vip-left-icon {
				width: 38rpx;
				height: 38rpx;
			}

			.vip-left-font {
				margin-left: 10rpx;
				font-size: 32rpx;
				font-family: PingFang SC;
				font-weight: 800;
				color: #363636;
			}
		}

		.vip-right {
			display: flex;
			align-items: center;

			.vip-right-icon {
				width: 10rpx;
				height: 20rpx;
			}

			.vip-right-font {
				margin-right: 10rpx;
				font-size: 24rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #989898;
			}
		}
	}

	// 分类
	.cate-section {
		justify-content: space-around;
		background-color: #fff;
		padding: 45rpx 0 30rpx;

		.cate-item {
			flex-grow: 0;
			width: 20%;
			flex-direction: column;
			text-align: center;
			align-items: center;
			justify-content: space-around;

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

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

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

	.tongz {
		width: 690rpx;
		margin: 32rpx auto 0;
		padding: 0 30rpx 0 24rpx;
		background: #f8f8fa;
		position: relative;

		.tongz-left {
			width: 640rpx;

			.left {
				flex-shrink: 0;
				display: flex;
				align-items: center;
			}

			.tongz-title {
				margin-left: 10rpx;
				font-size: 26rpx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #FD7811;
			}

			.image-left {
				width: 28rpx;
				height: 28rpx;
			}

			.tongz-font {
				margin-left: 22rpx;
				font-size: 28rpx;
				font-family: Source Han Sans CN;
				font-weight: 400;
				color: #0f253a;
			}
		}

		.tongz-right {
			position: relative;
			z-index: 11;
			width: 12rpx;
			height: 26rpx;

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

	.hotgoods {
		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: 20rpx;

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

				.give-jf {
					position: absolute;
					top: 0;
					left: 0;
					padding: 8rpx;
					background: #FD7811;
					border-radius: 10rpx 0px 10rpx 0px;
					font-size: 22rpx;
					font-weight: 500;
					color: #FFFFFF;
				}


				.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;
				color: #333333;
			}

			.hot-price {
				display: flex;
				justify-content: flex-start;
				align-items: center;
				// 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;
					justify-content: flex-start;
					align-items: center;

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


	.index-bg {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		width: 750rpx;
		height: 460rpx;

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

	.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: #363636;
			}
		}

		.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 {
		width: 750rpx;
		background: #fff;
		padding: 30rpx 0;

		.good {
			width: 690rpx;
			height: 276rpx;
			background: #ffffff;
			box-shadow: 0px 0px 20rpx 0px rgba(50, 50, 52, 0.06);
			border-radius: 10rpx;
			margin: auto;
			padding: 20rpx 15rpx;
			margin-bottom: 20rpx;

			&:last-of-type {
				margin-bottom: 0rpx;
			}

			.good-img {
				flex-shrink: 0;
				width: 236rpx;
				height: 236rpx;
				border-radius: 10rpx;
				margin-right: 22rpx;

				image {
					width: 236rpx;
					height: 236rpx;
					border-radius: 10rpx;
				}
			}

			.good-info {
				flex-grow: 1;
				height: 100%;
				flex-direction: column;
				justify-content: space-between;
				align-items: flex-start;

				.good-name {
					font-size: 32rpx;
					font-weight: bold;
					padding-top: 10rpx;
					color: #333333;
				}

				.good-price {
					width: 100%;

					image {
						width: 14rpx;
						margin: 0 6rpx 0 10rpx;
					}

					.old-price {
						.old-left {
							font-size: 26rpx;
							font-weight: 500;
							text-decoration: line-through;
							color: #999999;
						}

						.old-right {
							font-size: 24rpx;
							font-weight: bold;
							color: #b59467;
						}
					}

					.new-price {
						width: 100%;
						font-size: 36rpx;
						font-weight: bold;
						color: #ff4c4c;
						justify-content: space-between;

						.good-tip {
							width: 137rpx;
							height: 52rpx;
							background: #ff4a48;
							border-radius: 26rpx;
							position: relative;
							font-size: 26rpx;
							font-weight: 500;
							color: #ffffff;
							line-height: 52rpx;
							text-align: center;
						}
					}
				}
			}
		}
	}

	.product-box {
		background: #ffffff;
		padding: 26rpx 0 20rpx;

		.product-tit {
			justify-content: center;

			image {
				width: 609rpx;
				height: 31rpx;
			}

			.hx {
				width: 120rpx;
				height: 1rpx;
				background: #363636;
				color: #363636;
				line-height: 28px;
			}

			.tit {
				margin: 0 70rpx;
				font-size: 32rpx;
				font-weight: 500;
				color: #363636;
			}
		}

		.in-ban {
			margin-top: 40rpx;
			width: 750rpx;
			height: 220rpx;
		}
	}
</style>