<?php
if (session_status() !== PHP_SESSION_ACTIVE && !headers_sent()) {
    session_start();
}

require_once __DIR__ . '/config/config.php';
require_once __DIR__ . '/includes/Database.php';
require_once __DIR__ . '/includes/UserCenter.php';
require_once __DIR__ . '/includes/PackageQuota.php';

// 校验登录态（严格：必须有 user_id）
$userId = isset($_SESSION['user_id']) ? (int)$_SESSION['user_id'] : 0;
if ($userId <= 0) {
    http_response_code(403);
    echo '<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><title>权限不足</title></head><body>';
    echo '<div style="max-width:800px;margin:80px auto;font-size:16px;line-height:1.8;">错误：未检测到登录用户，请先登录后访问个人中心。</div>';
    echo '</body></html>';
    exit;
}

$db = Database::getInstance();
$pdo = $db->pdo();

if (function_exists('isMobile')) {
    $ua = strtolower($_SERVER['HTTP_USER_AGENT'] ?? '');
    $isWx = strpos($ua, 'micromessenger') !== false;
    if ($isWx || isMobile()) {
        header('Location: /m/uc/index.php');
        exit;
    }
}

// 用户信息与指标
$profile = UserCenter::getProfile($db, $userId);
$metrics = UserCenter::computeMetrics($db, $userId);
$pendingList = UserCenter::pendingArticles($db, $userId, 10);

// 中文注释：信息脱敏显示（UI效果与截图一致）
$maskPhone = function(string $m): string {
    $s = preg_replace('/\s+/', '', $m);
    if (strlen($s) >= 11) { return substr($s, 0, 3) . ' **** ' . substr($s, -4); }
    return $m;
};
$maskQQ = function(string $q): string {
    return (strlen($q) >= 6) ? substr($q, 0, 4) . '****' . substr($q, -2) : $q;
};

?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <title>个人中心</title>
    <link href="/fontawesome/css/all.min.css" rel="stylesheet" />
    <link rel="stylesheet" href="/css/main.css" />
    <link rel="stylesheet" href="/css/header.css" />
    <link rel="stylesheet" href="/css/footer.css" />
    <script src="/libs/jquery-3.7.1.min.js"></script>
    <script src="/js/header.js"></script>
