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

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

if(isset($_GET['id'])) {
    $id = (int)$_GET['id'];
    
    if(isset($_POST['submit'])) {
        $stmt = $db->prepare("UPDATE ibo SET username = :user, password = :pass, expire_date = :expire, url = :url, title = :title WHERE id = :id");
        $stmt->bindValue(':user', trim($_POST['username']), SQLITE3_TEXT);
        $stmt->bindValue(':pass', trim($_POST['password']), SQLITE3_TEXT);
        $stmt->bindValue(':expire', trim($_POST['expire_date']), SQLITE3_TEXT);
        $stmt->bindValue(':url', trim($_POST['dns']), SQLITE3_TEXT);
        $stmt->bindValue(':title', trim($_POST['title']), SQLITE3_TEXT);
        $stmt->bindValue(':id', $id, SQLITE3_INTEGER);
        
        if($stmt->execute()) {
            header('Location: users.php');
            exit;
        } else {
            $erro = "Erro ao atualizar: " . $db->lastErrorMsg();
        }
    }
    
    $stmt = $db->prepare("SELECT * FROM ibo WHERE id = :id");
    $stmt->bindValue(':id', $id, SQLITE3_INTEGER);
    $user = $stmt->execute()->fetchArray(SQLITE3_ASSOC);
}

if(!isset($user) || !$user) {
    header('Location: users.php');
    exit;
}

include 'includes/header.php';
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<title>Editar Usuário</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
    body{ margin:0; font-family: "Segoe UI",sans-serif; background: radial-gradient(circle at top,#1b2a45 0%,#050712 55%,#03040a 100%); color:#eef2ff; padding:20px; }
    .wrapper{ max-width:600px; margin:auto; }
    .card{ background:rgba(15,27,52,0.9); padding:30px; border-radius:20px; border:1px solid rgba(114,159,255,0.3); box-shadow:0 15px 35px rgba(0,0,0,0.6); }
    .input{ width:100%; padding:12px; border:1px solid rgba(113,145,232,0.4); border-radius:12px; background:rgba(8,14,30,0.95); color:#eaf0ff; margin-bottom:15px; }
    .btn{ padding:12px 25px; border-radius:12px; border:none; cursor:pointer; font-weight:bold; transition:0.3s; background:linear-gradient(135deg,#52ffb8,#2fcc8a); color:#050712; width:100%; }
    .alert-danger{ background:rgba(255,82,82,0.1); color:#ff5252; border:1px solid #ff5252; padding:15px; border-radius:10px; margin-bottom:20px; }
</style>
</head>
<body>
<div class="wrapper">
    <div class="card">
        <h1>Editar Usuário</h1>
        <?php if(isset($erro)): ?><div class="alert alert-danger"><?= $erro ?></div><?php endif; ?>
        <form method="post">
            <label>Nome do Servidor</label>
            <input type="text" name="title" class="input" value="<?= htmlspecialchars($user['title']) ?>" required>
            
            <label>MAC Address</label>
            <input type="text" class="input" value="<?= htmlspecialchars($user['mac_address']) ?>" disabled>
            
            <label>Usuário</label>
            <input type="text" name="username" class="input" value="<?= htmlspecialchars($user['username']) ?>" required>
            
            <label>Senha</label>
            <input type="text" name="password" class="input" value="<?= htmlspecialchars($user['password']) ?>" required>
            
            <label>DNS / URL</label>
            <input type="text" name="dns" class="input" value="<?= htmlspecialchars($user['url']) ?>" required>
            
            <label>Data de Expiração</label>
            <input type="date" name="expire_date" class="input" value="<?= htmlspecialchars($user['expire_date']) ?>" required>
            
            <button type="submit" name="submit" class="btn">Salvar Alterações</button>
        </form>
        <a href="users.php" style="display:block; text-align:center; margin-top:20px; color:#8ba7ff; text-decoration:none;">Voltar para Lista</a>
    </div>
</div>
</body>
</html>
<?php $db->close(); ?>
