<template>
	<view class="content">
		<view class="navbar">
			<view class="logo">
				<image src="/static/images/logo.png" mode="heightFix"></image>
			</view>
		</view>

		<view class="menu">
			<view class="one-menu" :style="colorlsit[index]" v-for="(item,index) in menuList" :key="index"
				@click="toPage(index)">
				<img :src="item.img">
				<view class="text">{{item.name}}</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				indicatorDots: true,
				autoplay: true,
				interval: 3000,
				duration: 500,
				swiperList: [],
				article: [],
				menuList: [],
				articleList: [],
				colorlsit: [
					"background: linear-gradient( 90deg, #6077EB 0%, #3457D6 100%);",
					"background: linear-gradient( 90deg, #54BDC5 0%, #239CB3 100%);",
					"background: linear-gradient( 90deg, #66A4DD 0%, #3488D6 100%);",
				]
			};
		},
		onLoad() {
		
		},
		onShow() {
			this.getSwiperList();
			this.getArticleList();
			this.getTableList();
			this.getInfo()


		},
		methods: {
		async getInfo(){
			let res = await this.$myRequest({
					url: '/getInfo',
				})
			
			uni.setStorageSync('CodeDeptId', res.data.dept.deptId);
			uni.setStorageSync('CodeDeptName', res.data.dept.deptName);
			},
			// 用户拒绝授权的展示
			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: #fff;
	}

	.content {
		.navbar {
			width: 100%;

			display: flex;
			align-items: center;
			justify-content: center;


		}

		.logo {
			margin: 45px auto;

			image {
				width: 250px;
				height: 100px;
			}
		}


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

			.one-menu {
				width: 600rpx;
				height: 340rpx;
				background: linear-gradient(90deg, #6077EB 0%, #3457D6 100%);
				box-sizing: border-box;
				padding-top: 30px;
				line-height: 60rpx;
				border-radius: 30rpx;
				background-color: rgba(255, 255, 255, 1);
				text-align: center;
				margin: 0px 25px;
				box-shadow: 0px 2px 6px 0px #ccc;
				font-weight: bold;
				font-size: 20px;
				color: #FFFFFF;

				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>