<?php
require_once __DIR__ . '/../MPHX/common.php';

if (!is_admin_login()) {
    header('Location: /admin/login.php');
    exit;
}

// 获取所有宝塔服务器
$bts = $DB->query("SELECT * FROM `bt` ORDER BY `id` DESC");
$bt_list = array();
while ($bt = $DB->fetch($bts)) {
    $bt_list[] = $bt;
}

// 获取服务器状态
$server_status = array();

foreach ($bt_list as &$bt) {
    $bt_api = new bt_api();
    $protocol = $bt['aqfw'] == 'true' ? 'https' : 'http';
    $bt_api->BT_PANEL = $protocol . '://' . $bt['ip'] . ':' . $bt['dk'];
    $bt_api->BT_KEY = $bt['key'];
    
    $system_info = $bt_api->GetPanelStatus();
    $system_data = json_decode($system_info, true);
    
    // 单独获取磁盘信息
    $disk_info = $bt_api->GetDiskInfo();
    
    if ($system_data && isset($system_data['version'])) {
        // 获取网络信息
        $network_info = $bt_api->GetNetworkInfo();
        
        $server_status[$bt['id']] = array(
            'status' => 'online',
            'version' => isset($system_data['version']) ? $system_data['version'] : '',
            'cpu' => isset($system_data['cpuRealUsed']) ? $system_data['cpuRealUsed'] : '0',
            'mem_total' => isset($system_data['memTotal']) ? $system_data['memTotal'] : '0',
            'mem_used' => isset($system_data['memRealUsed']) ? $system_data['memRealUsed'] : '0',
            'system' => isset($system_data['system']) ? $system_data['system'] : '',
            'time' => isset($system_data['time']) ? $system_data['time'] : '',
            'disk_info' => $disk_info,
            'network_info' => $network_info
        );
    } else {
        $server_status[$bt['id']] = array(
            'status' => 'offline',
            'version' => '',
            'cpu' => '0',
            'mem_total' => '0',
            'mem_used' => '0',
            'system' => '',
            'time' => '',
            'disk_info' => array(),
            'network_info' => null
        );
    }
}
unset($bt);
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Expires" content="0">
    <title>宝塔监控 - 宝塔主机管理系统</title>
    <link rel="stylesheet" href="/static/css/bootstrap.min.css">
    <link rel="stylesheet" href="/static/css/all.min.css">
    <link rel="stylesheet" href="css/style.css?v=2026040501">
    <script src="/static/js/jquery.min.js"></script>
    <style>
        .bt-monitor { padding: 0; }

        .page-header {
            background: linear-gradient(135deg, #34a853 0%, #0d904f 100%);
            color: #fff;
            padding: 20px;
            border-radius: 8px;
            margin-bottom: 20px;
        }
        .page-header h2 { margin: 0; font-size: 18px; font-weight: 500; }

        .server-table {
            width: 100%;
            min-width: 900px;
            background: #fff;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 1px 3px rgba(0,0,0,0.08);
        }

        .server-table thead {
            background: #f8f9fa;
        }

        .server-table th {
            padding: 14px 16px;
            text-align: center;
            font-size: 13px;
            font-weight: 600;
            color: #5f6368;
            border-bottom: 1px solid #e8eaed;
            white-space: nowrap;
        }

        .server-table th:first-child { 
            text-align: left; 
            width: 50px;
        }
        
        .server-table th:nth-child(2) { 
            text-align: left; 
            min-width: 140px;
        }
        
        .server-table th:nth-child(3) { 
            text-align: left; 
            min-width: 130px;
        }
        
        .server-table th:nth-child(4) { min-width: 90px; }
        .server-table th:nth-child(5) { min-width: 100px; }
        .server-table th:nth-child(6) { min-width: 100px; }
        .server-table th:nth-child(7) { min-width: 100px; }
        .server-table th:nth-child(8) { min-width: 100px; }
        .server-table th:nth-child(9) { min-width: 100px; }
        
        .server-table td { text-align: center; }
        .server-table td:first-child { 
            text-align: left; 
            font-weight: 500;
        }
        
        .server-table td:nth-child(2) { 
            text-align: left; 
            font-weight: 500;
        }
        
        .server-table td:nth-child(3) { 
            text-align: left; 
        }

        .server-table td {
            padding: 16px;
            font-size: 14px;
            color: #202124;
            border-bottom: 1px solid #e8eaed;
            vertical-align: middle;
        }

        .server-table tbody tr:hover {
            background: #f8f9fa;
        }

        .status-badge {
            display: inline-flex;
            align-items: center;
            gap: 5px;
            padding: 4px 10px;
            border-radius: 12px;
            font-size: 11px;
            font-weight: 500;
        }

        .status-online { background: #e6f4ea; color: #137333; }
        .status-offline { background: #fce8e6; color: #c5221f; }

        .status-dot {
            width: 6px;
            height: 6px;
            border-radius: 50%;
            background: currentColor;
        }

        .status-online .status-dot {
            animation: pulse 2s infinite;
        }

        @keyframes pulse {
            0%, 100% { opacity: 1; }
            50% { opacity: 0.5; }
        }

        .data-value {
            font-weight: 600;
            font-size: 13px;
        }

        .data-value.cpu { color: #34a853; }
        .data-value.memory { color: #1a73e8; }
        .data-value.disk { color: #fbbc04; }
        .data-value.up { color: #ea4335; }
        .data-value.down { color: #4285f4; }
        
        .progress-mini {
            width: 50px;
            height: 4px;
            background: #e8eaed;
            border-radius: 2px;
            display: inline-block;
            vertical-align: middle;
            margin-left: 5px;
            overflow: hidden;
        }
        
        .progress-mini-bar {
            height: 100%;
            border-radius: 2px;
        }
        
        .progress-mini-bar.cpu { background: #1a73e8; }
        .progress-mini-bar.memory { background: #34a853; }
        .progress-mini-bar.disk { background: #fbbc04; }
        
        .refresh-btn {
            position: fixed;
            bottom: 30px;
            right: 30px;
            width: 50px;
            height: 50px;
            border-radius: 50%;
            background: #1a73e8;
            color: #fff;
            border: none;
            box-shadow: 0 4px 12px rgba(26, 115, 232, 0.4);
            cursor: pointer;
            font-size: 18px;
            transition: all 0.3s;
        }
        
        .refresh-btn:hover {
            background: #1557b0;
            transform: rotate(180deg);
        }
        
        @media (max-width: 1200px) {
            .server-table th, .server-table td { padding: 12px; }
        }
        
        @media (max-width: 768px) {
            .server-table-container { 
                overflow-x: auto;
                -webkit-overflow-scrolling: touch;
                border-radius: 8px;
            }
            .server-table th, .server-table td { padding: 10px; font-size: 13px; }
        }
    </style>
</head>
<body>
    <div class="content-area bt-monitor">
        <!-- 页面头部 -->
        <div class="page-header">
            <h2><i class="fas fa-server"></i> 宝塔服务器监控</h2>
        </div>
        
        <!-- 服务器列表 -->
        <div class="table-responsive server-table-container">
            <table class="server-table">
                <thead>
                    <tr>
                        <th>D</th>
                        <th>宝塔名字</th>
                        <th>IP地址</th>
                        <th>状态</th>
                        <th>CPU</th>
                        <th>内存</th>
                        <th>磁盘</th>
                        <th>总发送</th>
                        <th>总接收</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($bt_list as $index => $bt): ?>
                    <?php 
                    $status = isset($server_status[$bt['id']]) ? $server_status[$bt['id']] : array('status' => 'offline', 'cpu' => '0', 'mem_total' => '0', 'mem_used' => '0');
                    
                    // 计算内存百分比
                    $mem_percent = 0;
                    if ($status['mem_total'] > 0) {
                        $mem_percent = round(($status['mem_used'] / $status['mem_total']) * 100);
                    }
                    
                    // 获取磁盘使用率
                    $disk_percent = 0;
                    if (!empty($status['disk_info'])) {
                        foreach ($status['disk_info'] as $disk) {
                            if (isset($disk['size']) && isset($disk['size'][3])) {
                                $percent_str = str_replace('%', '', $disk['size'][3]);
                                $disk_percent = intval(floatval($percent_str));
                                break;
                            }
                        }
                    }
                    
                    // 获取总流量
                    // 注意：upTotal/downTotal 单位是字节，需要转换为 GB (除以 1024^3)
                    $total_up_gb = '0';
                    $total_down_gb = '0';
                    if ($status['network_info'] && isset($status['network_info']['upTotal'])) {
                        $total_up_gb = round($status['network_info']['upTotal'] / (1024 * 1024 * 1024), 2);
                        $total_down_gb = round($status['network_info']['downTotal'] / (1024 * 1024 * 1024), 2);
                    }
                    ?>
                    <tr>
                        <td><?php echo $index + 1; ?></td>
                        <td style="text-align: left; font-weight: 500;">
                            <?php echo isset($bt['btmc']) ? htmlspecialchars($bt['btmc']) : '-'; ?>
                        </td>
                        <td style="text-align: left;"><?php echo $bt['ip']; ?></td>
                        <td>
                            <?php if ($status['status'] == 'online'): ?>
                            <span class="status-badge status-online">
                                <span class="status-dot"></span> 在线
                            </span>
                            <?php else: ?>
                            <span class="status-badge status-offline">
                                <span class="status-dot"></span> 离线
                            </span>
                            <?php endif; ?>
                        </td>
                        <td>
                            <span class="data-value cpu"><?php echo $status['cpu']; ?>%</span>
                            <div class="progress-mini" title="<?php echo $status['cpu']; ?>%">
                                <div class="progress-mini-bar cpu" style="width: <?php echo $status['cpu']; ?>%"></div>
                            </div>
                        </td>
                        <td>
                            <span class="data-value memory"><?php echo $mem_percent; ?>%</span>
                            <div class="progress-mini" title="<?php echo $mem_percent; ?>%">
                                <div class="progress-mini-bar memory" style="width: <?php echo $mem_percent; ?>%"></div>
                            </div>
                        </td>
                        <td>
                            <span class="data-value disk"><?php echo $disk_percent; ?>%</span>
                            <div class="progress-mini" title="<?php echo $disk_percent; ?>%">
                                <div class="progress-mini-bar disk" style="width: <?php echo $disk_percent; ?>%"></div>
                            </div>
                        </td>
                        <td>
                            <span class="data-value up"><?php echo $total_up_gb; ?> GB</span>
                        </td>
                        <td>
                            <span class="data-value down"><?php echo $total_down_gb; ?> GB</span>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                    
                    <?php if (empty($bt_list)): ?>
                    <tr>
                        <td colspan="9" style="text-align: center; padding: 30px; color: #5f6368;">
                            <i class="fas fa-info-circle"></i> 暂无服务器配置
                        </td>
                    </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
    
    <!-- 刷新按钮 -->
    <button class="refresh-btn" onclick="window.location.reload()" title="刷新数据">
        <i class="fas fa-sync-alt"></i>
    </button>
    
    <script>
    // 自动刷新（每30秒）
    setTimeout(function() {
        window.location.reload();
    }, 30000);
    </script>
</body>
</html>