<template>
	<view class="settings-page">
		<!-- 顶部导航栏 -->
		<view class="header">
			<view class="back-btn" @click="goBack">
				<text class="back-icon">←</text>
			</view>
			<text class="header-title">账户设置</text>
			<view class="placeholder"></view>
		</view>

		<scroll-view scroll-y class="content">
			<!-- 账号安全分组 -->
			<view class="group-block">
				<text class="group-title">账户安全</text>
				<view class="card">
					<view class="setting-item" @click="goToPage('changePassword')">
						<view class="item-left">
							<text class="item-icon shield">🛡️</text>
							<text class="item-text">修改密码</text>
						</view>
						<text class="item-arrow">›</text>
<!--					</view>-->
<!--					<text class="item-arrow">›</text>-->
<!--				</view>-->

				<view class="setting-item" @click="goToPage('about')">
					<view class="item-left">
						<text class="item-icon">ℹ️</text>
						<text class="item-text">关于我们</text>
					</view>
					<text class="item-arrow">›</text>
				</view>

				<view class="setting-item logout-item" @click="handleLogout">
					<view class="item-left">
						<text class="item-icon">🚪</text>
						<text class="item-text">退出登录</text>
					</view>
					<text class="item-arrow">›</text>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				phoneNumber: '未绑定',
				idCardNumber: '未绑定',
				educationStatus: '未认证',
				notifyNewMessage: true,
				notifyMatchSuccess: true,
				notifySystem: true,
				darkMode: false
			}
		},
		onLoad() {
			this.loadUserInfo()
		},
		methods: {
			goBack() {
				uni.navigateBack()
			},
			
			loadUserInfo() {
				// 获取用户信息
				const userInfo = uni.getStorageSync('userInfo')
				
				// 加载手机号
				if (userInfo && userInfo.phone) {
					this.phoneNumber = this.maskPhone(userInfo.phone)
				} else {
					this.phoneNumber = '未绑定'
				}
				
				// 加载身份证号
				if (userInfo && userInfo.idCard) {
					this.idCardNumber = this.maskIdCard(userInfo.idCard)
				} else {
					this.idCardNumber = '未绑定'
				}
				
				// 加载学历认证状态
				if (userInfo && userInfo.isEducationVerified) {
					this.educationStatus = '已认证'
				} else {
					this.educationStatus = '未认证'
				}
			},
			
			maskPhone(phone) {
				if (!phone || phone.length < 11) return phone
				return phone.substring(0, 3) + '****' + phone.substring(7)
			},
			
			maskIdCard(idCard) {
				if (!idCard || idCard.length < 8) return idCard
				return idCard.substring(0, 6) + '********' + idCard.substring(idCard.length - 4)
			},
			
			goToPage(page) {
			console.log('跳转页面:', page)
			
			if (page === 'bindIdCard') {
				// 跳转到身份证绑定页面
				uni.navigateTo({
					url: '/pages/settings/id-verification'
				})
			} 
			// else if (page === 'bindPhone') {
			// 	// 跳转到手机号绑定页面
			// 	uni.navigateTo({
			// 		url: '/pages/settings/phone-binding'
			// 	})
			// } 
			else if (page === 'about') {
				// 跳转到关于我们页面
				uni.navigateTo({
					url: '/pages/settings/about'
				})
			} else {
				// // 其他功能
				// uni.showToast({
				// 	title: '功能开发中',
				// 	icon: 'none'
				// })
			}
		},
			
			handleLogout() {
				uni.showModal({
					title: '退出登录',
					content: '确定要退出登录吗？',
					success: (res) => {
						if (res.confirm) {
							// 1. 断开WebSocket连接
							try {
								const timPresenceManager = require('@/utils/tim-presence-manager.js').default;
								if (timPresenceManager) {
									timPresenceManager.disconnect();
									console.log('✅ 在线状态WebSocket已断开');
								}
							} catch (error) {
								console.error('❌ 断开在线状态WebSocket失败:', error);
							}
							
							// 2. 断开TIM WebSocket
							try {
								const timManager = require('@/utils/tim-manager.js').default;
								if (timManager && timManager.isLogin) {
									timManager.logout();
									console.log('✅ TIM已登出');
								}
							} catch (error) {
								console.error('❌ TIM登出失败:', error);
							}
							
							// 3. 清除登录信息
							uni.clearStorageSync()
							
							// 4. 跳转到登录页
							uni.reLaunch({
								url: '/pages/page3/page3'
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.settings-page {
		min-height: 100vh;
		background: #F5F5F5;
		display: flex;
		flex-direction: column;
	}

	/* 顶部导航栏 */
	.header {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 25rpx 30rpx;
		padding-top: calc(25rpx + env(safe-area-inset-top));
		background: #FFFFFF;
		border-bottom: 1rpx solid #F0F0F0;
		
		.back-btn {
			width: 60rpx;
			height: 60rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			
			.back-icon {
				font-size: 40rpx;
				color: #333333;
				font-weight: bold;
			}
		}
		
		.header-title {
			font-size: 36rpx;
			font-weight: bold;
			color: #333333;
		}
		
		.placeholder {
			width: 60rpx;
		}
	}

	.content {
		flex: 1;
		background: #F5F5F5;
	}

	/* 分组标题 */
	.group-title {
		padding: 30rpx 30rpx 20rpx;
		font-size: 28rpx;
		color: #999999;
	}

	/* 设置列表 */
	.settings-list {
		background: #FFFFFF;
		
		.setting-item {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 35rpx 30rpx;
			border-bottom: 1rpx solid #F5F5F5;
			transition: background-color 0.2s;
			
			&:active {
				background-color: #F8F8F8;
			}
			
			&:last-child {
				border-bottom: none;
			}
			
			.item-left {
				display: flex;
				align-items: center;
				flex: 1;
				
				.item-icon {
					font-size: 40rpx;
					margin-right: 20rpx;
				}
				
				.item-text {
					font-size: 32rpx;
					color: #333333;
				}
			}
			
			.item-right {
				display: flex;
				align-items: center;
				
				.item-value {
					font-size: 28rpx;
					color: #999999;
					margin-right: 15rpx;
				}
				
				.item-arrow {
					font-size: 40rpx;
					color: #CCCCCC;
					font-weight: 300;
				}
			}
			
			.item-arrow {
				font-size: 40rpx;
				color: #CCCCCC;
				font-weight: 300;
			}
		}
	}
</style>
