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

// Conexão com o banco de dados
$db1 = new SQLite3('a/.eggziedb.db');

// Verifica se o formulário foi submetido
$message = '';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $old_mac = trim($_POST['old_mac']);
    $new_mac = trim($_POST['new_mac']);
    
    if (!empty($old_mac) && !empty($new_mac)) {
        // Verifica se o MAC antigo existe
        $result = $db1->query("SELECT * FROM ibo WHERE mac_address = '$old_mac'");
        if ($row = $result->fetchArray()) {
            // Atualiza o MAC no banco de dados
            $db1->exec("UPDATE ibo SET mac_address = '$new_mac' WHERE mac_address = '$old_mac'");
            $message = '<div class="alert alert-success">MAC atualizado com sucesso!</div>';
        } else {
            $message = '<div class="alert alert-danger">MAC antigo não encontrado.</div>';
        }
    } else {
        $message = '<div class="alert alert-warning">Por favor, preencha ambos os campos.</div>';
    }
}

include 'includes/header.php';
?>

<main role="main" class="col-15 pt-4 px-5">
    <div class="container">
        <div class="card border-left-primary shadow h-100 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"></i> Migrar MAC</h6>
            </div>
            <div class="card-body">
                <?php echo $message; ?>
                <form method="post">
                    <div class="form-group">
                        <label for="old_mac">MAC Antigo:</label>
                        <input type="text" name="old_mac" id="old_mac" class="form-control" placeholder="Digite o MAC antigo">
                    </div>
                    <div class="form-group">
                        <label for="new_mac">Novo MAC:</label>
                        <input type="text" name="new_mac" id="new_mac" class="form-control" placeholder="Digite o novo MAC">
                    </div>
                    <button type="submit" class="btn btn-primary">Migrar</button>
                </form>
            </div>
        </div>
    </div>
</main>

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