<?php
// 只引入必要的函数和常量
require_once __DIR__ . '/inc_functions.php';  // 创建新文件存放共享函数

// 设置错误处理
error_reporting(0);
ini_set('display_errors', 0);
ini_set('log_errors', 1);
ini_set('error_log', __DIR__ . '/error.log');

header('Content-Type: text/html; charset=UTF-8');

// 加载并排序数据
$scores = loadScores();
if (isset($scores['users'])) {
    uasort($scores['users'], function($a, $b) {
        return $b['score'] - $a['score'];
    });
}
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>福袋积分排行榜</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/lxgw-wenkai-screen-webfont/1.7.0/lxgwwenkaiscreen.css" rel="stylesheet">
    <style>
        * { box-sizing: border-box; }
        body {
            margin: 0;
            padding: 15px;
            font-family: LXGW WenKai Screen, serif;
        }
        .ranking-container {
            max-width: 800px;
            margin: 0 auto;
        }
        .ranking-list {
            list-style: none;
            padding: 0;
        }
        .ranking-item {
            display: flex;
            align-items: center;
            padding: 10px;
            margin: 5px 0;
            background: #f5f5f5;
            border-radius: 5px;
        }
        .rank {
            width: 40px;
            font-weight: bold;
        }
        .username {
            flex: 1;
        }
        .score {
            width: 80px;
            text-align: right;
        }
        @media (max-width: 600px) {
            .ranking-item {
                font-size: 14px;
            }
            .rank {
                width: 30px;
            }
            .score {
                width: 60px;
            }
        }
    </style>
</head>
<body>
    <div class="ranking-container">
        <h1>福袋积分排行榜</h1>
        <ul class="ranking-list">
        <?php if (isset($scores['users']) && !empty($scores['users'])): ?>
            <?php $rank = 1; ?>
            <?php foreach ($scores['users'] as $user): ?>
                <li class="ranking-item">
                    <span class="rank"><?php echo $rank++; ?></span>
                    <span class="username"><?php echo e($user['username']); ?></span>
                    <span class="score"><?php echo (int)$user['score']; ?>分</span>
                </li>
            <?php endforeach; ?>
        <?php else: ?>
            <li>暂无排名数据</li>
        <?php endif; ?>
        </ul>
    </div>
    <script>
        // 每60秒自动刷新页面
        setTimeout(() => location.reload(), 60000);
    </script>
</body>
</html>
