<template>
	<view class="container">
		<!-- 轮播图 start -->
		<view class="swiper-wrap">
			<swiper class="top-swiper" autoplay="true" duration="400" interval="5000" @change="swiperChange"
				indicator-dots indicator-active-color="#fff" indicator-color="rgba(255,255,255,0.8)">
				<swiper-item v-for="(item, index) in carouselList" :key="index" class="carousel-item"
					@click="bannerNavToUrl(item.url)">
					<image :src="item.pic" />
				</swiper-item>
			</swiper>
		</view>
		<!-- 轮播图 end -->
		<!-- 工具箱 start -->
		<view class="tool-box flex">
			<view class="tool-item" @click="navto('/pages/user/myjslis')">
				<image src="../../static/img/t1.png" mode="widthFix"></image>
			</view>
			<button open-type="contact" class="tool-item" plain="true">
				<image src="../../static/img/t2.png" mode="widthFix"></image>
			</button>

			<view class="tool-item" @click="navto('/pages/index/zngs?type=0')">
				<image src="../../static/img/t4.png" mode="widthFix"></image>
			</view>
			<view class="tool-item" @click="navto('/pages/index/zngs?type=1')">
				<image src="../../static/img/t3.png" mode="widthFix"></image>
			</view>
		</view>
		<!-- 工具箱 end -->
		<!-- tip start -->
		<!-- <view class="tip">
			<image src="../../static/img/tip.png" mode="widthFix"></image>
			<view class="tip-tit">
				<view class="">
					系统使用说明
				</view>
				<view class="tit-tit">
					计算系统使用教程小贴士
				</view>
			</view>
		</view> -->
		<image src="../../static/img/gbc.png" mode="widthFix" class="gcb"></image>
		<view class="" style="height: 47rpx;">

		</view>
		<!-- tip end -->
		<!-- 客服 start -->
		<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>
		<!-- 客服 end -->
	</view>
</template>

<script>
	import {
		saveUrl,
		interceptor
	} from '@/utils/loginUtils.js';
	import {
		loadIndexs
	} from '@/api/index.js';
	import {
		getUserInfo
	} from '@/api/user.js';
	import {
		mapState
	} from 'vuex';

	export default {
		data() {
			return {
				shareShow: false, //分享海报
				pageProportion: 0, //保存页面基于750宽度的比例
				swiperHeight: 0,
				checkid: 0,
				titleNViewBackground: '',
				swiperCurrent: 0,
				swiperLength: 0,
				carouselList: [], //轮播列表
				page: 1,
				limit: 5,
				text: ''
			};
		},
		computed: {
			...mapState(['loginInterceptor']),
			...mapState('user', ['hasLogin', 'userInfo'])
		},
		onLoad: function(option) {
			// #ifndef MP
			if (option.spread) {
				// 存储其他邀请人
				uni.setStorageSync('spread', option.spread);
			}
			// #endif
			// #ifdef MP
			if (option.scene) {
				// 存储小程序邀请人
				uni.setStorage({
					key: 'spread_code',
					data: option.scene
				});
			}
			// #endif
		},
		onShow: function() {
			// 判断是否强制登录
			if (this.loginInterceptor && !this.hasLogin) {
				// 登录拦截
				interceptor();
			}
			this.loadData();
		},
		//下拉刷新
		onPullDownRefresh() {
			this.loadData();
		},
		onShareAppMessage(options) {
			// 设置菜单中的转发按钮触发转发事件时的转发内容
			let pages = getCurrentPages(); //获取加载的页面
			let currentPage = pages[pages.length - 1]; //获取当前页面的对象
			let url = currentPage.route; //当前页面url
			let item = currentPage.options; //如果要获取url中所带的参数可以查看options
			let 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;
		},
		methods: {
			// 打开客服
			openKf() {
				this.$refs.popupkf.open();
			},
			// 关闭客服
			cancel() {
				this.$refs.popupkf.close();
			},
			navto(url) {
				if (!this.hasLogin) {
					// 保存地址
					saveUrl();
					// 登录拦截
					interceptor();
				} else {
					uni.navigateTo({
						url,
						fail() {
							uni.switchTab({
								url
							})
						}
					});
				}
			},
			swiperChange() {

			},
			bannerNavToUrl(url) {
				uni.navigateTo({
					url,
					fail() {
						uni.switchTab({
							url
						})
					}
				})
			},
			// 请求载入数据
			async loadData() {
				loadIndexs({})
					.then(({
						data
					}) => {
						let goods = data.info;
						this.carouselList = data.banner;
						this.swiperLength = this.carouselList.length;

						uni.stopPullDownRefresh();
					})
					.catch(e => {
						uni.stopPullDownRefresh();
					});
			},
			// 复制客服微信
			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
			},
		}
	};
</script>

<style lang="scss">
	// 顶部轮播图
	.swiper-wrap {
		width: 750rpx;

		.top-swiper {
			width: 750rpx;
			height: 375rpx;
			margin: auto;

			image {
				width: 750rpx;
				height: 375rpx;
			}
		}
	}

	// 工具箱
	.tool-box {
		width: 700rpx;
		margin: 20rpx auto 5rpx;
		justify-content: space-between;
		flex-wrap: wrap;
		font-size: 0;

		.tool-item {
			padding: 0;
			margin: 0;
			width: 344rpx;
			height: 180rpx;
			border: none;
			display: inline-block;
			margin-bottom: 15rpx;

			image {
				width: 344rpx;
			}
		}
	}

	.tip {
		width: 702rpx;
		height: 164rpx;
		position: relative;
		margin: auto;

		image {
			width: 702rpx;
		}

		.tip-tit {
			height: 164rpx;
			position: absolute;
			top: 0;
			font-size: 32rpx;
			font-weight: bold;
			color: #333333;
			display: flex;
			justify-content: center;
			flex-direction: column;
			padding-left: 140rpx;

			.tit-tit {
				margin-top: 15rpx;
				font-size: 20rpx;
				font-weight: 500;
				color: #666666;
			}
		}
	}

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

	.gcb {
		display: block;
		margin: 20rpx auto 0;
		width: 700rpx;
		border-radius: 20rpx;
	}
</style>