</head>
<body>
<?php require_once __DIR__ . '/includes/site-header.php'; ?>
<main class="user-center">
    <div class="container uc-layout">
        <!-- 左侧导航：统一文件引用 -->
        <?php require_once __DIR__ . '/includes/uc-nav.php'; ?>

        <!-- 右侧内容 -->
        <section class="uc-content">
            <h2 class="uc-title">个人信息</h2>
            <p style="color:#666; margin-top:-6px;">完善资料能帮助顾问更快提供精准服务，并提升账号安全等级。</p>

            <!-- 账户信息卡片 -->
            <div class="uc-card" style="margin-top:12px;">
                <h3 class="uc-card-title">账户信息</h3>
                <div class="uc-grid"> 
                    <div class="uc-field">
                        <span class="uc-label">昵称</span>
                        <span class="uc-value"><?php echo htmlspecialchars(((string)($profile['nickname'] ?? '') !== '' ? (string)$profile['nickname'] : (string)$profile['username']), ENT_QUOTES); ?></span>
                    </div>
                    <div class="uc-field">
                        <span class="uc-label">手机号</span>
                        <span class="uc-value"><?php echo htmlspecialchars($maskPhone((string)($profile['mobile'] ?? '')), ENT_QUOTES); ?></span>
                    </div>
                    <div class="uc-field">
                        <span class="uc-label">微信号</span>
                        <span class="uc-value"><?php echo htmlspecialchars((string)($profile['wechat'] ?? ''), ENT_QUOTES); ?></span>
                    </div>
                    <div class="uc-field">
                        <span class="uc-label">QQ号</span>
                        <span class="uc-value"><?php echo htmlspecialchars($maskQQ((string)($profile['qq'] ?? '')), ENT_QUOTES); ?></span>
                    </div>
                </div>
                <div class="uc-actions" style="margin-top:12px;">
                    <a class="btn-secondary" href="/user-profile-edit.php">修改信息</a>
                </div>
            </div>
            <!-- 账户余额和充值 -->
            <div class="uc-card" style="margin-top:12px;">
                <h3 class="uc-card-title">账户余额信息</h3>
    <?php
                require_once __DIR__ . '/includes/Wallet.php';
                Wallet::ensureTables($pdo);
                $balance = Wallet::getBalance($pdo, $userId);
                ?>
                <div class="uc-grid"> 
                    <div class="uc-field">
                        <span class="uc-label">账户余额</span>
                        <span class="uc-value">￥<?php echo number_format($balance, 2, '.', ''); ?></span>
                    </div>
                </div>
                <div class="uc-actions" style="margin-top:12px; display:flex; gap:10px;">
                    <a class="btn-secondary" href="/recharge.php">去充值</a>                    
                <a class="secondary" href="/wallet.php">钱包流水</a>
                </div>
            </div>

            <!-- 账号数据卡片 -->
            <div class="uc-card" style="margin-top:16px;">
                <h3 class="uc-card-title">账号数据</h3>
                <div class="uc-cards">
                    <div class="uc-card">
                        <div class="uc-label">累计文章发布</div>
                        <div class="uc-value" style="font-size:28px; color:#333;">
                            <?php echo (int)$metrics['total_articles']; ?>
                        </div>
                        <div class="uc-label" style="margin-top:4px; color:#999;">待审核 <?php echo count($pendingList); ?> 篇</div>
                    </div>
                    <div class="uc-card">
                        <div class="uc-label">累计发布站点</div>
                        <div class="uc-value" style="font-size:28px; color:#333;">
                            <?php echo (int)$metrics['total_sites']; ?>
                        </div>
                    </div>
                </div>
                <div class="uc-cards" style="margin-top:12px;">
                    <div class="uc-card">
                        <div class="uc-label">本月发布站点</div>
                        <div class="uc-value" style="font-size:28px; color:#333;">
                            <?php echo (int)$metrics['month_sites']; ?>
                        </div>
                    </div>
                    <div class="uc-card">
                        <div class="uc-label">可发布站点</div>
                        <div class="uc-value" style="font-size:28px; color:#333;">
                            <?php echo (int)$metrics['remain_sites']; ?>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 待审核文章 -->
            <div class="uc-card" style="margin-top:16px;">
            <h3 class="uc-card-title">审核状态</h3>
                <?php if (!empty($pendingList)): ?>
                    <?php foreach ($pendingList as $item): ?>
                        <div style="display:grid; grid-template-columns: 1fr auto; gap:12px; align-items:center; padding:10px; border:1px solid #eee; border-radius:8px; margin-bottom:10px;">
                            <div>
                                <div style="font-weight:600; color:#333;">
                                    <?php echo htmlspecialchars((string)$item['title'], ENT_QUOTES); ?>
                                </div>
                                <div style="font-size:12px; color:#666; margin-top:6px;">
                                    提交时间：<?php echo htmlspecialchars((string)($item['created_at'] ?? ''), ENT_QUOTES); ?>
                                    <span style="margin-left:16px;">发布站点：<?php echo (int)($item['site_count'] ?? 0); ?></span>
                                </div>
                            </div>
                            <div>
                                <a class="btn-secondary" href="/user-article-cancel.php?id=<?php echo (int)$item['id']; ?>">取消审核</a>
                            </div>
                        </div>
                    <?php endforeach; ?>
                <?php else: ?>
                    <div style="font-size:13px; color:#666;">暂无待审核广告</div>
                <?php endif; ?>
            </div>

        </section>
    </div>
</main>
<?php require_once __DIR__ . '/includes/site-footer.php'; ?>
</body>
</html>
