<template>
	<view class="container">
		<view class="header">
			<view class="user-info" v-if="isLoggedIn">
				<view class="avatar">
					<text class="avatar-text">{{ userInfo.nickname ? userInfo.nickname.charAt(0) : '用' }}</text>
				</view>
				<view class="user-details">
					<text class="nickname">{{ userInfo.nickname || '酒谷用户' }}</text>
					<text class="phone">{{ userInfo.phone || '' }}</text>
				</view>
			</view>
			<view class="login-prompt" v-else>
				<view class="avatar">
					<text class="avatar-text">未</text>
				</view>
				<view class="user-details">
					<text class="nickname">未登录</text>
					<button class="login-btn" @click="goLogin">立即登录</button>
				</view>
			</view>
		</view>
		
		<view class="menu-section">
			<view class="menu-group">
				<view class="menu-item" @click="handleMenuClick('orders')">
					<text class="menu-icon">📦</text>
					<text class="menu-text">我的订单</text>
					<text class="menu-arrow">></text>
				</view>
				<view class="menu-item" @click="handleMenuClick('favorites')">
					<text class="menu-icon">❤️</text>
					<text class="menu-text">我的收藏</text>
					<text class="menu-arrow">></text>
				</view>
				<view class="menu-item" @click="handleMenuClick('address')">
					<text class="menu-icon">📍</text>
					<text class="menu-text">收货地址</text>
					<text class="menu-arrow">></text>
				</view>
			</view>
			
			<view class="menu-group">
				<view class="menu-item" @click="handleMenuClick('coupon')">
					<text class="menu-icon">🎫</text>
					<text class="menu-text">优惠券</text>
					<text class="menu-arrow">></text>
				</view>
				<view class="menu-item" @click="handleMenuClick('points')">
					<text class="menu-icon">💎</text>
					<text class="menu-text">积分中心</text>
					<text class="menu-arrow">></text>
				</view>
				<view class="menu-item" @click="handleMenuClick('service')">
					<text class="menu-icon">🎧</text>
					<text class="menu-text">客服中心</text>
					<text class="menu-arrow">></text>
				</view>
			</view>
			
			<view class="menu-group">
				<view class="menu-item" @click="handleMenuClick('settings')">
					<text class="menu-icon">⚙️</text>
					<text class="menu-text">设置</text>
					<text class="menu-arrow">></text>
				</view>
			</view>
		</view>
		
		<view class="logout-section" v-if="isLoggedIn">
			<button class="logout-btn" @click="handleLogout">
				退出登录
			</button>
		</view>
	</view>
</template>

<script>
	import { isLoggedIn, getUserInfo, requireAuth, navigateToLogin, clearUserData, PROTECTED_ACTIONS } from '@/utils/auth.js'
	
	export default {
		name: 'User',
		data() {
			return {
				isLoggedIn: false,
				userInfo: {}
			}
		},
		onLoad() {
			console.log('我的页面加载')
		},
		onShow() {
			this.checkLoginStatus()
			// 无论是否登录都进行权限检查，确保游客模式下能正确触发登录
			this.checkAuthAccess()
			// 更新自定义tabBar选中状态
			this.updateTabBar()
		},
		methods: {
			// 更新tabBar选中状态
			updateTabBar() {
				try {
					if (typeof this.getTabBar === 'function' && this.getTabBar()) {
						this.getTabBar().updateSelected(4) // 我的索引为4
					}
				} catch (error) {
					console.log('更新tabBar状态失败:', error)
				}
			},
			
			checkLoginStatus() {
				this.isLoggedIn = isLoggedIn()
				if (this.isLoggedIn) {
					this.userInfo = getUserInfo() || {}
				}
			},
			
			checkAuthAccess() {
				requireAuth(() => {
					console.log('用户已登录，可以访问个人中心')
					this.checkLoginStatus()
				}, {
					action: PROTECTED_ACTIONS.VIEW_MY_PAGE,
					message: '访问个人中心需要先登录'
				})
			},
			
			goLogin() {
				navigateToLogin('/pages/user/index')
			},
			
			handleMenuClick(type) {
				requireAuth(() => {
					switch(type) {
						case 'orders':
							uni.showToast({ title: '我的订单', icon: 'none' })
							break
						case 'favorites':
							uni.showToast({ title: '我的收藏', icon: 'none' })
							break
						case 'address':
							uni.showToast({ title: '收货地址', icon: 'none' })
							break
						case 'coupon':
							uni.showToast({ title: '优惠券', icon: 'none' })
							break
						case 'points':
							uni.showToast({ title: '积分中心', icon: 'none' })
							break
						case 'service':
							uni.showToast({ title: '客服中心', icon: 'none' })
							break
						case 'settings':
							uni.showToast({ title: '设置', icon: 'none' })
							break
						default:
							break
					}
				}, {
					message: '此功能需要先登录',
					returnUrl: '/pages/user/index'
				})
			},
			
			handleLogout() {
				uni.showModal({
					title: '提示',
					content: '确定要退出登录吗？',
					success: (res) => {
						if (res.confirm) {
							clearUserData()
							this.checkLoginStatus()
							uni.showToast({
								title: '已退出登录',
								icon: 'success'
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
.container {
	min-height: 100vh;
	background: #f5f5f5;
}

.header {
	background: linear-gradient(135deg, #FF6600 0%, #FF8533 100%);
	padding: 60rpx 30rpx 40rpx;
	color: white;
	
	.user-info, .login-prompt {
		display: flex;
		align-items: center;
		
		.avatar {
			width: 120rpx;
			height: 120rpx;
			border-radius: 60rpx;
			background: rgba(255, 255, 255, 0.3);
			display: flex;
			align-items: center;
			justify-content: center;
			margin-right: 30rpx;
			
			.avatar-text {
				font-size: 48rpx;
				font-weight: bold;
				color: white;
			}
		}
		
		.user-details {
			flex: 1;
			
			.nickname {
				display: block;
				font-size: 36rpx;
				font-weight: bold;
				margin-bottom: 10rpx;
			}
			
			.phone {
				display: block;
				font-size: 28rpx;
				opacity: 0.8;
			}
			
			.login-btn {
				background: rgba(255, 255, 255, 0.2);
				color: white;
				border: 2rpx solid rgba(255, 255, 255, 0.5);
				border-radius: 30rpx;
				padding: 15rpx 30rpx;
				font-size: 28rpx;
				margin-top: 10rpx;
				
				&:active {
					background: rgba(255, 255, 255, 0.3);
				}
			}
		}
	}
}

.menu-section {
	padding: 20rpx;
	
	.menu-group {
		background: white;
		border-radius: 20rpx;
		margin-bottom: 20rpx;
		overflow: hidden;
		
		.menu-item {
			display: flex;
			align-items: center;
			padding: 30rpx;
			border-bottom: 1rpx solid #f0f0f0;
			
			&:last-child {
				border-bottom: none;
			}
			
			&:active {
				background: #f8f8f8;
			}
			
			.menu-icon {
				font-size: 40rpx;
				margin-right: 30rpx;
			}
			
			.menu-text {
				flex: 1;
				font-size: 32rpx;
				color: #333;
			}
			
			.menu-arrow {
				font-size: 32rpx;
				color: #ccc;
			}
		}
	}
}

.logout-section {
	padding: 40rpx 20rpx;
	
	.logout-btn {
		width: 100%;
		background: #fff;
		color: #ff4757;
		border: 2rpx solid #ff4757;
		border-radius: 40rpx;
		padding: 30rpx;
		font-size: 32rpx;
		
		&:active {
			background: #ff4757;
			color: white;
		}
	}
}
</style>
