<template>
	<view class="page">
		<view class="profile-header">
			<view class="profile-info">
				<view class="profile-avatar">
					<text class="avatar-text">{{ avatarText }}</text>
				</view>
				<view class="profile-detail">
					<text class="profile-name">{{ user?.nickname || '云泽用户' }}</text>
					<text class="profile-id">ID: {{ userId }}</text>
				</view>
				<view class="profile-edit" @tap="onEdit">
					<FaIcon name="pen-to-square" color="#ffffff" :size="16" />
				</view>
			</view>

			<view class="profile-stats">
				<view class="profile-stat" v-for="item in profileStats" :key="item.label">
					<text class="stat-num">{{ item.value }}</text>
					<text class="stat-label">{{ item.label }}</text>
				</view>
			</view>
		</view>

		<view class="main-scroll">
			<view class="menu-group" v-for="(group, gIndex) in menuGroups" :key="gIndex">
				<view
					class="menu-item"
					v-for="item in group"
					:key="item.title"
					@tap="onMenuTap(item)"
				>
					<view class="menu-left">
						<view class="menu-icon">
							<FaIcon :name="item.icon" color="#3c9cff" :size="18" />
						</view>
						<text class="menu-title">{{ item.title }}</text>
					</view>
					<view class="menu-right">
						<text v-if="item.badge" class="menu-badge">{{ item.badge }}</text>
						<FaIcon name="chevron-right" color="#c0c4cc" :size="12" />
					</view>
				</view>
			</view>

			<view class="logout-btn" @tap="handleLogout">
				<text class="logout-text">退出登录</text>
			</view>

			<view class="bottom-space" />
		</view>

		<AppTabbar current="profile" />
	</view>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { getUser, logout, isLoggedIn } from '@/utils/auth.js'
import AppTabbar from '@/components/AppTabbar.vue'

const user = ref(null)

const avatarText = computed(() => {
	const name = user.value?.nickname || '云'
	return name.charAt(0).toUpperCase()
})

const userId = computed(() => {
	return user.value?.phone
		? user.value.phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
		: '10086'
})

const profileStats = ref([
	{ label: '关注', value: '128' },
	{ label: '粉丝', value: '256' },
	{ label: '获赞', value: '1.2k' }
])

const menuGroups = ref([
	[
		{ title: '我的订单', icon: 'clipboard-list' },
		{ title: '我的收藏', icon: 'star' },
		{ title: '浏览历史', icon: 'clock-rotate-left' }
	],
	[
		{ title: '消息通知', icon: 'bell', badge: '3' },
		{ title: '账号安全', icon: 'lock' },
		{ title: '隐私设置', icon: 'eye' }
	],
	[
		{ title: '帮助中心', icon: 'circle-question' },
		{ title: '关于我们', icon: 'circle-info' },
		{ title: '设置', icon: 'gear' }
	]
])

onMounted(() => {
	if (!isLoggedIn()) {
		uni.reLaunch({ url: '/pages/login/login' })
		return
	}
	user.value = getUser()
})

function onEdit() {
	uni.showToast({ title: '编辑资料', icon: 'none' })
}

function onMenuTap(item) {
	uni.showToast({ title: item.title, icon: 'none' })
}

function handleLogout() {
	uni.showModal({
		title: '提示',
		content: '确定要退出登录吗？',
		success(res) {
			if (res.confirm) {
				logout()
				uni.reLaunch({ url: '/pages/login/login' })
			}
		}
	})
}
</script>

<style lang="scss" scoped>
@import '@/src/styles/page-common.scss';

.page {
	@include page-shell;
}

.profile-header {
	flex-shrink: 0;
	padding: calc(var(--status-bar-height, 44px) + 20rpx) $page-padding-x 36rpx;
	background: $color-primary;
}

.profile-info {
	display: flex;
	align-items: center;
	gap: 24rpx;
}

.profile-avatar {
	width: 112rpx;
	height: 112rpx;
	border-radius: 50%;
	background: rgba(255, 255, 255, 0.2);
	border: 4rpx solid rgba(255, 255, 255, 0.35);
	display: flex;
	align-items: center;
	justify-content: center;
	flex-shrink: 0;
}

.avatar-text {
	font-size: 44rpx;
	font-weight: 600;
	color: #ffffff;
}

.profile-detail {
	flex: 1;
	min-width: 0;
}

.profile-name {
	display: block;
	font-size: 36rpx;
	font-weight: 600;
	color: #ffffff;
}

.profile-id {
	display: block;
	font-size: 24rpx;
	color: rgba(255, 255, 255, 0.75);
	margin-top: 6rpx;
}

.profile-edit {
	width: 60rpx;
	height: 60rpx;
	border-radius: 50%;
	background: rgba(255, 255, 255, 0.15);
	display: flex;
	align-items: center;
	justify-content: center;

	&:active {
		background: rgba(255, 255, 255, 0.25);
	}
}

.profile-stats {
	display: flex;
	justify-content: space-around;
	margin-top: 36rpx;
	padding-top: 28rpx;
	border-top: 1rpx solid rgba(255, 255, 255, 0.2);
}

.profile-stat {
	display: flex;
	flex-direction: column;
	align-items: center;
}

.stat-num {
	font-size: 36rpx;
	font-weight: 600;
	color: #ffffff;
}

.stat-label {
	font-size: 24rpx;
	color: rgba(255, 255, 255, 0.75);
	margin-top: 6rpx;
}

.main-scroll {
	@include scroll-body;
	margin-top: -20rpx;
}

.menu-group {
	@include card;
	margin-bottom: 24rpx;
	overflow: hidden;
}

.menu-item {
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding: 28rpx 24rpx;

	& + & {
		border-top: 1rpx solid $color-divider;
	}

	&:active {
		background: $color-bg-page;
	}
}

.menu-left {
	display: flex;
	align-items: center;
	gap: 20rpx;
	flex: 1;
	min-width: 0;
}

.menu-icon {
	@include icon-box(64rpx);
}

.menu-title {
	font-size: 28rpx;
	color: $color-text;
}

.menu-right {
	display: flex;
	align-items: center;
	gap: 12rpx;
	flex-shrink: 0;
}

.menu-badge {
	font-size: 20rpx;
	color: #ffffff;
	background: $color-primary;
	padding: 4rpx 12rpx;
	border-radius: $radius-full;
	min-width: 32rpx;
	text-align: center;
	line-height: 1.4;
}

.logout-btn {
	@include card;
	height: 96rpx;
	display: flex;
	align-items: center;
	justify-content: center;

	&:active {
		background: $color-bg-page;
	}
}

.logout-text {
	font-size: 30rpx;
	color: $color-text-secondary;
}

.bottom-space {
	height: 24rpx;
}
</style>
