<template>
	<view class="content">
		<view class="navbar">
			<view class="logo">
				<image src="/static/images/logo.png" mode="heightFix"></image>
			</view>
		</view>
		<view class="header">
			<!-- <swiper class="swiper" circular :indicator-dots="indicatorDots" :autoplay="autoplay" :interval="interval"
				:duration="duration">
				<swiper-item v-for="(item,index) in swiperList" :key="index" class="swiper-item" @click="toArticle(item.url)">
					<view class="swiper-title">{{item.swiperName}}</view>
					<image class="img-swiper" :src="item.swiperPicture"></image>
				</swiper-item>
			</swiper> -->
			<u-swiper height="360rpx" showTitle :autoplay="autoplay" :interval="interval" :list="swiperList"
				keyName="swiperPicture" :duration="duration" @click="toArticle">
				<!-- <swiper-item v-for="(item,index) in swiperList" :key="index" class="swiper-item" @click="toArticle(item.url)">
					<view class="swiper-title">{{item.swiperName}}</view>
					<image class="img-swiper" :src="item.swiperPicture"></image>
				</swiper-item> -->
			</u-swiper>
		</view>
		<view class="menu">
			<view class="one-menu" v-for="(item,index) in menuList" :key="index" @click="toPage(index)">
				<img :src="item.img">
				<view class="text">{{item.name}}</view>
			</view>
		</view>
		<view class="article-con">
			<view class="article-title">
				<view class="title-left">
					<view class="icon"></view>
					<view class="name">精品文章</view>
				</view>
				<view class="title-more" @click="more">
					更多<u-icon name="arrow-right" size="14" color="#101010"></u-icon>
				</view>
			</view>
			<view class="article-list" v-if="articleList.length > 0">
				<one-article :article="item" v-for="item,index in articleList" :key="index"></one-article>
			</view>
			<u-empty mode="data" icon="http://cdn.uviewui.com/uview/empty/data.png" v-else></u-empty>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				indicatorDots: true,
				autoplay: true,
				interval: 3000,
				duration: 500,
				swiperList: [],
				article: [],
				menuList: [],
				articleList: []
			};
		},
		onLoad(query) {
			// #ifdef MP-WEIXIN
			//检查是否存在新版本
			uni.getUpdateManager().onCheckForUpdate(function(res) {
				// 请求完新版本信息的回调
				console.log("是否有新版本：" + res.hasUpdate);
				if (res.hasUpdate) { //如果有新版本

					// 小程序有新版本，会主动触发下载操作（无需开发者触发）
					uni.getUpdateManager().onUpdateReady(function() { //当新版本下载完成，会进行回调
						uni.showModal({
							title: '更新提示',
							content: '新版本已经准备好，单击确定重启应用',
							showCancel: false,
							success: function(res) {
								if (res.confirm) {
									// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
									uni.getUpdateManager().applyUpdate();
								}
							}
						})

					})

					// 小程序有新版本，会主动触发下载操作（无需开发者触发）
					uni.getUpdateManager().onUpdateFailed(function() { //当新版本下载失败，会进行回调
						uni.showModal({
							title: '提示',
							content: '检查到有新版本，但下载失败，请检查网络设置',
							showCancel: false,
						})
					})
				}
			});
			// #endif
			if (query) {
				console.log(query, 'query')
				try {
					const url = decodeURIComponent(query.q) // 获取到二维码原始链接内容
					let info = url.split("?deptId=")[1]
					let infoRes = info.split("&pwd=");
					uni.setStorageSync('CodeDeptId', infoRes[0]);
					uni.setStorageSync('CodeDeptName', infoRes[1]);
				} catch (e) {

				}
			}
			let _this = this
			uni.getSetting({
				success(res) {
					if (res.authSetting['scope.userLocation'] == false) {
						// uni.clearStorageSync();
						// _this.getStorageSize()

						_this.showRefuseLocationPermission()
					} else {
						// 用户同意获取位置信息
						_this.getLatLon()
					}
				},
				fail(err) {
					console.log('jujuel', err);
				},
				complete(ress) {

				},
				withSubscriptions(resx) {
					console.log('4', resx);
				}
	  })
			
		},
		onShow() {
			this.getSwiperList();
			this.getArticleList();
			this.getTableList();
		},
		onShareAppMessage: function() { // 分享到微信
					// 更多参数配置，参考文档
					return {
						title: '孑医测评',
						path: '/pages/tabbar/home/Home'
					}
		},
		
		onShareTimeline() { // 分享到朋友圈
			return {
				title: '孑医测评',
				path: '/pages/tabbar/home/Home'
			}
		},
		onShare() {
			wx.showShareMenu({
				withShareTicket: true,
				success: function() {
					console.log('成功开启分享菜单')
				},
				fail: function(res) {
					console.log('开启分享菜单失败', res)
				}
			})

			wx.onShareAppMessage(function() {
				return {
					title: '孑医测评',
					path: '/pages/tabbar/home/Home',
					success: function(res) {
						console.log('分享成功', res)
					},
					fail: function(res) {
						console.log('分享失败', res)
					}
				}
			})
		},
		methods: {
			// 用户拒绝授权的展示
			showRefuseLocationPermission() {
				const that = this;
				wx.showModal({
					title: "提示",
					content: "需要获取用户位置权限",
					confirmText: "前往设置",
					showCancel: false,
					success(res) {
						if (res.confirm) {
							uni.openSetting({
								success(res) {
									console.log("打开设置成功", res);
									if (res.authSetting['scope.userLocation']) {
										console.log('成功授权userLocation')
										that.getLatLon()
									} else {
										console.log('用户未授权userLocation')
										// 递归调用本函数，(函数套函数)
										that.showRefuseLocationPermission()
									}
								},
								fail(err) {
									console.log("打开设置失败", err)
								}
							})
						}
					}
				})
			},
			toArticle(index) {
				uni.navigateTo({
					url: "/homePackage/pages/newsDetail?id=" + this.swiperList[index].url
				})
			},
// 获取定位信息
			getLatLon() {
				let _this = this;
				uni.getLocation({
					// 谷歌使用wgs84  其他使用gcj02
					type: 'gcj02', // 使用国测局坐标系
					geocode: true,
					success: function(res) {
						uni.setStorageSync("lon", res.longitude)
						uni.setStorageSync("lat", res.latitude)
					},
					fail: function(err) {
						// uni.showToast({
						// 	title: "获取位置信息失败",
						// 	icon: "none"
						// })
						console.log('获取位置信息失败: ' + err.errMsg);
						uni.setStorageSync("isGetAddress", false)
					}
				});
			},
			// 获取轮播图
			async getSwiperList() {
				const res = await this.$myRequest({
					url: '/system/swiper/list',
				})
				let arr = [];
				for (let i = 0; i < res.data.rows.length; i++) {
					let obj = {
						...res.data.rows[i]
					};
					obj.swiperPicture = this.baseUrl + res.data.rows[i].swiperPicture;
					obj.title = obj.swiperName;
					arr.push(obj);
				}
				this.swiperList = [...arr]
				console.log(116, this.swiperList);
			},
			// 获取文章列表
			async getArticleList() {
				const res = await this.$myRequest({
					url: '/system/article/articleList',
					data: {
						pageNum: 1,
						pageSize: 5,
						parentId: ''
					}
				})
				if (res.data.code == 200) {
					console.log(138, res.data)
					for (let i = 0; i < res.data.rows.length; i++) {
						res.data.rows[i].litpic = this.baseUrl + res.data.rows[i].litpic
					}
					this.articleList = res.data.rows
				}

			},
			// 获取量表
			async getTableList() {
				const table1 = await this.$myRequest({
					url: '/system/table/table1',
				})
				const table2 = await this.$myRequest({
					url: '/system/table/table2',
				})
				const table3 = await this.$myRequest({
					url: '/system/table/table3',
				})
				this.menuList = [{
						img: this.baseUrl + table1.data.data.tableInfo.tableIcon,
						name: table1.data.data.tableInfo.tableName
					},
					{
						img: this.baseUrl + table2.data.data.tableInfo.tableIcon,
						name: table2.data.data.tableInfo.tableName
					},
					{
						img: this.baseUrl + table3.data.data.tableInfo.tableIcon,
						name: table3.data.data.tableInfo.tableName
					}
				]
				// 写入缓存
				try {
					uni.setStorageSync('table1', JSON.stringify(table1.data.data));
					uni.setStorageSync('table2', JSON.stringify(table2.data.data));
					uni.setStorageSync('table3', JSON.stringify(table3.data.data));
				} catch (e) {
					//TODO handle the exception
				}
			},

			toPage(index) {

				if (this.$store.getters.token) {
					uni.navigateTo({
						url: '/tablePackage/pages/table?alias=table' + (index + 1)
					})
				} else {
					uni.navigateTo({
						url: '/loginPackage/pages/Login'
					})
				}

			},
			more() {
				uni.switchTab({
					url: '/pages/tabbar/essay/Essay'
				})
			},

		}

	};
