<template>
	<view class="container">
		<view class="carousel-section">
			<!-- 背景色区域 -->
			<view class="bg"><image src="../../static/index/index1.png" mode=""></image></view>
			<!-- 标题栏和状态栏占位符 -->
			<view class="titleNview-placing"></view>
		</view>
		<image class="bg3" src="../../static/index/index3.png" mode=""></image>
		<view class="tongz flex">
			<view>
				<view class="tongz-bg"><image src="../../static/img/tongz-bg.png" mode=""></image></view>
				<view class="tongz-left flex">
					<image class="image-left" src="../../static/img/ling.png" mode=""></image>
					<u-notice-bar
						style="width: 100%;"
						mode="vertical"
						type="none"
						:volume-icon="false"
						:more-icon="true"
						:list="text"
						@click="goDetails"
						@getMore="navTo('/pages/index/jieshao')"
					></u-notice-bar>
				</view>
			</view>
		</view>
		<view class="main-box" v-if="integralList.length != 0">
			<view class="main-top">
				<image src="../../static/icon/indexI.png" mode="" class="main-icon"></image>
				<view class="main-title">阳光兑换</view>
				<view class="main-synopsis">超值体验</view>
			</view>
			<view class="main-content">
				<view class="content-top flex" @click="navTo('/pages/index/integral')">
					<view class="content-font">
						<view class="content-title">阳光兑换热门好物</view>
						<view class="content-synopsis">买到就是赚到</view>
					</view>
					<image src="../../static/icon/fanhui.png" class="go" mode=""></image>
				</view>
				<view class="commodity flex">
					<view class="commodity-item" v-for="(item, index) in integralList" :key="item.id" @click="navToDetailPagejf(item)">
						<image class="commodity-image" :src="item.image" mode=""></image>
						<view class="commodity-title clamp">{{ item.store_name }}</view>
						<view class="price clamp">{{ item.result.value[0].integral * 1 }}阳光+</view>
						<view class="vip-price">
							<text>￥{{ item.result.value[0].price }}</text>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="product-box" v-if="bastList.length != 0">
			<view class="product-title">
				<image class="pt-image" src="../../static/index/index15.png" mode=""></image>
				<view class="pt-title">热销商品</view>
				<view class="pt-tip">好货不用挑</view>
			</view>
			<view class="hotgoods">
				<view class="hotgoods-item" v-for="item in bastList" :key="item.id" @click="navToDetailPage(item)">
					<view class="image-wrapper">
						<image class="image-bg" src="../../static/img/libao-bg.png" mode=""></image>
						<image class="image" :src="item.image" mode="scaleToFill"></image>
					</view>
					<view class="title clamp2">{{ item.store_name }}</view>
					<view class="hot-price">
						<!-- <view class="hotPrice-box">会员价</view> -->
						<view class="price">
							<text class="font-size-sm">￥</text>
							{{ item.price * 1 }}
						</view>
						<view class="yuanPrice">{{ item.ot_price }}</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 客服 -->
		<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="kfClose()">取消</view>
						<view class="comfirm" @click="comfirm(text)">复制微信</view>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
