<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="#909399" :size="16" />
				<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-label">{{ item.label }}</text>
					<text class="stat-value">{{ item.value }}</text>
					<view class="stat-trend" :class="item.up ? 'up' : 'down'">
						<FaIcon :name="item.up ? 'arrow-trend-up' : 'arrow-trend-down'" :color="item.up ? '#3c9cff' : '#909399'" :size="12" />
						<text>{{ item.trend }}</text>
					</view>
				</view>
			</view>

			<view class="section">
				<view class="section-header">
					<text class="section-title">快捷入口</text>
				</view>
				<view class="quick-card">
					<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="20" />
						</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-icon">
							<FaIcon name="circle" color="#3c9cff" :size="8" />
						</view>
						<view class="activity-body">
							<text class="activity-title">{{ item.title }}</text>
							<text class="activity-time">{{ item.time }}</text>
						</view>
						<FaIcon name="chevron-right" color="#c0c4cc" :size="12" />
					</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>
@import '@/src/styles/page-common.scss';

.page {
	@include page-shell;
}

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

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

.greeting {
	display: block;
	font-size: 26rpx;
	color: $color-text-muted;
}

.username {
	display: block;
	font-size: 40rpx;
	font-weight: 600;
	color: $color-text;
	margin-top: 4rpx;
}

.avatar {
	width: 88rpx;
	height: 88rpx;
	border-radius: 50%;
	background: $color-primary-bg;
	display: flex;
	align-items: center;
	justify-content: center;
}

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

.search-bar {
	display: flex;
	align-items: center;
	gap: 16rpx;
	background: $color-bg-page;
	border-radius: $radius-md;
	padding: 20rpx 24rpx;
}

.search-text {
	font-size: 28rpx;
	color: $color-text-muted;
}

.main-scroll {
	@include scroll-body;
}

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

.stat-card {
	@include card;
	padding: 28rpx 24rpx;
}

.stat-label {
	display: block;
	font-size: 24rpx;
	color: $color-text-muted;
}

.stat-value {
	display: block;
	font-size: 40rpx;
	font-weight: 600;
	color: $color-text;
	margin-top: 8rpx;
}

.stat-trend {
	display: inline-flex;
	align-items: center;
	gap: 6rpx;
	margin-top: 16rpx;
	padding: 4rpx 12rpx;
	border-radius: $radius-sm;
	font-size: 22rpx;

	&.up {
		background: $color-primary-bg;
		color: $color-primary;
	}

	&.down {
		background: $color-divider;
		color: $color-text-muted;
	}
}

.section {
	margin-bottom: 32rpx;
}

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

.section-title {
	@include section-title;
}

.section-more {
	font-size: 26rpx;
	color: $color-primary;
}

.quick-card {
	@include card;
	display: grid;
	grid-template-columns: repeat(4, 1fr);
	padding: 32rpx 16rpx;
}

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

	&:active {
		opacity: 0.7;
	}
}

.quick-icon {
	@include icon-box(88rpx);
	border-radius: $radius-lg;
}

.quick-name {
	font-size: 24rpx;
	color: $color-text-secondary;
}

.activity-card {
	@include card;
	overflow: hidden;
}

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

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

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

.activity-icon {
	width: 32rpx;
	display: flex;
	justify-content: center;
}

.activity-body {
	flex: 1;
	min-width: 0;
}

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

.activity-time {
	display: block;
	font-size: 24rpx;
	color: $color-text-muted;
	margin-top: 6rpx;
}

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