<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$db = new SQLite3('../a/.eggziedb.db');
$db->exec('CREATE TABLE IF NOT EXISTS ibo(id INTEGER PRIMARY KEY NOT NULL,mac_address VARCHAR(100),username VARCHAR(100),password VARCHAR(100),expire_date VARCHAR(100),url VARCHAR(100),title VARCHAR(100),created_at VARCHAR(100))');
$res = $db->query('SELECT * FROM ibo');

if(isset($_GET['index'])) {
    $index = $_GET['index'];
} else {
    $index = ""; 
}

if(isset($_GET['update'])) {
    $user_id = $_GET['update'];
    $res1 = $db->query("SELECT * FROM ibo WHERE id = '$user_id'");
    while ($row1 = $res1->fetchArray()) {
        $user_id = $row1['id'];
        $username = $row1['username'];
        $password = $row1['password'];
        $mac_address_old = $row1['mac_address'];
        $dns = $row1['url'];
        $extra = $row1['extra'];
        $title = $row1['title'];
    }
} else {
    echo "<script>alert('Selecione o usuario a ser editado!');</script>";
    header('Location: index.php');
}

include '../includes/header2.php';

if (isset($_POST['submit'])) {
    $usuario = $_POST['username'];
    $mac_adress = strtoupper($_POST['mac_address']);
    // Lógica original de verificação
    $result = $db1->query("SELECT COUNT(*) as count FROM ibo WHERE mac_address = '$mac_adress' AND mac_address <> '$mac_address_old'");
    $row = $result->fetchArray(SQLITE3_ASSOC);
    $count = $row['count'];

    if($count || $usuario == "ADM") {
        echo "<script>alert('Já existe um usuario registrado com esse usuario que você esta tentando, por favor escolha outro!');window.location.href='index.php';</script>";
    } else {
        $mac_adress = strtoupper($_POST['mac_address']);
        $username = $_POST['username'];
        $password = $_POST['password'];
        $dns = $_POST['url'];
        $title = $_POST['title'];
        $id = $_POST['id'];
        $descricao = $_POST['Descricao'];
        $db1->exec("UPDATE ibo SET mac_address = '$mac_adress', username = '$username', password = '$password', url = '$dns', title = '$title', extra = '$descricao' WHERE id = '$id'");
        $db1->close();
        echo "<script>window.location.href='index.php';</script>";
    }
}
?>

<style>
    @import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;700&family=Poppins:wght@300;400;600&display=swap');

    :root {
        --neon-emerald: #00ff88;
        --neon-gold: #ffcc00;
        --dark-bg: #050a0e;
        --glass-bg: rgba(255, 255, 255, 0.02);
        --glass-border: rgba(0, 255, 136, 0.15);
    }

    /* Blindagem contra fundo branco */
    html, body, #wrapper, #content-wrapper, #content, .container-fluid, .bg-white {
        background-color: var(--dark-bg) !important;
        color: #e0e0e0 !important;
        font-family: 'Poppins', sans-serif;
    }

    .h3 {
        font-family: 'Orbitron', sans-serif;
        color: #fff !important;
        text-transform: uppercase;
        letter-spacing: 2px;
        text-shadow: 0 0 10px rgba(0, 255, 136, 0.3);
    }

    .card-futuristic {
        background: var(--glass-bg) !important;
        backdrop-filter: blur(15px);
        border: 1px solid var(--glass-border) !important;
        border-radius: 15px !important;
        box-shadow: 0 10px 30px rgba(0,0,0,0.5);
        margin-bottom: 25px;
    }

    .card-header-futuristic {
        background: rgba(0, 255, 136, 0.03) !important;
        border-bottom: 1px solid var(--glass-border) !important;
        padding: 1.2rem !important;
    }

    .card-header-futuristic h6 {
        font-family: 'Orbitron', sans-serif;
        color: var(--neon-emerald) !important;
        text-shadow: 0 0 8px var(--neon-emerald);
        margin: 0;
        text-transform: uppercase;
    }

    .form-control {
        background: rgba(0, 0, 0, 0.5) !important;
        border: 1px solid var(--glass-border) !important;
        color: #fff !important;
        border-radius: 8px !important;
        padding: 12px 15px !important;
        height: auto !important;
    }

    .form-control:focus {
        border-color: var(--neon-emerald) !important;
        box-shadow: 0 0 15px rgba(0, 255, 136, 0.2) !important;
    }

    label strong {
        color: var(--neon-emerald);
        font-family: 'Orbitron', sans-serif;
        font-size: 0.75rem;
        text-transform: uppercase;
    }

    .btn-emerald {
        background: transparent;
        border: 1px solid var(--neon-emerald);
        color: var(--neon-emerald) !important;
        font-family: 'Orbitron', sans-serif;
        padding: 12px 30px;
        border-radius: 8px;
        transition: 0.4s;
        text-transform: uppercase;
        font-size: 0.8rem;
        cursor: pointer;
    }

    .btn-emerald:hover {
        background: var(--neon-emerald);
        color: #000 !important;
        box-shadow: 0 0 20px var(--neon-emerald);
    }
</style>

<div class="container-fluid pt-4 px-5">
    <h1 class="h3 mb-4">Update User</h1>

    <div class="row">
        <div class="col-lg-10 mx-auto">
            <div class="card card-futuristic shadow">
                <div class="card-header-futuristic">
                    <h6><i class="fas fa-user-edit mr-2"></i> Edit User Details</h6>
                </div>
                <div class="card-body p-4">
                    <form method="post">
                        <input name="id" value="<?php echo $user_id; ?>" type="hidden">
                        
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group mb-4">
                                    <label><strong>Mac Address</strong></label>
                                    <input class="form-control" id="mac_address" name="mac_address" value="<?php echo $mac_address_old;?>" type="text" required/>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group mb-4">
                                    <label><strong>Server Name</strong></label>
                                    <input type="text" class="form-control" name="title" value="<?php echo $title;?>" required/>
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group mb-4">
                                    <label><strong>Username</strong></label>
                                    <input type="text" class="form-control" name="username" value="<?php echo $username;?>" required/>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group mb-4">
                                    <label><strong>Password</strong></label>
                                    <input type="text" class="form-control" name="password" value="<?php echo $password;?>" required/>
                                </div>
                            </div>
                        </div>

                        <div class="form-group mb-4">
                            <label><strong>DNS / URL</strong></label>
                            <input type="text" class="form-control" name="url" value="<?php echo $dns;?>" required/>
                        </div>

                        <div class="form-group mb-4">
                            <label><strong>Description</strong></label>
                            <input type="text" class="form-control" name="Descricao" value="<?php echo $extra;?>" required/>
                        </div>

                        <div class="text-right mt-4">
                            <a href="index.php" class="btn btn-outline-secondary mr-2" style="font-family:'Orbitron'; border-radius:8px;">Cancel</a>
                            <button class="btn btn-emerald" name="submit" type="submit">
                                <i class="fas fa-save mr-2"></i> Save Changes
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
document.addEventListener("DOMContentLoaded", function() {
    var macAddressInput = document.getElementById("mac_address");
    macAddressInput.addEventListener("input", function(e) {
        var value = e.target.value;
        // Máscara para 8 pares (16 caracteres hex)
        value = value.replace(/[^a-fA-F0-9]/g, "").toUpperCase().substring(0, 16);

        var formattedValue = "";
        for (var i = 0; i < value.length; i++) {
            formattedValue += value[i];
            if ((i + 1) % 2 === 0 && i < value.length - 1) {
                formattedValue += ":";
            }
        }
        e.target.value = formattedValue;
    });
});
</script>

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