<?php
/**
 * 会员中心 - 仪表盘
 */
$memberId = intval($_SESSION['member_id'] ?? 0);
$guestbookTable = $db->table('guestbook');

$contributeCountRow = $db->fetch(
    "SELECT COUNT(*) AS cnt FROM {$db->table('contribute')} WHERE member_id = ?",
    [$memberId]
);
$contributeCount = intval($contributeCountRow['cnt'] ?? 0);

$downloadCountRow = $db->fetch(
    "SELECT COUNT(*) AS cnt FROM {$db->table('download_log')} WHERE member_id = ?",
    [$memberId]
);
$downloadCount = intval($downloadCountRow['cnt'] ?? 0);

$guestbookCountRow = $db->fetch(
    "SELECT COUNT(*) AS cnt FROM {$guestbookTable} WHERE member_id = ?",
    [$memberId]
);
$guestbookCount = intval($guestbookCountRow['cnt'] ?? 0);
cmsEnsureFavoriteTable($db);
cmsEnsureMemberMessageTable($db);
$favoriteCount = intval($db->fetch("SELECT COUNT(*) AS cnt FROM {$db->table('favorite')} WHERE member_id = ?", [$memberId])['cnt'] ?? 0);
$unreadMessageCount = intval($db->fetch("SELECT COUNT(*) AS cnt FROM {$db->table('member_message')} WHERE to_id = ? AND is_read = 0", [$memberId])['cnt'] ?? 0);
?>
<div class="page-header">
    <h1 class="page-title">
        <i class="icon-home"></i>
        欢迎回来，<?php echo htmlspecialchars($member['nickname'] ?: $member['username']); ?>
    </h1>
</div>
<div class="page-content">
    <div class="dashboard-welcome">
        <div class="welcome-icon">🌿</div>
        <div class="welcome-text">
            <p>这里是您的个人中心，管理您的资料、内容和财务信息</p>
        </div>
    </div>
    <div class="dashboard-cards">
        <a href="?page=contribute" class="dash-card">
            <div class="dash-card-icon">📝</div>
            <div class="dash-card-info">
                <div class="dash-card-val"><?php echo $contributeCount; ?></div>
                <div class="dash-card-lbl">我的投稿</div>
            </div>
        </a>
        <a href="?page=guestbook" class="dash-card">
            <div class="dash-card-icon">💬</div>
            <div class="dash-card-info">
                <div class="dash-card-val"><?php echo $guestbookCount; ?></div>
                <div class="dash-card-lbl">我的留言</div>
            </div>
        </a>
        <a href="?page=mydownload" class="dash-card">
            <div class="dash-card-icon">📥</div>
            <div class="dash-card-info">
                <div class="dash-card-val"><?php echo $downloadCount; ?></div>
                <div class="dash-card-lbl">下载记录</div>
            </div>
        </a>
        <a href="?page=finance" class="dash-card">
            <div class="dash-card-icon">💳</div>
            <div class="dash-card-info">
                <div class="dash-card-val">¥<?php echo number_format($member['balance'] ?? 0, 2); ?></div>
                <div class="dash-card-lbl">账户余额</div>
            </div>
        </a>
        <a href="?page=favorites" class="dash-card">
            <div class="dash-card-icon">&#9733;</div>
            <div class="dash-card-info"><div class="dash-card-val"><?php echo $favoriteCount; ?></div><div class="dash-card-lbl">我的收藏</div></div>
        </a>
        <a href="?page=messages" class="dash-card">
            <div class="dash-card-icon">&#9993;</div>
            <div class="dash-card-info"><div class="dash-card-val"><?php echo $unreadMessageCount; ?></div><div class="dash-card-lbl">未读消息</div></div>
        </a>
    </div>
</div>
<style>
.dashboard-welcome {
    background: linear-gradient(135deg, #10b981 0%, #059669 100%);
    border-radius: 12px;
    padding: 32px;
    color: #fff;
    display: flex;
    align-items: center;
    gap: 20px;
    margin-bottom: 28px;
}
.welcome-icon { font-size: 48px; }
.welcome-text p { font-size: 15px; opacity: 0.9; margin-top: 4px; }
.dashboard-cards {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 16px;
}
.dash-card {
    background: #f9fafb;
    border-radius: 10px;
    padding: 20px;
    display: flex;
    align-items: center;
    gap: 16px;
    text-decoration: none;
    transition: all 0.2s ease;
}
.dash-card:hover {
    background: #f0fdf4;
    transform: translateY(-2px);
}
.dash-card-icon { font-size: 32px; }
.dash-card-info { color: inherit; }
.dash-card-val { font-size: 22px; font-weight: 700; color: #1a1a1a; }
.dash-card-lbl { font-size: 13px; color: #888; margin-top: 2px; }
@media (max-width: 768px) {
    .dashboard-cards { grid-template-columns: repeat(2, 1fr); }
}
</style>