import { lookSubpoints, lookOneself } from '../../api/user.js';
import seckill from '../../components/seckill/seckill.vue';
import { loadIndexs } from '@/api/index.js';
import { article } from '@/api/user.js';
import { getUserInfo, spread } from '@/api/user.js';
import { setCoupons } from '@/api/functionalUnit.js';
import { getProducts } from '@/api/product.js';
import { interceptor } from '@/utils/loginUtils';
import { mapState, mapMutations } from 'vuex';
// #ifdef H5
import { weixindata, shareLoad } from '@/utils/wxAuthorized';
// #endif
import uniCopy from '@/js_sdk/xb-copy/uni-copy.js';
export default {
	components: {
		seckill
	},
	data() {
		return {
			text: '',
			pageProportion: 0, //保存页面基于750宽度的比例
			swiperHeight: 0,
			checkid: 0,
			titleNViewBackground: '',
			longitude: 0, //经度
			latitude: 0, //纬度
			swiperCurrent: 0,
			swiperLength: 0,
			carouselList: [], //轮播列表
			bastList: [], //会员礼包
			integralList: [], //兑换专区
			page: 1,
			limit: 10,
			loadType: 'more',
			text: [],
			article: []
		};
	},
	computed: {
		...mapState(['loginInterceptor', 'baseURL']),
		...mapState('user', ['hasLogin', 'userInfo'])
	},
	onLoad: function(option) {
		// #ifdef MP
		if (option.scene) {
			// 存储小程序邀请人
			uni.setStorage({
				key: 'spread_code',
				data: option.scene
			});
		}
		// #endif
	},
	onShow: function() {
		// 判断是否强制登录
		// if (!this.hasLogin) {
		// 	// 登录拦截
		// 	interceptor();
		// }
		this.loadData();
		this.getproducts();
	},
	onReachBottom() {
		this.getproducts();
	},
	methods: {
		...mapMutations(['setLat', 'setLon']),
		kfClose() {
			this.$refs.popupkf.close();
		},
		goDetails(e) {
			uni.navigateTo({
				url: '/pages/index/detail?id=' + this.article[e].id
			});
		},
		// 复制
		comfirm(text) {
			console.log(text);
			const result = uniCopy(text);
			if (result === false) {
				uni.showToast({
					title: '不支持'
				});
			} else {
				uni.showToast({
					title: '复制成功',
					icon: 'none'
				});
			}
			this.$refs.popupkp.close();
		},
		navTo(url) {
			if (url == '') {
				this.$api.msg('暂未开通，敬请期待');
			} else {
				uni.navigateTo({
					url,
					fail() {
						uni.switchTab({
							url
						});
					}
				});
			}
		},
		// 监听切换事件
		listChange(e) {
			this.checkid = e.detail.current;
		},
		//积分详情页
		navToDetailPagejf(item) {
			uni.navigateTo({
				url: '/pages/product/product?id=' + item.id + '&isJF=1'
			});
		},
		// 点击触发领取优惠券
		setCoupons(item) {
			// 判断是否已经领取了优惠券
			let obj = this;
			uni.showModal({
				title: '领取提示',
				content: '是否领取优惠券',
				success(e) {
					if (e.confirm) {
						setCoupons({
							couponId: item.id
						}).then(e => {
							item.is_use = true;
							uni.showToast({
								title: '领取成功',
								type: 'top',
								duration: 2000
							});
						});
					}
				}
			});
		},
		//商品种类切换
		change(item) {
			let id = item;
			this.checkid = id;
			if (this.checkid == 1) {
				// console.log(1);
				this.detail = this.selected_detail;
			} else if (this.checkid == 2) {
				// console.log(2);
				this.detail = this.new_product;
			} else {
				this.detail = this.cheap_good;
			}
		},
		// 监听图片加载完成
		onImageError(key, index) {
			this[key][index].image = '/static/error/errorImage.jpg';
		},
		// 请求载入数据
		async loadData() {
			this.text = [];
			this.article = [];
			loadIndexs({})
				.then(({ data }) => {
					let goods = data.info;
					this.carouselList = data.banner;
					console.log(this.carouselList, 'this.carouselList++++++++');
					this.swiperLength = this.carouselList.length;
					this.integralList = data.likeInfo; //兑换专区
					console.log(data.likeInfo, '123456');
				})
				.catch(e => {});
			article({}, 1).then(({ data }) => {
				data.forEach(e => {
					this.text.push(e.synopsis);
				});
				this.article = data;
			});
		},
		//轮播图切换修改背景色
		swiperChange(e) {
			const index = e.detail.current;
			this.swiperCurrent = index;
			this.titleNViewBackground = this.carouselList[index].background;
		},
		//详情页
		navToDetailPage(item) {
			let id = item.id;
			uni.navigateTo({
				url: '/pages/product/product?id=' + id + '&isVip=1'
			});
		},
		// 轮播图跳转
		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
			});
		},
		getproducts() {
			const obj = this;
			if (obj.loadType == 'loading' || obj.loadType == 'nomore') {
				return;
			}
			obj.loadType = 'loading';
			getProducts({ page: obj.page, limit: obj.limit }).then(({ data }) => {
				obj.bastList = obj.bastList.concat(data);
				if (data.length == obj.limit) {
					obj.page++;
					obj.loadType = 'more';
				} else {
					obj.loadType = 'nomore';
				}
			});
		}
	}
};
</script>

