<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="goPage('/pages/myCenter/myCenter')" />
			万载三中
		</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="https://wanzai-1306339220.cos.ap-shanghai.myqcloud.com/excelModel/nupp7VVnHWpVa413437dba82ce85374b2a4ee1a6b973.png" />
			<!-- 学校图标区域 -->
			<img mode="aspectFill" class="header_icon" src="../../static/images/school-logo.jpg" />
			<!-- 学校名称区域 -->
			<view class="header_school">万载县第三中学</view>
			<!-- 学生姓名区域 -->
			<view class="header_name">{{ userInfo.name }}</view>
			<!-- 学生年级区域 -->
			<view class="header_grade" v-if="userInfo.identityId == 1">家长</view>
			<view class="header_grade" v-if="userInfo.identityId == 2">学生</view>
			<view class="header_grade" v-if="userInfo.identityId == 3">教师</view>

			<!-- 二维码区域 -->
			<view class="header_code">
				<uv-qrcode ref="qrcodeRef" size="82rpx" :value="QRCodeUrl" :options="options" @click="handleClickCode"></uv-qrcode>
			</view>
		</view>

		<!-- 详细信息区域 -->
		<view class="body">
			<!-- 个人信息区域 -->
			<view class="body_header" v-if="userInfo.identityId == 1">
				<img class="info_bg" src="../../static/images/info-bg.png" />

				<img v-if="childMsgList.length" mode="aspectFill" class="info_img" :src="currentChild.headImage || '../../static/images/user-photo.png'" />
				<view v-if="childMsgList.length" class="info_name">{{ currentChild.name }}</view>

				<view v-if="childMsgList.length" class="info_detail" @click="goPage(`/pages/myMsg/myMsg?id=${currentChild.id}`)">孩子信息＞</view>

				<view v-if="childMsgList.length > 1" class="info_change" @click="goPage(`/pages/change/change?list=${JSON.stringify(childMsgList)}&id=${currentChild.id}`)">
					切换 ＞
				</view>

				<view v-if="!childMsgList.length" class="info_no">暂无孩子绑定</view>

				<!-- 关联区域 -->
				<view class="info_bind">
					<img @click="goPage('/pages/bind/bind')" class="bind_img" src="../../static/images/bind.png" />
					<text @click="goPage('/pages/bind/bind')">关联小孩</text>
				</view>
			</view>

			<!-- 应用列表区域 -->
			<view class="body_list" v-if="useAppList.length">
				<!-- 每一个应用区域 -->
				<view class="list_box" v-for="item in useAppList" :key="item.id" @click="goPage(item.path, item)">
					<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, onShow } 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
		}
	})

	userInfo.value = uni.getStorageSync('userInfo')

	if (userInfo.value.identityId == 1) {
		// 如果是家长身份获取绑定学生信息
		getChildMsg()
	}

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

	uni.$on('updateObj', updateObj)
})

onShow(() => {
	// 获取身份码信息
	getQrcode()
})

// 页面下拉刷新回调
onPullDownRefresh(() => {
	getQrcode()
	qrcodeRef.value.remake()
	if (userInfo.value.identityId == 1) {
		getChildMsg()
	}
	getUseAppList()
	setTimeout(() => {
		uni.stopPullDownRefresh()
	}, 1500)
})

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

// 用户信息
const userInfo = ref({})

// 绑定小孩信息
const childMsgList = ref([])

// 当前展示的小孩信息
const currentChild = ref({})

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

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

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

// 二维码元素引用标记
const qrcodeRef = ref(null)

// 全部应用列表数据
const list = ref([
	{
		id: 3,
		title: '学生轨迹',
		path: '/pages/track/track'
	},
	{
		id: 7,
		title: '消息通知',
		path: '/pages/msgWarn/msgWarn'
	},
	{
		id: 8,
		title: '访客预约',
		path: '/pagesReservation/reservation/reservation'
	},
	{
		id: 10,
		title: '校园预警',
		path: '/pagesWarning/home/home'
	},
	{
		id: 11,
		title: '通讯录',
		path: '/pages/grade/grade'
	},
	{
		title: '能耗管理',
		path: '/pages/energy/energy'
	}
])

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

// 二维码信息
const QRCodeUrl = ref('')

// 二维码自定义样式
const options = {
	typeNumber: -1,
	foregroundImageBorderRadius: 5,
	foregroundImageSrc: '/static/images/school-logo.jpg'
}

// 获取身份码信息
const getQrcode = async () => {
	const res = await myRequest({
		url: '/wanzai/api/smartQrcode/generateQrcode',
		data: {
			userId: uni.getStorageSync('userInfo').id
		}
	})
	// console.log(res)
	const result = JSON.parse(decryptDes(res.data))
	QRCodeUrl.value = result.qrcode
}

