<template>
	<view class="newindex">
		<view class="exit" @click="exitUser">退出</view>
		<!-- 上方个人信息 -->
		<view class="onebox">
			<view class="twobox">
				<view class="img">
					<image style="height: 100%;" model="aspectFill" @click="toOwnPage" src="../../static/logo.png"
						mode=""></image>
				</view>
				<view class="message">
					<view><text>姓名：{{stu_name}}</text> <text v-if="isDirection=='1'">(定向生)</text></view>
					<!-- <text>暂定班级：{{bj}}</text> -->
					<text>学号：{{xh}}</text>
				</view>
				<!-- 二维码 -->
				<view class="qr-code" @click="toggleQRPreview">
					<image v-if="!showQRPreview" src="../../static/ewm1.png" mode=""></image>
					<view v-if="showQRPreview" class="qr-preview">
						<!-- 这里放置二维码预览 -->
						<image src="../../static/ewm.png" mode=""></image>
					</view>

				</view>
			</view>
		</view>
		<!-- 线上迎新模块 -->
		<view class="newbox">
			<view class="topbox">
				<view class="left">
					<text>线上迎新</text>
					<text class="time">2024-8-26 09:00~2024-9-9 12:30</text>
				</view>
				<view class="right">
					<text>总进度 {{ completedCardsCount }}/{{ totalCards }}</text>
					<text class="title">请先完成必办</text>
				</view>
			</view>
			<view class="bigbox">
				<view class="cardList">
					<view :class="[
				    card.isActive == '1' ? 'active' : 'card']" v-for="(card, index) in cardList" :key="index"
						@click="goToDetail(index)">
						<text>{{ card.projectName }}</text>
						<text>{{ card.isMust == '1' ? "必办" : "" }}</text>
						<text v-if="index === 6 && card.isActive !== '1'" class="special-text">如未核验，请尽快核验信息（线下核验）</text>
						<view class="buttom">
							<text v-if="card.isActive == '1'">已办理</text>
							<image v-else src="../../static/go.png" mode=""></image>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 弹窗 -->


		<!-- 弹窗遮罩层 -->
		<view v-if="showPopup" class="popup-mask"></view>
		<view v-if="showPopup" class="popup">
			<text>安全提示</text>
			<view>欢迎进入页面，请修改密码以确保账户安全！</view>
			<view class="btn">
				<button @click="closePopup">下次一定</button>
				<button @click="gotoChangePassword" class="password">修改密码</button>
			</view>
		</view>

		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import {
		getUserInfo,
		exitLogin,
	} from "@/api/validApi.js";

	import {
		listTask,
		listOwnTask
	} from "@/api/toApi.js";
	import {
		isEmpty
	} from "@/api/helpFunc.js";

	export default {
		name: "newindex",
		data() {
			return {
				isEmpty,
				showPopup: false, // 控制弹窗显示隐藏
				isopen: "",
				showQRPreview: false,
				stu_name: "",
				isDirection: "",
				bj: "",
				xh: "",
				cardList: []
			}
		},
		created() {
			this.getOwnInfo();
			this.listTask();
		},
		onshow() {
			this.getOwnInfo();
			this.listTask();
		},
		methods: {
			shouldDisplayDefaultState(index) {
				return index >= 4 && index <= 7; // 索引为5、6、7、8的卡片将显示默认状态
			},
			toOwnPage() {
				uni.navigateTo({
					url: '/pages/Userinformation/Userinformation' // 跳转至修改密码页面路径
				});
			},
			async listTask() {
				let sdata = {
					projectName: null
				}
				let res = await listTask(sdata);
				if (res.code == 200) {
					let tempCardList = [...res.data]; // 临时保存数据
					// console.log(tempCardList)
					let res1 = await listOwnTask();
					// console.log(res1)
					if (res1.code == 200) {
						if (!isEmpty(res1.data)) {
							tempCardList.map(x => {
								res1.data.map(y => {
									if (x.taskCode == y.taskCode) {
										x.isActive = y.status;
									}
								});
							});
						}
					}
					if (this.isDirection == '1') {
						var index = tempCardList.findIndex(item => {
							if (item.taskCode === "ZXJF") {
								return true;
							}
						})
						tempCardList.splice(index, 1);
					}
					// 交换第6条和第7条的位置
					// if (tempCardList.length >= 7) {
					// 	const temp = tempCardList[6];
					// 	tempCardList[6] = tempCardList[7];
					// 	tempCardList[7] = temp;
					// }
					// 更新组件状态
					this.cardList = tempCardList;
					//console.log(this.cardList);
				}
			},

			exitUser() {
				exitLogin().then(res => {
					uni.navigateTo({
						url: '/pages/login/index' // 跳转至修改密码页面路径
					});
				});
			},
			dialogToggle(type) {
				this.msgType = type
				this.$refs.alertDialog.open()
			},
			toggleQRPreview() {
				this.showQRPreview = !this.showQRPreview;
			},
			goToDetail(index) {
				console.log(this.cardList);
				if (index < 6) {
					uni.navigateTo({
						url: this.cardList[index].url,
					});
				} else if (index == 6 || index == 7 || index == 8) {
					let all = true;
					this.cardList.map(x => {
						if (x.isActive != '1' && (x.rowRank < 7 && x.rowRank != null)) {
							all = false;
						}
					});
					// if (all == false) {
					// 	this.$refs.uToast.show({
					// 		type: "error",
					// 		message: "请先完成上面的内容",
					// 		duration: 1500,
					// 	});
					// 	return;
					// } else {
						
					// }
					uni.navigateTo({
						url: this.cardList[index].url,
					});

				} else if (index == 6) {
					let all = true;
					this.cardList.map(x => {
						if (x.isActive != '1' && (x.rowRank < 8 && x.rowRank != null)) {
							all = false;
						}
					});
					// if (all == false) {
					// 	this.$refs.uToast.show({
					// 		type: "error",
					// 		message: "请先完成上面的内容",
					// 		duration: 1500,
					// 	});
					// 	return;
					// } else {
					// 	uni.navigateTo({
					// 		url: this.cardList[index].url,
					// 	});
					// }
					uni.navigateTo({
						url: this.cardList[index].url,
					});
					
				} else {
					uni.navigateTo({
						url: this.cardList[index].url,
					});
				}

			},
			closeReminder() {
				this.showPopup = false; // 关闭提醒弹窗
			},
			closePopup() {
				this.showPopup = false; // 关闭弹窗
			},
			gotoChangePassword() {
				uni.navigateTo({
					url: '/pages/Userinformation/Userinformation' // 跳转至修改密码页面路径
				});
			},
			async getOwnInfo() {
				//console.log(1111);
				let res = await getUserInfo();
				if (res.code == 200) {
					let data = {
						...res.data
					};
					if (data.isChangePwd == "1") {
						this.showPopup = false;
					} else {
						this.showPopup = true;
					}
					this.stu_name = data.xsxm
					this.bj = data.bj
					//this.xh = data.xh
					this.xh=data.ksh
					this.isDirection = data.isDirection;
				} 
			} 
		},
		computed: {
			totalCards() {
				// 总卡片数量，假设为 cardList 的长度
				let tot = this.cardList.length;
				// if(this.isDirection==="1"){
				// 	tot-=1;
				// }
				return tot;
			},
			completedCardsCount() {
				// 完成的卡片数量
				return this.cardList.filter(card => card.isActive === '1').length;
			}
		}
	}
