<?php
/**
 * 仪表盘模块 - 新增服务器信息区和版本信息
 */
$db = getDB();

// 统计数据
$categoryCount = $db->query("SELECT COUNT(*) as cnt FROM function_categories")->fetch()['cnt'];
$functionCount = $db->query("SELECT COUNT(*) as cnt FROM functions")->fetch()['cnt'];
$userCount = $db->query("SELECT COUNT(*) as cnt FROM users")->fetch()['cnt'];
$popupCount = $db->query("SELECT COUNT(*) as cnt FROM popups")->fetch()['cnt'];
$wechatUserCount = $db->query("SELECT COUNT(*) as cnt FROM users WHERE wechat_openid != '' AND wechat_openid IS NOT NULL")->fetch()['cnt'];

// 最近注册的用户
$recentUsers = $db->query("SELECT id, username, real_name, wechat_nickname, is_banned, created_at FROM users ORDER BY id DESC LIMIT 5")->fetchAll();

// 活跃弹窗
$activePopups = $db->query("SELECT id, title, is_active FROM popups WHERE is_active = 1 ORDER BY sort_order")->fetchAll();

// 服务器信息
$serverInfo = [
    'php_version' => phpversion(),
    'mysql_version' => '',
    'server_software' => $_SERVER['SERVER_SOFTWARE'] ?? '-',
    'php_uname' => php_uname('s') . ' ' . php_uname('r'),
    'max_upload' => ini_get('upload_max_filesize'),
    'max_post' => ini_get('post_max_size'),
    'memory_limit' => ini_get('memory_limit'),
    'disk_free' => '',
    'db_size' => '',
];

try {
    $mysqlVer = $db->query("SELECT VERSION() as ver")->fetch()['ver'];
    $serverInfo['mysql_version'] = $mysqlVer;
} catch (Exception $e) {
    $serverInfo['mysql_version'] = '未知';
}

// 数据库大小
try {
    $dbName = $db->query("SELECT DATABASE() as db")->fetch()['db'];
    $dbSize = $db->query("SELECT ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS size_mb FROM information_schema.TABLES WHERE table_schema = '" . addslashes($dbName) . "'")->fetch()['size_mb'];
    $serverInfo['db_size'] = $dbSize . ' MB';
} catch (Exception $e) {
    $serverInfo['db_size'] = '未知';
}

// 磁盘剩余空间
$diskFree = @disk_free_space(__DIR__);
if ($diskFree !== false) {
    $serverInfo['disk_free'] = round($diskFree / 1024 / 1024 / 1024, 2) . ' GB';
} else {
    $serverInfo['disk_free'] = '未知';
}
?>

<!-- 统计卡片 -->
<div class="dashboard-stats">
    <div class="stat-card">
        <div class="stat-icon blue"><i class="fas fa-users"></i></div>
        <div class="stat-num"><?php echo $userCount; ?></div>
        <div class="stat-label">注册用户</div>
    </div>
    <div class="stat-card">
        <div class="stat-icon green"><i class="fas fa-th-large"></i></div>
        <div class="stat-num"><?php echo $functionCount; ?></div>
        <div class="stat-label">菜单功能</div>
    </div>
    <div class="stat-card">
        <div class="stat-icon orange"><i class="fas fa-layer-group"></i></div>
        <div class="stat-num"><?php echo $categoryCount; ?></div>
        <div class="stat-label">功能分类</div>
    </div>
    <div class="stat-card">
        <div class="stat-icon purple"><i class="fas fa-comment-dots"></i></div>
        <div class="stat-num"><?php echo $popupCount; ?></div>
        <div class="stat-label">弹窗数量</div>
    </div>
</div>

