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

// Conectar ao banco de dados
$db = new SQLite3('a/.eggziedb.db');

// Atualizar DNS
if (isset($_POST['update_dns'])) {
    $selected_dns = $_POST['selected_dns'];
    $new_dns = $_POST['new_dns'];

    if (!empty($selected_dns) && !empty($new_dns)) {
        $stmt = $db->prepare('UPDATE ibo SET url = :new_dns WHERE url = :selected_dns');
        $stmt->bindValue(':new_dns', $new_dns, SQLITE3_TEXT);
        $stmt->bindValue(':selected_dns', $selected_dns, SQLITE3_TEXT);
        $stmt->execute();
        header('Location: DNS.php'); // Redireciona para evitar envio duplo do formulário
        exit;
    }
}

// Buscar DNS cadastradas
$dns_result = $db->query('SELECT DISTINCT url FROM ibo');

include 'includes/header.php';
?>
<main role="main" class="container-fluid pt-4 px-5">
    <div class="row justify-content-center">
        <div class="col-lg-6 col-md-8 col-sm-12">
            <!-- Imagem acima do container -->
            <div class="text-center mb-4">
            </div>
            <div class="card border-left-primary shadow mb-4" style="border-radius: 0.5rem;">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-primary"><i class="fas fa-fw fa-server"></i> Atualizar DNS</h6>
                </div>
                <div class="card-body" style="padding: 1.5rem;">
                    <form method="post" class="mb-3">
                        <div class="form-group">
                            <label for="selected_dns">Selecionar DNS:</label>
                            <select name="selected_dns" id="selected_dns" class="form-control">
                                <option value="">Selecione uma DNS</option>
                                <?php while ($row = $dns_result->fetchArray()) { ?>
                                    <option value="<?php echo htmlspecialchars($row['url']); ?>"><?php echo htmlspecialchars($row['url']); ?></option>
                                <?php } ?>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="new_dns">Nova DNS:</label>
                            <input type="text" name="new_dns" id="new_dns" class="form-control" placeholder="Digite a nova DNS">
                        </div>
                        <button type="submit" name="update_dns" class="btn btn-primary">Atualizar DNS</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</main>
<?php
include 'includes/footer.php';
?>