<template>
	<view class="container">
		<!-- 用户资料头部 -->
		<view class="user-header">
			<view class="user-info">
				<image class="avatar" :src="displayAvatar" mode="aspectFill"></image>
				<view class="user-details">
					<text class="username">{{ userInfo ? userInfo.nickname || userInfo.username : '未登录' }}</text>
					<text class="edit-profile" v-if="userInfo" @click="editProfile">编辑个人资料</text>
					<text class="edit-profile" v-else @click="goToLogin">去登录</text>
				</view>
			</view>
			<view class="settings-btn" @click="goToSettings">
				<text class="settings-icon">⚙️</text>
			</view>
		</view>
		
		<!-- VIP会员横幅 -->
		<view class="vip-banner" @click="goToVip">
			<text class="vip-text">开通VIP无限畅读好书</text>
			<button class="vip-btn">去开通</button>
		</view>
		
		<scroll-view class="scroll-content" scroll-y>
			<!-- 菜单列表 -->
			<view class="menu-section" v-if="userInfo">
				<view class="menu-item" v-for="(item, index) in menuItems" :key="index" @click="handleMenuClick(item)">
					<view class="menu-icon-wrapper">
						<text class="menu-icon">{{ item.icon }}</text>
					</view>
					<text class="menu-text">{{ item.name }}</text>
					<text class="menu-arrow">></text>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import { getUserProfile } from '../../utils/api.js'
	
	const defaultAvatar = 'https://picsum.photos/seed/avatar/200/200'
	
	export default {
		data() {
			return {
				userInfo: null,
				menuItems: [
					{ 
						name: '消息', 
						icon: '💬',
						action: 'messages'
					},
					{ 
						name: '笔记', 
						icon: '📝',
						action: 'notes'
					},
					{ 
						name: '阅读排行', 
						icon: '🏆',
						action: 'readingRank'
					},
					{ 
						name: '浏览记录', 
						icon: '🕐',
						action: 'browsingHistory'
					},
					{ 
						name: '开通VIP记录', 
						icon: '💰',
						action: 'vipRecords'
					},
					{ 
						name: '意见反馈', 
						icon: '❓',
						action: 'feedback'
					}
				]
			}
		},
		computed: {
			displayAvatar() {
				if (this.userInfo && this.userInfo.avatar) {
					return this.userInfo.avatar
				}
				return defaultAvatar
			}
		},
		onLoad() {
			this.loadUserInfo()
		},
		onShow() {
			this.loadUserInfo()
		},
		methods: {
			loadUserInfo() {
				try {
					const storedUser = uni.getStorageSync('userInfo')
					const isLogin = uni.getStorageSync('isLogin')
					if (!storedUser || !storedUser.id || !isLogin) {
						this.userInfo = null
						return
					}
					this.userInfo = storedUser
					this.fetchRemoteUser(storedUser.id)
				} catch (e) {
					console.error('读取用户信息失败', e)
					this.userInfo = null
				}
			},
			async fetchRemoteUser(userId) {
				try {
					const res = await getUserProfile(userId)
					if (res && res.code === 200 && res.data) {
						this.userInfo = {
							...this.userInfo,
							...res.data
						}
						uni.setStorageSync('userInfo', this.userInfo)
					}
				} catch (e) {
					console.error('刷新用户信息失败', e)
				}
			},
			editProfile() {
				uni.navigateTo({
					url: '/pages/edit-profile/edit-profile'
				})
			},
			goToLogin() {
				uni.navigateTo({
					url: '/pages/login/login'
				})
			},
			goToSettings() {
				uni.navigateTo({
					url: '/pages/settings/settings'
				})
			},
			goToVip() {
				uni.navigateTo({
					url: '/pages/vip/vip'
				})
			},
			handleMenuClick(item) {
				switch(item.action) {
					case 'messages':
						uni.navigateTo({
							url: '/pages/messages/messages'
						})
						break
					case 'notes':
						uni.navigateTo({
							url: '/pages/notes/notes'
						})
						break
					case 'readingRank':
						uni.navigateTo({
							url: '/pages/reading-rank/reading-rank'
						})
						break
					case 'browsingHistory':
						uni.navigateTo({
							url: '/pages/browsing-history/browsing-history'
						})
						break
					case 'vipRecords':
						uni.navigateTo({
							url: '/pages/vip-records/vip-records'
						})
						break
					case 'feedback':
						uni.navigateTo({
							url: '/pages/feedback/feedback'
						})
						break
				}
			}
		}
	}
</script>

<style scoped>
	.container {
		width: 100%;
		height: 100vh;
		background-color: #FFFFFF;
		display: flex;
		flex-direction: column;
		padding-top: 80px;
		box-sizing: border-box;
	}
	
	.user-header {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 40rpx 30rpx;
		background-color: #FFFFFF;
		border-bottom: 1rpx solid #F0F0F0;
	}
	
	.user-info {
		display: flex;
		align-items: center;
		flex: 1;
	}
	
	.avatar {
		width: 120rpx;
		height: 120rpx;
		border-radius: 50%;
		margin-right: 30rpx;
		background-color: #F5F5F5;
		border: 2rpx solid #E0E0E0;
	}
	
	.user-details {
		display: flex;
		flex-direction: column;
	}
	
	.username {
		font-size: 40rpx;
		font-weight: bold;
		color: #333333;
		margin-bottom: 15rpx;
	}
	
	.edit-profile {
		font-size: 26rpx;
		color: #999999;
	}
	
	.settings-btn {
		width: 60rpx;
		height: 60rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	
	.settings-icon {
		font-size: 40rpx;
		color: #333333;
	}
	
	.vip-banner {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin: 30rpx;
		padding: 30rpx;
		background: linear-gradient(135deg, #FFE5CC 0%, #FFF0E0 100%);
		border-radius: 16rpx;
		box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.05);
	}
	
	.vip-text {
		font-size: 28rpx;
		color: #666666;
		flex: 1;
	}
	
	.vip-btn {
		width: 140rpx;
		height: 60rpx;
		background-color: #666666;
		color: #FFFFFF;
		font-size: 26rpx;
		border: none;
		border-radius: 30rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-shrink: 0;
	}
	
	.vip-btn::after {
		border: none;
	}
	
	.scroll-content {
		flex: 1;
		width: 100%;
	}
	
	.menu-section {
		background-color: #FFFFFF;
		padding: 0 30rpx;
	}
	
	.menu-item {
		display: flex;
		align-items: center;
		padding: 35rpx 0;
		border-bottom: 1rpx solid #F0F0F0;
	}
	
	.menu-item:last-child {
		border-bottom: none;
	}
	
	.menu-icon-wrapper {
		width: 60rpx;
		height: 60rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-right: 30rpx;
	}
	
	.menu-icon {
		font-size: 40rpx;
	}
	
	.menu-text {
		flex: 1;
		font-size: 32rpx;
		color: #333333;
	}
	
	.menu-arrow {
		font-size: 32rpx;
		color: #CCCCCC;
	}
</style>

