<template>
	<view class="page">
		<view class="header">
			<view class="header-row">
				<view class="user-info">
					<text class="greeting">{{ greeting }}</text>
					<text class="username">{{ user?.nickname || '云泽用户' }}</text>
				</view>
				<view class="avatar">
					<text class="avatar-text">{{ avatarText }}</text>
				</view>
			</view>
			<view class="search-bar">
				<FaIcon name="magnifying-glass" color="#9acafc" :size="18" />
				<text class="search-text">搜索功能、数据...</text>
			</view>
		</view>

		<view class="main-scroll">
			<view class="stats-grid">
				<view class="stat-card" v-for="item in stats" :key="item.label">
					<text class="stat-value">{{ item.value }}</text>
					<text class="stat-label">{{ item.label }}</text>
					<text class="stat-trend" :class="item.up ? 'up' : 'down'">
						{{ item.up ? '↑' : '↓' }} {{ item.trend }}
					</text>
				</view>
			</view>

			<view class="section">
				<text class="section-title">快捷入口</text>
				<view class="quick-grid">
					<view
						class="quick-item"
						v-for="item in quickActions"
						:key="item.name"
						@tap="onQuickTap(item)"
					>
						<view class="quick-icon">
							<FaIcon :name="item.icon" color="#3c9cff" :size="22" />
						</view>
						<text class="quick-name">{{ item.name }}</text>
					</view>
				</view>
			</view>

			<view class="section">
				<view class="section-header">
					<text class="section-title">最近动态</text>
					<text class="section-more">查看全部</text>
				</view>
				<view class="activity-card">
					<view class="activity-item" v-for="(item, index) in activities" :key="index">
						<view class="activity-dot" />
						<view class="activity-body">
							<text class="activity-title">{{ item.title }}</text>
							<text class="activity-time">{{ item.time }}</text>
						</view>
					</view>
				</view>
			</view>

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

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

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

const user = ref(null)

const greeting = computed(() => {
	const hour = new Date().getHours()
	if (hour < 12) return '早上好'
	if (hour < 18) return '下午好'
	return '晚上好'
})

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

const stats = ref([
	{ label: '今日访问', value: '2,847', trend: '12%', up: true },
	{ label: '活跃用户', value: '1,256', trend: '8%', up: true },
	{ label: '转化率', value: '68.5%', trend: '3%', up: false },
	{ label: '总收入', value: '¥8.2k', trend: '15%', up: true }
])

const quickActions = ref([
	{ name: '数据分析', icon: 'chart-line' },
	{ name: '消息中心', icon: 'bell' },
	{ name: '订单管理', icon: 'clipboard-list' },
	{ name: '设置', icon: 'gear' }
])

const activities = ref([
	{ title: '新用户注册 +128', time: '5 分钟前' },
	{ title: '系统更新完成 v2.1', time: '1 小时前' },
	{ title: '订单 #8821 已发货', time: '2 小时前' },
	{ title: '数据备份成功', time: '昨天 23:00' }
])

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

function onQuickTap(item) {
	uni.showToast({ title: item.name, icon: 'none' })
}
</script>

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

.header {
	flex-shrink: 0;
	padding: calc(var(--status-bar-height, 44px) + 24rpx) 40rpx 32rpx;
	background: #ffffff;
}

.header-row {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-bottom: 32rpx;
}

.greeting {
	display: block;
	font-size: 26rpx;
	color: $u-primary-disabled;
}

.username {
	display: block;
	font-size: 40rpx;
	font-weight: 600;
	color: $u-primary;
	margin-top: 6rpx;
}

.avatar {
	width: 80rpx;
	height: 80rpx;
	border-radius: 50%;
	background: #ffffff;
	border: 2rpx solid #e8f0f8;
	display: flex;
	align-items: center;
	justify-content: center;
}

.avatar-text {
	font-size: 32rpx;
	font-weight: 600;
	color: $u-primary;
}

.search-bar {
	display: flex;
	align-items: center;
	gap: 16rpx;
	background: #ffffff;
	border: 2rpx solid #e8f0f8;
	border-radius: 16rpx;
	padding: 22rpx 28rpx;
}

.search-text {
	font-size: 26rpx;
	color: $u-primary-disabled;
}

.main-scroll {
	flex: 1;
	min-height: 0;
	overflow-y: auto;
	-webkit-overflow-scrolling: touch;
	padding: 8rpx 40rpx 0;
}

.stats-grid {
	display: grid;
	grid-template-columns: 1fr 1fr;
	gap: 20rpx;
	margin-bottom: 48rpx;
}

.stat-card {
	background: #ffffff;
	border: 2rpx solid #e8f0f8;
	border-radius: 16rpx;
	padding: 28rpx;
}

.stat-value {
	display: block;
	font-size: 36rpx;
	font-weight: 600;
	color: $u-primary;
}

.stat-label {
	display: block;
	font-size: 24rpx;
	color: $u-primary-disabled;
	margin-top: 6rpx;
}

.stat-trend {
	display: block;
	margin-top: 12rpx;
	font-size: 22rpx;

	&.up {
		color: $u-primary;
	}

	&.down {
		color: $u-primary-disabled;
	}
}

.section {
	margin-bottom: 48rpx;
}

.section-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-bottom: 24rpx;
}

.section-title {
	font-size: 30rpx;
	font-weight: 600;
	color: $u-primary;
}

.section-more {
	font-size: 24rpx;
	color: $u-primary-disabled;
}

.quick-grid {
	display: grid;
	grid-template-columns: repeat(4, 1fr);
	gap: 20rpx;
}

.quick-item {
	display: flex;
	flex-direction: column;
	align-items: center;
	gap: 12rpx;
}

.quick-icon {
	width: 96rpx;
	height: 96rpx;
	border-radius: 24rpx;
	background: #ffffff;
	border: 2rpx solid #e8f0f8;
	display: flex;
	align-items: center;
	justify-content: center;

	&:active {
		border-color: $u-primary-disabled;
	}
}

.quick-name {
	font-size: 22rpx;
	color: $u-primary-disabled;
}

.activity-card {
	background: #ffffff;
	border: 2rpx solid #e8f0f8;
	border-radius: 16rpx;
	overflow: hidden;
}

.activity-item {
	display: flex;
	align-items: center;
	padding: 28rpx;
	gap: 20rpx;

	& + & {
		border-top: 2rpx solid #f0f6fb;
	}
}

.activity-dot {
	width: 12rpx;
	height: 12rpx;
	border-radius: 50%;
	background: $u-primary;
	flex-shrink: 0;
}

.activity-body {
	flex: 1;
}

.activity-title {
	display: block;
	font-size: 28rpx;
	color: $u-primary;
}

.activity-time {
	display: block;
	font-size: 22rpx;
	color: $u-primary-disabled;
	margin-top: 6rpx;
}

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