<template>
	<view class="container">
		<!-- 自定义导航栏区域 -->
		<view class="tab" :style="{ height: customBarH * 2 + 'rpx', paddingTop: statusBarH * 2 + 'rpx' }" v-show="!showHeader">
			<img class="tab_img" src="../../static/images/people.png" @click="goLogin(1)" />
			万载三中
		</view>

		<!-- 页面下拉时显示的导航栏 -->
		<view class="tab2" :style="{ height: customBarH * 2 + 'rpx', paddingTop: statusBarH * 2 + 'rpx' }" v-show="showHeader">万载三中</view>

		<!-- 顶部图片区域 -->
		<view class="header">
			<img mode="aspectFill" class="header_img" src="../../static/images/header.png" />
			<!-- 学校图标区域 -->
			<img mode="aspectFill" class="header_icon" src="../../static/images/school-logo.jpg" />
			<!-- 学校名称区域 -->
			<view class="header_school">万载县第三中学</view>
		</view>

		<!-- 登录按钮区域 -->
		<view class="login">
			<view class="login_btn" @click="goLogin(2)">登录</view>
		</view>

		<!-- 详细信息区域 -->
		<view class="body">
			<!-- 应用列表区域 -->
			<view class="body_list" v-if="useAppList.length">
				<!-- 每一个应用区域 -->
				<view class="list_box" v-for="item in useAppList" :key="item.id" @click="goLogin(1)">
					<img class="box_img" :src="item.url" />
					<view class="box_text">
						{{ item.title }}
					</view>
				</view>
			</view>

			<NoData v-if="!useAppList.length" />
		</view>
	</view>
</template>

<script setup>
import { ref } from 'vue'
import { onLoad, onPageScroll, onPullDownRefresh } from '@dcloudio/uni-app'
import { myRequest } from '@/utils/api.js'
import NoData from '@/components/noData.vue'
import { decryptDes } from '@/utils/des.js'

onLoad(() => {
	// 获取系统信息
	uni.getSystemInfo({
		success: (e) => {
			// 获取状态栏高度
			statusBarH.value = e.statusBarHeight
			// // 获取菜单按钮栏高度
			let custom = uni.getMenuButtonBoundingClientRect()
			customBarH.value = custom.height
		}
	})

	// 获取用户应用权限列表
	getUseAppList()
})

onPageScroll((e) => {
	if (e.scrollTop > 70) {
		showHeader.value = true
	} else {
		showHeader.value = false
	}
})

// 顶部导航栏显示隐藏控制
const showHeader = ref(false)

// 状态栏高度
const statusBarH = ref(0)

// 胶囊按钮栏高度
const customBarH = ref(0)

// 展示的app列表
const useAppList = ref([])

// 获取用户应用权限列表
const getUseAppList = async () => {
	const res = await myRequest({
		url: '/wanzai/api/smartIdentity/queryIdentityApplyById',
		data: {
			// 未登录时默认展示家长权限的应用
			id: 1
		}
	})
	// console.log(res)
	const result = JSON.parse(decryptDes(res.data))
	useAppList.value = result
}

// 跳转登录页
const goLogin = (type) => {
	if (type === 1) {
		uni.showToast({
			title: '请您先登录',
			icon: 'none',
			mask: true
		})
		setTimeout(() => {
			uni.navigateTo({
				url: '/pages/login2/login2'
			})
		}, 1500)
	} else {
		uni.navigateTo({
			url: '/pages/login2/login2'
		})
	}
}
</script>

<style lang="scss" scoped>
.container {
	display: flex;
	flex-direction: column;
	padding: 0 20rpx;
	min-height: 100vh;
	background-color: #f1f6fe;

	// 自定义导航栏区域样式
	.tab {
		position: relative;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 34rpx;

		.tab_img {
			position: absolute;
			top: v-bind(statusBarH);
			left: 0;
			width: 62rpx;
			height: 62rpx;
		}
	}

	.tab2 {
		z-index: 999;
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		padding: 0 20rpx;
		font-size: 34rpx;
		text-align: center;
		background-color: #fff;
	}

	// 顶部图片区域样式
	.header {
		position: relative;
		margin-top: 35rpx;
		width: 710rpx;
		height: 369rpx;
		color: #fff;
		font-size: 36rpx;
		border-radius: 10rpx;

		.header_img {
			width: 100%;
			height: 100%;
		}

		.header_icon {
			position: absolute;
			top: 25rpx;
			left: 25rpx;
			width: 47rpx;
			height: 47rpx;
			border-radius: 50%;
		}

		.header_school {
			position: absolute;
			top: 18rpx;
			left: 81rpx;
			font-size: 40rpx;
		}
	}

	// 登录按钮区域样式
	.login {
		display: flex;
		justify-content: center;
		align-items: center;
		height: 180rpx;

		.login_btn {
			display: flex;
			justify-content: center;
			align-items: center;
			width: 380rpx;
			height: 80rpx;
			color: #7abfa4;
			border-radius: 50rpx;
			border: 1rpx solid #7abfa4;
			background-color: #fff;
		}
	}

	// 详细信息区域样式
	.body {
		.body_list {
			padding: 30rpx 30rpx;
			display: grid;
			grid-template-columns: repeat(3, 1fr);
			grid-auto-rows: 240rpx;
			background-color: #fff;
			gap: 20rpx 10rpx;
			border-radius: 20rpx;

			.list_box {
				display: flex;
				flex-direction: column;
				justify-content: space-evenly;
				align-items: center;
				font-size: 28rpx;
				overflow: hidden;

				.box_img {
					width: 120rpx;
					height: 120rpx;
					border-radius: 32rpx;
				}

				.box_text {
					width: 100%;
					text-align: center;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
				}
			}
		}
	}
}
</style>
