<!-- index/index.vue -->
<template>
	<view class="index">
		<!-- 头部个人信息 -->
		<view class="header">
			<view class="left">
				<image :src="avater" mode=""></image>
			</view>
			<view class="right">
				<text>账号：{{username}}</text>
				<text>姓名：{{nickName}}</text>
				<!-- <text>班级：{{studentClass}}</text> -->
			</view>
		</view>

		<uni-notice-bar show-icon scrollable :text="noticeText" />

		<!-- 个人信息结束 -->
		<!-- 工作台 -->
		<view class="workbench">
			<view class="workbench-grid" v-for="(item,index) in iconBoxList">
				<view class="title">
					{{item.meta.title}}
				</view>
				<view class="list">
					<view class="item" v-for="(child,index) in item.children" :key="index"
						@click="gotopage(child.component)">
						<image :src="getImgUrl(child.path)" mode="aspectFit"></image>
						<text> {{child.meta.title}}</text>
						<text class="num" v-show="child.meta.title=='任务管理'&&total">{{total}}</text>
						<text class="num" v-show="child.meta.title=='工作记录审核'&&qgzxLogTotal">{{qgzxLogTotal}}</text>
						<text class="num" v-show="child.meta.title=='部门薪资审核'&&qgzxMenoyTotal">{{qgzxMenoyTotal}}</text>
					</view>
				</view>
			</view>

			<!-- AI 悬浮按钮 -->
			<view class="ai-hover" @click="toAI">
				<view class="ai-hover-content">
					<text class="ai-hover-text">AI</text>
				</view>
			</view>

			<!-- <view>{{log}}</view> -->
		</view>
	</view>
</template>

<script>
	import {
		getToken,
		setToken
	} from '@/utils/auth'
	import {
		getOwnInfo,
		getInfo
	} from "@/api/person/person.js";
	import {
		getUserProfile
	} from "@/api/system/user.js";
	import {
		getRouters
	} from '@/api/login'
	import {
		todoList
	} from "@/api/task.js";
	import {
		listZdls as getList,
	} from "@/api/workStudy/worklog";
	import {
		listDept as getMoneyList,
	} from "@/api/workStudy/money";
	import {
		initCoze
	} from "@/utils/ai.js";
	import {
		getAccessToken
	} from "@/api/aiJWT/aiJWT.js"
	export default {
		data() {
			return {
				total: "",
				qgzxLogTotal: "", //指导老师审核记录数量
				qgzxMenoyTotal: "", //部门审核数据
				avater: require('../../static/avatar2.png'),
				nickName: '',
				username: '',
				studentId: "",
				studentClass: "",
				noticeText: "您好, ",

				// 图标列表
				iconBoxList: [], // [{
				// 		img: require('../../static/images/workbench/degree-hat.png'),
				// 		text: '学业奖学金',
				// 		path: "/pages/award/award"
				// 	},
				// 	{
				// 		img: require('../../static/images/workbench/Subtract.png'),
				// 		text: '优秀毕业生',
				// 		path: "/pages/graduate/graduate"
				// 	},
				// 	{
				// 		img: require('../../static/images/workbench/peoples.png'),
				// 		text: '三好学生',
				// 		path: "/pages/student/student"
				// 	},
				// 	{
				// 		img: require('../../static/images/workbench/effects.png'),
				// 		text: '静湖之星',
				// 		path: "/pages/star/star"
				// 	},
				// 	{
				// 		img: require('../../static/images/workbench/finance.png'),
				// 		text: '助学金',
				// 		path: "/pages/Financialaid/Financialaid"
				// 	},
				// 	{
				// 		img: require('../../static/images/workbench/Vector.png'),
				// 		text: '贫困认定',
				// 		path: "/pages/cognizance/cognizance"
				// 	},
				// 	{
				// 		img: require('../../static/images/workbench/necktie.png'),
				// 		text: '优秀学生干部',
				// 		path: "/pages/cadre/cadre"
				// 	},
				// 	{
				// 		img: require('../../static/images/workbench/add.png'),
				// 		text: '思想品德加分',
				// 		path: "/pages/add/add"
				// 	}, {
				// 		img: require('../../static/images/workbench/home.png'),
				// 		text: '调宿管理',
				// 		path: "/pages/administration/administration"
				// 	},
				// 	{
				// 		img: require('../../static/images/workbench/calendar-alt.png'),
				// 		text: '请假管理',
				// 		// path: "/pages/applyleave/applyleave"
				// 		path: "/pages/applyleave/list"
				// 	},
				// 	{
				// 		img: require('../../static/images/workbench/ban.png'),
				// 		text: '解除处分',
				// 		path: "/pages/applyrelieve/applyrelieve"
				// 	},
				// 	{
				// 		img: require('../../static/images/workbench/edit.png'),
				// 		text: '任务管理',
				// 		path: "/pages/Approval/Approval",
				// 		num: this.total
				// 	},
				// 	{
				// 		img: require('../../static/images/workbench/class-meeting.png'),
				// 		text: '班会管理',
				// 		path: "/pages/classMeeting/classMeeting",
				// 	},
				// 	{
				// 		img: require('../../static/images/workbench/One-stop-management.png'),
				// 		text: '一站式管理',
				// 		path: "/pages/functionRoom/functionRoom",
				// 	},
				// 	{
				// 		img: require('../../static/images/workbench/instructor.png'),
				// 		text: '辅导员管理',
				// 		path: "/pages/superintendent/index",
				// 	}

				// ]

				log: "log",
				// 新增：AI聊天显示状态，与聊天页同步
				isAIChatVisible: true
			}
		},
		onLoad(query) {
			let ticket = query.jwt;
			// console.log("jwt", ticket)
			// this.log = '标题' + ticket + window.location.href
			if (ticket) {
				setToken(ticket);
				this.getUserInfo();
				this.getUndo();
				this.getUserRouters();
				this.getQgzxLogExamineTotal()
				this.getqgzxMenoyTotal();
			}

			// 从本地存储获取AI状态
			const aiStatus = uni.getStorageSync('aiVisibleStatus');
			if (aiStatus !== null && aiStatus !== undefined) {
				this.isAIChatVisible = aiStatus;
			} else {
				// 默认显示
				this.isAIChatVisible = true;
				uni.setStorageSync('aiVisibleStatus', true);
			}
		},
		onShow() {
			// 检查token是否存在
			if (!getToken()) {
				uni.showToast({
					title: '请先登录',
					icon: 'none'
				});
				uni.navigateTo({
					url: '/pages/login/index'
				});
				return;
			}
			this.getUserInfo();
			this.getUndo();
			this.getUserRouters();
			this.getQgzxLogExamineTotal()
			this.getqgzxMenoyTotal();

			// 页面显示时同步AI状态
			this.isAIChatVisible = uni.getStorageSync('aiVisibleStatus') || true;
		},
		methods: {
			getUserRouters() {
				let params = {
					menuUseful: 1
				}
				getRouters(params).then(res => {
					console.log(res);
					if (res.data.length > 0) {
						this.iconBoxList = res.data[0].children;
					}
				});
			},
			getUndo() {
				let sdata = {
					pageNum: 1,
					pageSize: 10
				};
				todoList(sdata).then(res => {
					this.total = res.data.total;
					// this.iconBoxList[11].num = this.total;
				})
			},
			getQgzxLogExamineTotal() {
				let sdata = {
					pageNum: 1,
					pageSize: 10
				};
				getList(sdata).then(res => {
					this.qgzxLogTotal = res.total;
				})
			},
			getqgzxMenoyTotal() {
				let sdata = {
					pageNum: 1,
					pageSize: 10
				};
				getMoneyList(sdata).then(res => {
					this.qgzxMenoyTotal = res.total;
				})
			},
			// 页面调转
			gotopage(path) {
				uni.navigateTo({
					url: path
				})
			},
			// 获取个人信息
			getUserInfo() {
				getUserProfile().then(res => {
					if (res.code == 200) {
						let data = {
							...res.data
						};
						console.log(187, data)
						uni.setStorageSync('stuName', data.nickName);
						uni.setStorageSync('stuId', data.userId);
						uni.setStorageSync("stuNo", data.userName)
						//uni.setStorageSync("deptName", data.dept.deptName)
						uni.setStorageSync("roles", res.roleGroup)
						this.nickName = data.nickName
						this.username = data.userName
						this.noticeText = "你好 ，" + this.nickName;
					}
				});
			},
			//组装图片路径
			getImgUrl(name) {
				return require('../../static/images/workbench/' + name + '.png');
			},
			//跳转到AI
			toAI() {
				uni.navigateTo({
					url: '/pages/aiChat/simple_chat'
				});
			},

			async getAccessToken() {
				const res = await getAccessToken(); // 调用请求函数
				const data = JSON.parse(res.data); // 解析数据
				return data; // ✅ 返回 data
			}
		}
	}
