<?php
include "includes/header.php";

$message = "";
$db = new SQLite3('a/.eggziedb.db');

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['migrate'])) {
    $old_dns = $_POST['old_dns'];
    $new_dns = $_POST['new_dns'];
    
    if (!empty($old_dns) && !empty($new_dns)) {
        $stmt = $db->prepare("UPDATE ibo SET url = :new WHERE url LIKE :old");
        $stmt->bindValue(':new', $new_dns, SQLITE3_TEXT);
        $stmt->bindValue(':old', '%' . $old_dns . '%', SQLITE3_TEXT);
        $result = $stmt->execute();
        
        $changes = $db->changes();
        $message = "<div class='alert alert-success'>Sucesso! <b>$changes</b> clientes foram migrados para o novo DNS.</div>";
    } else {
        $message = "<div class='alert alert-danger'>Por favor, preencha ambos os campos.</div>";
    }
}

// Obter lista de DNS únicos para sugestão
$dns_list = $db->query("SELECT DISTINCT url FROM ibo");
?>

<div class="container-fluid">
    <div class="d-sm-flex align-items-center justify-content-between mb-4">
        <h1 class="h3 mb-0 text-gray-800">Migração de DNS em Massa</h1>
    </div>

    <?php echo $message; ?>

    <div class="row justify-content-center">
        <div class="col-lg-8">
            <div class="card shadow mb-4">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-primary"><i class="fas fa-exchange-alt mr-2"></i> Migrar Clientes</h6>
                </div>
                <div class="card-body">
                    <form method="post">
                        <div class="form-group">
                            <label class="font-weight-bold">DNS Antigo (ou parte dele):</label>
                            <input type="text" name="old_dns" class="form-control" placeholder="Ex: antigo-dns.com" required>
                            <small class="text-muted">Todos os clientes que contiverem este texto no DNS serão alterados.</small>
                        </div>
                        
                        <div class="form-group mt-4">
                            <label class="font-weight-bold">Novo DNS:</label>
                            <input type="text" name="new_dns" class="form-control" placeholder="Ex: http://novo-dns.com:8080" required>
                        </div>

                        <div class="alert alert-warning mt-4">
                            <i class="fas fa-exclamation-triangle mr-2"></i> <b>Atenção:</b> Esta ação é irreversível e afetará múltiplos clientes simultaneamente.
                        </div>

                        <div class="text-center mt-4">
                            <button type="submit" name="migrate" class="btn btn-primary btn-lg px-5 shadow">
                                <i class="fas fa-rocket mr-2"></i> Iniciar Migração
                            </button>
                        </div>
                    </form>
                </div>
            </div>

            <div class="card shadow mb-4">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-info"><i class="fas fa-info-circle mr-2"></i> DNS Atuais em Uso</h6>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-sm table-hover">
                            <thead>
                                <tr>
                                    <th>DNS</th>
                                    <th class="text-center">Qtd. Clientes</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php while($row = $dns_list->fetchArray()): 
                                    $url = $row['url'];
                                    $count = $db->querySingle("SELECT COUNT(*) FROM ibo WHERE url = '$url'");
                                ?>
                                    <tr>
                                        <td><code><?php echo $url; ?></code></td>
                                        <td class="text-center"><span class="badge badge-primary"><?php echo $count; ?></span></td>
                                    </tr>
                                <?php endwhile; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include "includes/footer.php"; ?>