<!-- 服务器信息 + 官网/二维码区域 -->
<div class="dashboard-bottom">
    <!-- 服务器信息 -->
    <div class="panel-card">
        <div class="panel-title"><i class="fas fa-server"></i> 服务器信息</div>
        <div style="display:flex;align-items:center;justify-content:space-between;margin-bottom:16px;background:linear-gradient(135deg,#1f2937,#374151);border-radius:10px;padding:16px 20px;color:white;">
            <div>
                <div style="font-size:14px;color:#9ca3af;">当前版本</div>
                <div style="font-size:20px;font-weight:700;margin-top:4px;">校园助手v1公益版</div>
            </div>
            <div style="text-align:right;">
                <div style="font-size:11px;color:#9ca3af;">一课</div>
                <div style="font-size:14px;color:#60a5fa;margin-top:2px;">Free Edition</div>
            </div>
        </div>
        <div style="display:grid; grid-template-columns:1fr 1fr; gap:10px;">
            <div style="background:#f9fafb; padding:10px 14px; border-radius:8px;">
                <div style="font-size:12px; color:#9ca3af;">PHP版本</div>
                <div style="font-size:14px; font-weight:600; color:#1f2937; margin-top:2px;"><?php echo $serverInfo['php_version']; ?></div>
            </div>
            <div style="background:#f9fafb; padding:10px 14px; border-radius:8px;">
                <div style="font-size:12px; color:#9ca3af;">MySQL版本</div>
                <div style="font-size:14px; font-weight:600; color:#1f2937; margin-top:2px;"><?php echo $serverInfo['mysql_version']; ?></div>
            </div>
            <div style="background:#f9fafb; padding:10px 14px; border-radius:8px;">
                <div style="font-size:12px; color:#9ca3af;">操作系统</div>
                <div style="font-size:14px; font-weight:600; color:#1f2937; margin-top:2px;"><?php echo $serverInfo['php_uname']; ?></div>
            </div>
            <div style="background:#f9fafb; padding:10px 14px; border-radius:8px;">
                <div style="font-size:12px; color:#9ca3af;">Web服务器</div>
                <div style="font-size:14px; font-weight:600; color:#1f2937; margin-top:2px;"><?php echo $serverInfo['server_software']; ?></div>
            </div>
            <div style="background:#f9fafb; padding:10px 14px; border-radius:8px;">
                <div style="font-size:12px; color:#9ca3af;">数据库大小</div>
                <div style="font-size:14px; font-weight:600; color:#1f2937; margin-top:2px;"><?php echo $serverInfo['db_size']; ?></div>
            </div>
            <div style="background:#f9fafb; padding:10px 14px; border-radius:8px;">
                <div style="font-size:12px; color:#9ca3af;">磁盘剩余</div>
                <div style="font-size:14px; font-weight:600; color:#1f2937; margin-top:2px;"><?php echo $serverInfo['disk_free']; ?></div>
            </div>
            <div style="background:#f9fafb; padding:10px 14px; border-radius:8px;">
                <div style="font-size:12px; color:#9ca3af;">上传限制</div>
                <div style="font-size:14px; font-weight:600; color:#1f2937; margin-top:2px;"><?php echo $serverInfo['max_upload']; ?></div>
            </div>
            <div style="background:#f9fafb; padding:10px 14px; border-radius:8px;">
                <div style="font-size:12px; color:#9ca3af;">内存限制</div>
                <div style="font-size:14px; font-weight:600; color:#1f2937; margin-top:2px;"><?php echo $serverInfo['memory_limit']; ?></div>
            </div>
        </div>
    </div>

    <!-- 官网入口 + 交流群二维码 -->
    <div class="panel-card">
        <div class="panel-title"><i class="fas fa-link"></i> 官网与交流</div>
        <div style="background:#f9fafb; border-radius:10px; padding:20px; margin-bottom:12px;">
            <div style="font-size:14px; font-weight:600; color:#1f2937; margin-bottom:8px;">产品入口</div>
            <div style="font-size:13px; color:#6b7280; margin-bottom:12px;">访问各产品官网获取最新资讯与更新信息</div>
            <div style="display:flex;flex-wrap:wrap;gap:10px;">
                <a href="https://yike.sx-wl.cn" target="_blank" style="display:inline-flex;align-items:center;gap:8px;padding:10px 20px;background:#374151;color:white;border-radius:8px;font-size:14px;text-decoration:none;transition:background 0.15s;">
                    <i class="fas fa-external-link-alt"></i> 一课官网
                </a>
                <a href="https://saas.sx-wl.cn" target="_blank" style="display:inline-flex;align-items:center;gap:8px;padding:10px 20px;background:#4F6EF7;color:white;border-radius:8px;font-size:14px;text-decoration:none;transition:background 0.15s;">
                    <i class="fas fa-cloud"></i> 易网SaaS
                </a>
                <a href="https://yksns.sx-wl.cn" target="_blank" style="display:inline-flex;align-items:center;gap:8px;padding:10px 20px;background:#8B5CF6;color:white;border-radius:8px;font-size:14px;text-decoration:none;transition:background 0.15s;">
                    <i class="fas fa-users"></i> 一刻SNS
                </a>
            </div>
        </div>
        <div style="background:#f9fafb; border-radius:10px; padding:20px;">
            <div style="font-size:14px; font-weight:600; color:#1f2937; margin-bottom:8px;">开发者交流群</div>
            <div style="font-size:13px; color:#6b7280; margin-bottom:12px;">扫码添加开发人员，参与交流与后续更新</div>
            <div style="text-align:center;">
                <div style="width:180px;height:180px;margin:0 auto;background:white;border-radius:12px;border:1px solid #e5e7eb;overflow:hidden;display:flex;align-items:center;justify-content:center;">
                    <img src="<?php echo SITE_URL; ?>/uploads/group_qrcode.png" style="max-width:100%;max-height:100%;object-fit:contain;" onerror="this.parentElement.innerHTML='<div style=\'color:#9ca3af;font-size:14px;padding:20px;\'>请上传群二维码<br><small>到 /uploads/group_qrcode.png</small></div>'">
                </div>
                <div style="font-size:12px;color:#9ca3af;margin-top:8px;">微信扫码进群交流</div>
            </div>
        </div>
    </div>