</script>

<style lang="less" scoped>
	.index {
		background-color: #F3F4F6;
		padding-bottom: 10rpx;
		min-height: 95vh;
	}

	.header {
		display: flex;
		align-items: center;
		width: 100%;
		height: 300rpx;
		background: url("../../static/top.png");
		padding: 0rpx 40rpx;

		.left {
			padding-top: 20rpx;
			width: 170rpx;
			height: 170rpx;
			border-radius: 50%;

			image {
				width: 160rpx;
				height: 160rpx;
			}
		}

		.right {
			display: flex;
			flex-direction: column;
			justify-content: space-around;
			margin-left: 20rpx;
			width: 65%;
			height: 160rpx;

			color: #fff;

			text {
				padding-top: 10rpx;
			}
		}

	}

	.workbench {
		width: 100%;
		padding: 5px 15px 0px;

		.workbench-grid {
			background-color: white;
			margin-bottom: 10px;
			padding: 20px 0;
			border-radius: 10px;

			.title {
				color: gray;
				font-size: 36rpx;
				margin: 0 0 25px 15px;
			}

			.list {
				display: flex;
				flex-wrap: wrap;

				.item {
					position: relative;
					width: 25%;
					display: flex;
					flex-direction: column;
					align-items: center;
					margin-bottom: 50rpx;
					color: #202020;
					font-size: 27rpx;

					image {
						width: 80rpx;
						height: 80rpx;
						margin-bottom: 10rpx;
					}

					.num {
						position: absolute;
						background-color: red;
						color: white;
						right: 8px;
						top: 0;
						width: 50rpx;
						height: 50rpx;
						line-height: 50rpx;
						border-radius: 50%;
						text-align: center;
						font-size: 30rpx;
					}
				}
			}

		}
	}

	// ai悬停
	.ai-hover {
		position: fixed;
		bottom: 120rpx;
		right: 40rpx;
		z-index: 999;
		width: 100rpx;
		height: 100rpx;
		border-radius: 50%;
		background-color: #409eff;
		color: #fff;
		display: flex;
		justify-content: center;
		align-items: center;
		cursor: pointer;
		box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
		transition: transform 0.2s;
	}

	.ai-hover-content {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 100%;
		height: 100%;
	}

	.ai-hover-text {
		font-size: 36rpx;
		font-weight: bold;
	}

	.ai-hover:active {
		transform: scale(0.95);
	}
</style>