<style lang="scss">
.carousel-section {
	position: relative;
	width: 750rpx;
	height: 1450rpx;

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

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

	.titleNview-placing {
		height: var(--status-bar-height);
		box-sizing: content-box;
	}
}
.bg2 {
	margin-top: -60rpx;
	width: 750rpx;
	height: 250rpx;
}
.bg3 {
	margin-top: -20rpx;
	width: 750rpx;
	height: 750rpx;
}
.product-box {
	margin-top: 20rpx;
	background: #ffffff;
	padding: 26rpx 30rpx 20rpx;

	.product-title {
		display: flex;
		align-items: center;

		.pt-image {
			width: 26rpx;
			height: 34rpx;
		}

		.pt-title {
			margin-left: 8rpx;
			font-size: 36rpx;
			font-family: SourceHanSansCN;
			font-weight: bold;
			color: #666666;
		}

		.pt-tip {
			margin-left: 16rpx;
			font-size: 24rpx;
			font-family: SourceHanSansCN;
			font-weight: bold;
			color: #aaaaaa;
		}
	}

	.hotgoods {
		margin-top: 38rpx;
		width: 100%;
		display: flex;
		flex-wrap: wrap;
		padding: 0 0 30rpx;

		.hotgoods-item {
			width: 48%;
			background-color: #ffffff;
			border-radius: 12rpx;

			&:nth-child(2n + 1) {
				margin-right: 24rpx;
			}

			.image-wrapper {
				width: 100%;
				height: 330rpx;
				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;
				font-size: 28rpx;
				font-family: PingFang SC;
				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: 40rpx;
					color: #ff0000;
					font-weight: 500;
				}

				.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;
					}
				}
			}
		}
	}
}
.main-box {
	background-color: #ffffff;
	margin-top: 20rpx;
	padding: 22rpx 30rpx;
	.main-top {
		display: flex;
		justify-content: flex-start;
		align-items: center;
		.main-icon {
			width: 36rpx;
			height: 36rpx;
		}
		.main-title {
			margin-left: 8rpx;
			font-size: 32rpx;
			font-family: Source Han Sans CN;
			font-weight: 500;
			color: #333333;
		}
		.main-synopsis {
			margin-left: 16rpx;
			font-size: 20rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #95a0b1;
		}
	}
	.main-content {
		width: 690rpx;
		background: #f4f4f4;
		border-radius: 12rpx;
		margin: 20rpx auto 0;
		padding-bottom: 10rpx;
		.xfq {
			background: linear-gradient(43deg, #ffc063, #ffa163) !important;
		}
		.content-top {
			width: 690rpx;
			background: linear-gradient(43deg, #73deb2, #52c696);
			border-radius: 12rpx 12rpx 0px 0px;
			padding: 24rpx 22rpx 68rpx 27rpx;
			.content-font {
				.content-title {
					font-size: 32rpx;
					font-family: Source Han Sans CN;
					font-weight: 500;
					color: #ffffff;
				}
				.content-synopsis {
					margin-top: 10rpx;
					font-size: 24rpx;
					font-family: Source Han Sans CN;
					font-weight: 400;
					color: #ffffff;
				}
			}
			.go {
				width: 30rpx;
				height: 35rpx;
				transform: rotate(180deg);
				-ms-transform: rotate(180deg); /* Internet Explorer */
				-moz-transform: rotate(180deg); /* Firefox */
				-webkit-transform: rotate(180deg); /* Safari 和 Chrome */
				-o-transform: rotate(180deg); /* Opera */
			}
		}
	}
	.commodity {
		width: 670rpx;
		background: #ffffff;
		border-radius: 8rpx;
		padding: 2rpx;
		margin: -50rpx auto 0;
		flex-wrap: wrap;
		justify-content: flex-start;
		.commodity-item {
			width: 33%;
			padding: 14rpx 30rpx 22rpx;
			.commodity-image {
				width: 160rpx;
				height: 160rpx;
			}
			.commodity-title {
				margin-top: 14rpx;
				width: 162rpx;
				font-size: 24rpx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #3f3f3f;
			}
			.price {
				font-size: 24rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #999999;
			}
			.vip-price {
				font-size: 30rpx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #ff6f0f;
				image {
					position: relative;
					z-index: 10;
					width: 26rpx;
					height: 26rpx;
				}
				text {
					display: inline-block;
					margin-left: 6rpx;
				}
			}
		}
	}
}
.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;

			.title {
			}

			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;
			}
		}
	}
}
.tongz {
	width: 690rpx;
	height: 70rpx;
	margin: 32rpx auto 0;
	padding: 18rpx 30rpx 18rpx 24rpx;
	align-items: center;
	position: relative;
	.tongz-bg {
		position: absolute;
		top: 0;
		right: 0;
		left: 0;
		width: 690rpx;
		height: 70rpx;
		image {
			width: 100%;
			height: 100%;
		}
	}
	.tongz-left {
		width: 640rpx;
		.image-left {
			width: 28rpx;
			height: 34rpx;
		}
		.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%;
		}
	}
}
</style>