</div>

<!-- 数据概览 -->
<div class="panel-card" style="margin-top:16px;">
    <div class="panel-title"><i class="fas fa-chart-bar"></i> 数据概览</div>
    <div style="display:grid; grid-template-columns:1fr 1fr 1fr 1fr; gap:12px;">
        <div style="background:#f9fafb; padding:12px 16px; border-radius:8px;">
            <div style="font-size:20px; font-weight:600; color:#1f2937;"><?php echo $wechatUserCount; ?></div>
            <div style="font-size:13px; color:#9ca3af;">微信用户</div>
        </div>
        <div style="background:#f9fafb; padding:12px 16px; border-radius:8px;">
            <div style="font-size:20px; font-weight:600; color:#1f2937;"><?php echo count($activePopups); ?></div>
            <div style="font-size:13px; color:#9ca3af;">活跃弹窗</div>
        </div>
        <div style="background:#f9fafb; padding:12px 16px; border-radius:8px;">
            <div style="font-size:20px; font-weight:600; color:#1f2937;">
                <?php
                $bannedCount = $db->query("SELECT COUNT(*) as cnt FROM users WHERE is_banned = 1")->fetch()['cnt'];
                echo $bannedCount;
                ?>
            </div>
            <div style="font-size:13px; color:#9ca3af;">封禁用户</div>
        </div>
    </div>
</div>

<!-- 快捷入口 -->
<div class="panel-card" style="margin-top:16px;">
    <div class="panel-title"><i class="fas fa-bolt"></i> 快捷入口</div>
    <div class="quick-grid">
        <div class="quick-item" onclick="navTo('categories')">
            <i class="fas fa-layer-group"></i>
            <div class="quick-name">分类管理</div>
        </div>
        <div class="quick-item" onclick="navTo('functions')">
            <i class="fas fa-th-large"></i>
            <div class="quick-name">菜单管理</div>
        </div>
        <div class="quick-item" onclick="navTo('popups')">
            <i class="fas fa-comment-dots"></i>
            <div class="quick-name">弹窗管理</div>
        </div>
        <div class="quick-item" onclick="navTo('site')">
            <i class="fas fa-globe"></i>
            <div class="quick-name">站点配置</div>
        </div>
        <div class="quick-item" onclick="navTo('system')">
            <i class="fas fa-cogs"></i>
            <div class="quick-name">系统配置</div>
        </div>
        <div class="quick-item" onclick="navTo('users')">
            <i class="fas fa-users"></i>
            <div class="quick-name">用户管理</div>
        </div>
        <div class="quick-item" onclick="window.open('<?php echo SITE_URL; ?>')">
            <i class="fas fa-external-link-alt"></i>
            <div class="quick-name">访问前台</div>
        </div>
        <div class="quick-item" onclick="location.href='login.php'">
            <i class="fas fa-sign-out-alt"></i>
            <div class="quick-name">退出登录</div>
        </div>
    </div>
</div>

<!-- 最近用户 -->
<div class="panel-card" style="margin-top:16px;">
    <div class="panel-title"><i class="fas fa-user-clock"></i> 最近注册用户</div>
    <?php if (empty($recentUsers)): ?>
    <div class="empty-state"><p>暂无用户数据</p></div>
    <?php else: ?>
    <table class="data-table">
        <thead><tr><th>ID</th><th>用户名</th><th>姓名</th><th>状态</th><th>注册时间</th></tr></thead>
        <tbody>
        <?php foreach ($recentUsers as $u): ?>
        <tr>
            <td><?php echo $u['id']; ?></td>
            <td><?php echo htmlspecialchars($u['wechat_nickname'] ?: $u['username']); ?></td>
            <td><?php echo htmlspecialchars($u['real_name'] ?: '-'); ?></td>
            <td>
                <?php if ($u['is_banned']): ?>
                <span class="status-tag banned">已封禁</span>
                <?php else: ?>
                <span class="status-tag active">正常</span>
                <?php endif; ?>
            </td>
            <td><?php echo substr($u['created_at'], 0, 16); ?></td>
        </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
    <?php endif; ?>
</div>