<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-->
		<!-- 轮播图 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>
		<image src="../../static/index/in-banner.png" mode="widthFix" class="in-banner"></image>
		<!-- 轮播图 end -->
		<!-- 分类 start -->
		<view class="cate-section flex">
			<view class="cate-item flex" v-for="(citem,cindex) in cateList" @click="gogogo(citem)">
				<view class="img-wrapper flex">
					<image :src="citem.img + (cindex + 1) + '.png'" mode=""></image>
				</view>
				<view class="item-title">{{citem.tit}}</view>
			</view>
		</view>
		<!-- 分类 ed -->
		<image src="../../static/index/in-tit.png" mode="" class="in-tit"></image>
		<image src="../../static/index/in-banner-t.png" mode="" class="in-banner-t"></image>
		<view class="in-vip" v-for="item in levelList">
			<image :src="item.image" mode=""></image>
			<view class="vip-info flex">
				<view class="">
					<view class="price">
						￥{{item.money}} <text>/{{item.valid_date}}天</text>
					</view>
					<view class="info">
						{{item.name}}
					</view>
				</view>
				<view class="btn" @click="gobuy(item)">
					立即开通
				</view>
			</view>
		</view>
		<image src="../../static/index/in-tit-t.png" mode="" class="in-tit"></image>
		<view class="art">
			<view class="art-tit flex">
				<view class="">
					M A S S A G E
				</view>
				<view class="">
					+++
				</view>
			</view>
			<view class="art-content" v-html="art">

			</view>
			<view class="art-line"></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="cancel">取消</view>
						<view class="comfirm" @click="comfirm(text)">复制微信</view>
					</view>
				</view>
			</view>
		</uni-popup>
		<uni-popup ref="popup" type="bottom">
			<view class="cz-model">
				<image :src="choose.image" mode=""></image>
				<view class="">
					{{choose.name}}
				</view>
				<view class="price">
					￥{{choose.money}} 
				</view>
				<view class="btn" @click="goNew">
					立即开通
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		loadIndexs,
		groom1,
		getLevelList
	} from '@/api/index.js';
	import {
		getUserInfo,
		spread,
		levelBuy,
		details
	} from '@/api/user.js';
	import {
		getBargainList,
		getProducts,
		goodsDetail,
		poster,
		getBd,
		getGp
	} 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 {
				buying: false,
				choose: {},
				levelList: [],
				art: '     希尔伯特作为科学疗人的方法，是指运用高端先进的仪器作用于病患的体表、受伤的部位、不适的所在.特定的脸穴、疼痛的地方，具体运用推、拿、按、摩、揉、捏、点、拍等形式多样的手法，以期达到疏通经络、推行气血、扶伤止痛、祛邪扶正、调和阴阳的 疗 效。', //文章介绍
				bastList: [], //爆款推荐
				muted: false,
				cateList: [{
						path: '/pages/index/artDetail?id=4',
						img: '../../static/index/in',
						tit: '仪器介绍'
					},
					{
						path: 'gift',
						img: '../../static/index/in',
						tit: '会员商品'
					},
					// {
					// 	path: 'kf',
					// 	img: '../../static/index/in',
					// 	tit: '联系客服'
					// },
					{
						path: '/pages/user/shareQrCode',
						img: '../../static/index/in',
						tit: '邀请好友'
					}
				],
				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: '',
				jfList: [],
			};
		},
		computed: {
			...mapState('user', ['hasLogin', 'userInfo'])
		},
		onReachBottom() {
			console.log('到底')
		},
		onLoad: function(option) {
			// #ifdef MP
			if (option.scene) {
				// 存储小程序邀请人
				uni.setStorage({
					key: 'spread_code',
					data: option.scene
				});
			}
			// #endif
			// #ifdef H5
			if (option.spread) {
				// 存储小程序邀请人
				uni.setStorage({
					key: 'spread',
					data: option.spread
				});
			}
			// #endif
		},
		onShow: function() {
			this.loadData();
			if(this.hasLogin) {
				this.getList()
			}
			this.details()
		},
		onHide() {},
		methods: {
			details() {
				details({},1).then(res => {
					console.log(res)
					this.art = res.data.content
				})
			},
			gobuy(e) {
				this.choose = e
				this.$refs.popup.open()
			},
			goNew() { 
				if(this.buying) {
					return
				}
				if(this.userInfo.now_money*1 < this.choose.money*1) {
					return this.$api.msg('您当前余额不足')
				}
				this.buying = true
				levelBuy({
					level_id: this.choose.id
				}).then(res => {
					this.$refs.popup.close()
					this.buying = false
					uni.showToast({
						title: '开通成功',
						icon: 'success'
					});
				}).catch(err => {
					this.buying = false
				})
			},
			getList() {
				getLevelList().then(res => {
					console.log(res)
					this.levelList = res.data.list
				})
			},
			getBd() {
				getBd().then(res => {
					console.log(res)
					this.navList[0].list = res.data.list
					this.navList[0].loaded = true
				})
			},
			getGp(id) {
				let nav = this.navList[id]
				getGp(nav.qdata).then(res => {
					console.log(res)
					nav.list = res.data.list
					nav.loaded = true
				})
			},
			gogogo(item) {
				if (!item.path) {
					uni.navigateTo({
						url: '/pages/index/dkf'
					})
				}
				if (item.path == 'kf') {
					this.$refs.popupkf.open()
				} else {
					if(item.path == 'gift') {
						if(this.hasLogin ) {
							if(this.userInfo.vip) {
								uni.navigateTo({
									url:'/pages/index/gift'
								})
							}else {
								uni.showModal({
									title:'提示',
									content: '您当前不是会员，请先升级会员'
								})
							}
						}else {
							// 保存地址
							saveUrl();
							// 登录拦截
							interceptor();
						}
					}else {
						uni.navigateTo({
							url: item.path + '&tit=' + item.tit,
							fail() {
								uni.navigateTo({
									url: item.path,
									fail() {
										uni.switchTab({
											url: item.path,
										})
									}
								})
							}
						})
					}
					
				}
			},
			getGoodList() {
				let obj = this
				getProducts({
					is_order: 0,
					tree_id: 0
				}).then(res => {
					this.jfList = res.data
				})
			},
			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.likeInfo; //会员礼包
						data.info.firstList.forEach(e => {
							e.isVip = e.store_type ? "3" : "0"
						})
						this.firstList = data.info.firstList //首页商品
						this.jfList = data.benefit
						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
				});
			},
			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">
	.container {
		background-image: url('../../static/index/in-bg.png');
		background-size: 100% 100%;
		padding-bottom: 68rpx;
	}

	image {
		vertical-align: middle;
	}

	page {
		min-height: 100%;
		height: auto;
	}

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

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

			image {
				width: 48rpx;
			}
		}

		.search-box {
			justify-content: center;
			width: 698rpx;
			height: 60rpx;
			background: rgba(255, 255, 255, 0.5);
			color: #CBCBCB;
			border-radius: 30rpx;

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

			.search-font {
				margin-left: 14rpx;
				font-size: 28rpx;
				font-weight: 500;
			}
		}
	}

	// 顶部轮播图
	.top-swiper {
		width: 750rpx;
		height: 360rpx;

		.carousel-item {
			image {
				width: 100%;
				height: 100%;
			}
		}
	}

	.in-banner {
		width: 750rpx;
		height: 57rpx;
	}

	.in-banner-t {
		width: 750rpx;
		height: 220rpx;
	}

	.in-vip {
		width: 680rpx;
		height: 504rpx;
		background-image: url('../../static/index/in-vip.png');
		background-size: 100% 100%;
		margin: 30rpx auto 70rpx;
		position: relative;

		image {
			width: 100%;
			height: 356rpx;
			position: absolute;
			top: 0;
			left: 0;
		}

		.vip-info {
			height: 147rpx;
			width: 100%;
			position: absolute;
			bottom: 0;
			left: 0;
			padding: 0 27rpx;

			.price {
				font-weight: bold;
				font-size: 35rpx;
				color: #FF4C4C;

				text {
					font-size: 25rpx;
					color: #333333;

				}
			}

			.info {
				font-weight: 500;
				font-size: 22rpx;
				color: #443516;
				padding-top: 10rpx;
			}

			.btn {
				width: 127rpx;
				line-height: 44rpx;
				background: linear-gradient(89deg, #BB9159, #E6C79D);
				border-radius: 22rpx;
				font-weight: 500;
				font-size: 22rpx;
				color: #443516;
				text-align: center;
			}
		}
	}

	.swiper-btm {
		height: 60rpx;
		width: 750rpx;
		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;
			}
		}
	}

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

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

			.img-wrapper {
				width: 90rpx;
				height: 90rpx;
				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;
				color: #443516;
			}
		}
	}

	.in-tit {
		width: 449rpx;
		height: 79rpx;
		display: block;
		margin: 40rpx auto;
	}

	.art {
		width: 680rpx;
		margin: auto;
		background: #FFFFFF;
		box-shadow: 0rpx 3rpx 3rpx 0rpx rgba(0, 0, 0, 0.1);
		border-radius: 30rpx;
		padding: 39rpx;

		.art-tit {
			font-weight: 400;
			font-size: 16rpx;
			color: #848484;
		}

		.art-content {
			// font-weight: 500;
			// font-size: 24rpx;
			// color: #000000;
			padding: 43rpx 10rpx 31rpx;
			// text-indent: 2em; 
			// text-align: justify;
		}

		.art-line {
			width: 446rpx;
			height: 1rpx;
			background: #434343;
			float: right;
		}
	}

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

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

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

	/deep/ img {
		width: 100% !important;
		height: auto;
	}
	.cz-model {
		width: 750rpx;
		background-color: #fff;
		padding: 60rpx 0 30rpx;
		text-align: center;
		border-radius: 25rpx 25rpx 0 0;
		padding-bottom:70px;
		/* #ifdef APP-PLUS */
		padding-bottom:40px;
		/* #endif */
		view {
			padding: 10rpx 0;
		}
		image {
			width: 680rpx;
			// height: ;
			height: 356rpx;
		}
		.price {
			color: $base-color;
			font-size: 48rpx;
			font-weight: bold;
		}
	
		.tip {
			color: #409eff;
		}
	
		.tips {
			padding-bottom: 20rpx;
		}
	
		.btn {
			width: 690rpx;
			text-align: center;
			line-height: 60rpx;
			color: #443516;
			background: linear-gradient(89deg, #BB9159, #E6C79D);
			border-radius: 40rpx;
			margin: auto;
		}
	}
</style>