</script>

<style scoped lang="scss">
	.newindex {
		width: 100%;
		height: 100%;
		background-color: #afc9fe;
		padding-top: 40rpx;
	}

	.exit {
		background-color: rgba(97, 176, 254, 0.7);
		position: fixed;
		width: 100rpx;
		height: 100rpx;
		text-align: center;
		line-height: 100rpx;
		border-radius: 50%;
		color: white;
		font-size: 28rpx;
		right: 20px;
		bottom: 50px;
	}

	.onebox {
		display: flex;
		align-items: center;
		margin: 0 auto 40rpx;
		border-radius: 40rpx;
		width: 85%;
		// height: 220rpx;
		background-color: rgba(255, 255, 255, 0.7);
		padding: 20rpx;
	}

	.twobox {
		display: flex;
		margin: 0 auto;
		border-radius: 30rpx;
		width: 95%;
		// align-items: center;
		// height: 190rpx;
		background-color: white;
		padding: 0rpx 20rpx 20rpx;
	}

	.twobox .img {
		height: 150rpx;
		margin-top: 20rpx;
		border-radius: 10rpx;

	}

	.twobox .img image {
		width: 150rpx;
		height: 150rpx;
		border-radius: 10rpx;
	}

	.twobox .message {
		margin-top: 20rpx;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		margin-left: 10rpx;
		width: 60%;
		// height: 150rpx;
	}


	.qr-code {

		width: 100rpx;
		height: 100rpx;
		margin-top: 20rpx;
	}

	.qr-code image {
		width: 100rpx;
		height: 100rpx;
	}

	.newbox {
		display: flex;
		justify-content: center;
		flex-direction: column;
		margin: 0 auto 20px;
		border-radius: 40rpx;
		width: 90%;
		// height: 1200rpx;
		background-color: rgba(255, 255, 255, 0.7);
	}

	.topbox {
		display: flex;
		justify-content: space-between;
		margin-top: 30rpx;
		width: 95%;
		/* height: 200rpx; */
		margin-bottom: 20rpx;

	}

	.left {

		display: flex;
		flex-direction: column;
		justify-content: space-between;
		width: 90%;
		height: 90rpx;

		margin-left: 20rpx;
	}

	.left text {
		font-size: 38rpx;
		font-weight: bold;
		color: black;
	}

	.left .time {
		color: #62727F;
		font-size: 24rpx
	}

	.right {
		display: flex;
		flex-direction: column;
		align-items: flex-end;

		width: 35%;
		height: 90rpx;

	}

	.right .title {
		color: #62727F;
		font-size: 24rpx;
		margin-top: 10rpx;
	}

	.right text {
		color: #4F87C4;
		font-weight: bold;
	}

	.bigbox {
		min-height: 800rpx;

		.cardList {
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;
			margin: 0 auto;
			margin-bottom: 30rpx;
			width: 95%;
			padding: 0;

			&>view {
				margin-bottom: 35rpx;
			}

			.special-text {
				color: red;
				font-size: 20rpx;
			}

		}
	}







	.card {
		position: relative;
		display: flex;
		flex-direction: column;
		width: 30%;
		height: 240rpx;
		background-color: white;
		border-radius: 20rpx;

		text {
			margin-top: 10rpx;
			margin-left: 10rpx;
			color: #717F8C;
			font-size: 25rpx
		}

		text:first-child {
			font-size: 30rpx;
			color: black;
			font-weight: bold;
		}

		.buttom {
			position: absolute;
			color: white;
			width: 100%;
			height: 60rpx;
			border-radius: 0 0 20rpx 20rpx;
			background-color: #5883CB;
			bottom: 0;
			display: flex;
			align-items: center;
			justify-content: end;

			text {
				font-size: 28rpx;
				color: white;
				margin-left: 10rpx;
			}

			image {
				margin-right: 10rpx;
				width: 40rpx;
				height: 40rpx;
			}
		}
	}

	.active {
		position: relative;
		display: flex;
		align-items: start;
		flex-direction: column;
		width: 30%;
		height: 240rpx;
		background-color: white;
		border-radius: 20rpx;

		text {
			margin-top: 10rpx;
			margin-left: 10rpx;
			color: #717F8C;
			font-size: 25rpx
		}



		.buttom {


			position: absolute;
			color: white;
			width: 100%;
			height: 60rpx;
			border-radius: 0 0 20rpx 20rpx;

			background-color: #26C791;
			bottom: 0;

			text {
				font-size: 28rpx;
				color: white;
				margin-right: 20rpx;

			}
		}

	}

	.defaultState .buttom {
		background-color: #D3D3D3;

	}

	.defaultState text {
		font-size: 28rpx;
		margin-right: 20rpx;
		color: #717F8C;

	}

	.popup-mask {
		position: fixed;
		top: 0;
		left: 0;
		width: 100vw;
		height: 100vh;
		background: rgba(0, 0, 0, 0.5);
		z-index: 999;
	}

	.popup {
		display: flex;
		flex-direction: column;
		align-items: center;
		width: 80%;
		height: 300rpx;
		position: fixed;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
		background: #fff;
		padding: 20rpx;
		border-radius: 10rpx;
		z-index: 1000;

		text {
			font-size: 40rpx;
			font-weight: bold;
			margin-bottom: 10rpx;
		}

		.btn {
			margin-top: 50rpx;
			display: flex;
			width: 100%;

			button {
				width: 280rpx;
			}

			.password {
				background-color: #59a1fe;
				color: white;
			}
		}
	}
</style>