// 获取绑定小孩的信息
const getChildMsg = async () => {
	const res = await myRequest({
		url: '/wanzai/api/smartUser/queryAffiliateUserById',
		data: {
			id: uni.getStorageSync('userInfo').id
		}
	})
	// console.log(res)
	const result = JSON.parse(decryptDes(res.data))
	childMsgList.value = result

	if (childMsgList.value.length) {
		currentChild.value = childMsgList.value[0]
	}
}

// 获取用户应用权限列表
const getUseAppList = async () => {
	const res = await myRequest({
		url: '/wanzai/api/smartIdentity/queryIdentityApplyById',
		data: {
			id: uni.getStorageSync('userInfo').identityId
		}
	})
	// console.log(res)
	const result = JSON.parse(decryptDes(res.data))
	// console.log(result)
	useAppList.value = result
}

// 跳转页面函数
const goPage = (url, item) => {
	if (item && item.category == '自建小程序') {
		uni.navigateToMiniProgram({
			appId: item.appid,
			path: item.path
		})
		return
	}
	if (item && item.category == '移动端H5') {
		uni.navigateTo({
			url: `/pages/web/web?url=${item.path}`
		})
		return
	}
	if (url === '/pages/track/track') {
		if (!currentChild.value.id) {
			uni.showToast({
				title: '请关联孩子后再查看轨迹',
				icon: 'none',
				mask: true
			})
			return
		}
		uni.navigateTo({
			url: `/pages/track/track?id=${currentChild.value.id}`
		})
	}
	if (url === '/pagesReservation/reservation/reservation') {
		if (userInfo.value.identityId == 1 && !currentChild.value.id) {
			uni.showToast({
				title: '请关联孩子后再开始预约',
				icon: 'none',
				mask: true
			})
			return
		}
		uni.setStorageSync('Tab-activeIndex', 0)
	}
	uni.navigateTo({
		url
	})
}

// 点击二维码时的回调
const handleClickCode = () => {
	uni.navigateTo({
		url: `/pages/quickMark/quickMark?value=${QRCodeUrl.value}`
	})
}

const updateObj = (data) => {
	currentChild.value = data
}
</script>

<style lang="scss" scoped>
.container {
	display: flex;
	flex-direction: column;
	padding: 0 20rpx;
	height: 100vh;
	overflow: hidden;
	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;
		}

		.header_name {
			position: absolute;
			top: 243rpx;
			right: 145rpx;
		}

		.header_grade {
			position: absolute;
			top: 292rpx;
			right: 145rpx;
		}

		.header_code {
			position: absolute;
			top: 245rpx;
			right: 28rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			width: 98rpx;
			height: 98rpx;
			border-radius: 10rpx;
			background-color: #fff;
		}
	}

	// 详细信息区域样式
	.body {
		height: calc(100vh - 550rpx);
		margin-top: 20rpx;

		.body_header {
			position: relative;
			width: 710rpx;
			height: 128rpx;

			.info_bg {
				position: absolute;
				z-index: 1;
				width: 100%;
				height: 100%;
				pointer-events: none;
			}

			.info_img {
				position: absolute;
				top: 20rpx;
				left: 20rpx;
				z-index: 1;
				width: 90rpx;
				height: 90rpx;
				border-radius: 50%;
			}

			.info_name {
				position: absolute;
				top: 15rpx;
				left: 132rpx;
				z-index: 1;
				font-size: 32rpx;
				font-weight: bold;
			}

			.info_detail {
				position: absolute;
				top: 64rpx;
				left: 132rpx;
				z-index: 1;
				width: 136rpx;
				height: 46rpx;
				line-height: 46rpx;
				text-align: center;
				font-size: 24rpx;
				color: #0061ff;
				border-radius: 5rpx;
				background-color: #ebf2ff;
			}

			.info_change {
				position: absolute;
				top: 48rpx;
				left: 393rpx;
				z-index: 1;
				font-size: 24rpx;
				color: #a6a6a6;
			}

			.info_no {
				position: absolute;
				top: 38rpx;
				left: 163rpx;
				z-index: 1;
				font-size: 30rpx;
				color: #a6a6a6;
			}

			.info_bind {
				position: absolute;
				top: 20rpx;
				right: 0;
				display: flex;
				justify-content: flex-end;
				align-items: center;
				padding-right: 25rpx;
				width: 300rpx;
				height: 102rpx;
				font-size: 28rpx;
				color: #0061ff;
				background-color: #e8f1ff;

				.bind_img {
					margin-right: 5rpx;
					width: 42rpx;
					height: 42rpx;
				}
			}
		}

		.body_list {
			max-height: calc(100vh - 790rpx);
			overflow-y: auto;
			box-sizing: border-box;
			padding: 30rpx 30rpx;
			display: grid;
			grid-template-columns: repeat(3, 1fr);
			grid-auto-rows: 240rpx;
			background-color: #fff;
			gap: 20rpx 10rpx;

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