</script>

<style lang="less">
	page {
		background-color: #F9F9FB;
	}

	.content {
		.navbar {
			width: 100%;
			height: 180rpx;
			display: flex;
			align-items: flex-end;

			.logo {
				height: 60%;
				margin-left: 20rpx;

				image {
					height: 100%;
				}
			}
		}


		.header {
			width: 95%;
			margin: 0 auto;
			margin-top: 20rpx;
			height: 360rpx;

			.swiper {
				width: 100%;
				height: 100%;
				border-radius: 12rpx;

				.swiper-item {
					width: 100%;
					height: 100%;
					border-radius: 12rpx;

					.img-swiper {
						width: 100%;
						height: 100%;
						border-radius: 12rpx;
					}

					.swiper-title {
						text-align: center;
						position: absolute;
						color: #fff;
						width: 280px;
						left: calc(50% - 140px);
						font-size: 20px;
						font-weight: bold;
						top: calc(50% - 30px);
					}
				}
			}

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

		.menu {
			width: 95%;
			margin: 0 auto;
			margin-top: 30rpx;
			display: flex;
			justify-content: space-between;

			.one-menu {
				width: 210rpx;
				height: 210rpx;
				line-height: 60rpx;
				border-radius: 30rpx;
				background-color: rgba(255, 255, 255, 1);
				text-align: center;
				box-shadow: 0px 2px 6px 0px rgba(230, 230, 230, 100);

				img {
					width: 96rpx;
					height: 96rpx;
					margin-top: 20rpx;
				}

				.text {
					font-size: 28rpx;
				}
			}
		}

		.article-con {
			width: 95%;
			margin: 0 auto;
			margin-top: 40rpx;

			.article-title {
				display: flex;
				justify-content: space-between;
				align-items: flex-end;
				margin-bottom: 20rpx;

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

					.icon {
						width: 6px;
						height: 38rpx;
						line-height: 22px;
						border-radius: 50px;
						background-color: rgba(40, 124, 206, 1);
						text-align: center;
						margin: 8rpx 10rpx 0rpx 0rpx;
					}

					.name {
						color: rgba(16, 16, 16, 1);
						font-size: 40rpx;
						text-align: left;
						font-weight: 600;
					}
				}

				.title-more {
					color: #101010;
					font-size: 14px;
					display: flex;
					align-items: center;
					margin-right: 30rpx;
				}
			}
		}
	}
</style>