<?php
// Developer eonbry
ini_set("display_errors", 0);
ini_set("display_startup_errors", 0);
error_reporting(E_ALL);

// Inicializa variáveis para evitar erro caso o banco falhe
$name = "";
$user = "";
$pass = "";
$logo = "";

try {
    $db = new SQLite3("./api/.anspanel.db");
    
    // Query limpa para evitar tela branca
    $res = $db->query("SELECT * FROM USERS WHERE ID='1'");
    if ($res) {
        $row = $res->fetchArray(SQLITE3_ASSOC);
        if ($row) {
            $name = $row["NAME"];
            $user = $row["USERNAME"];
            $pass = $row["PASSWORD"];
            $logo = $row["LOGO"];
        }
    }
} catch (Exception $e) {
    // Caso o banco não exista ou não possa ser lido
    die("Erro ao conectar ao banco de dados de usuários.");
}

$message = '<div class="alert alert-primary" id="flash-msg"><h4><i class="icon fa fa-check"></i> Profile Updated!</h4></div>';

if (isset($_POST["submit"])) {
    // Uso de Prepared Statement para corrigir erros de aspas simples nos campos e evitar SQL Injection
    $stmt = $db->prepare("UPDATE USERS SET NAME = :name, USERNAME = :username, PASSWORD = :password, LOGO = :logo WHERE ID = '1'");
    $stmt->bindValue(':name', $_POST["name"], SQLITE3_TEXT);
    $stmt->bindValue(':username', $_POST["username"], SQLITE3_TEXT);
    $stmt->bindValue(':password', $_POST["password"], SQLITE3_TEXT);
    $stmt->bindValue(':logo', $_POST["logo"], SQLITE3_TEXT);
    $stmt->execute();
    
    // Inicia a sessão corretamente
    if (session_status() === PHP_SESSION_NONE) {
        session_start();
    }
    session_regenerate_id();
    $_SESSION["loggedin"] = true;
    $_SESSION["name"] = $_POST["username"];
    
    // Redirecionamento
    header("Location: profile.php?success=1");
    exit;
}

include "includes/header.php";
?>
<style>
     html { scrollbar-width: thin; scrollbar-color: #00a8e1 #1a2a33; }
        /* Chrome, Edge e Safari */
        ::-webkit-scrollbar { width: 8px; height: 8px; }
        ::-webkit-scrollbar-track { background: #1a2a33; border-radius: 10px; }
        ::-webkit-scrollbar-thumb { background: #00a8e1; border-radius: 10px; }
        ::-webkit-scrollbar-thumb:hover { background: #0080b3; }
</style>
<div class="container-fluid">

    <?php
    if (isset($_GET["success"]) && $_GET["success"] == '1') {
        echo $message;
    }
    ?>

    <h1 class="h3 mb-4 text-gray-800">Update Login</h1>
    
    <div class="row">

        <div class="col-lg-12">

            <div class="card border-left-primary shadow mb-4">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-primary"><i class="fa fa-user"></i> Update Profile</h6>
                </div>
                <div class="card-body">
                    
                    <form method="post" action="">
                        <div class="form-group">
                            <label class="control-label" for="name">
                                <strong>Name</strong>
                            </label>
                            <div class="input-group">
                                <input type="text" class="form-control text-primary" name="name" value="<?php echo htmlspecialchars($name); ?>" placeholder="Enter Name">
                            </div>
                        </div>
                        
                        <div class="form-group">
                            <label class="control-label" for="username">
                                <strong>Username</strong>
                            </label>
                            <div class="input-group">
                                <input type="text" class="form-control text-primary" name="username" value="<?php echo htmlspecialchars($user); ?>" placeholder="Enter Username">
                            </div>
                        </div>
                        
                        <div class="form-group">
                            <label class="control-label" for="password">
                                <strong>Password</strong>
                            </label>
                            <div class="input-group">
                                <input type="text" class="form-control text-primary" name="password" value="<?php echo htmlspecialchars($pass); ?>" placeholder="Enter Password">
                            </div>
                        </div>
                        
                        <div class="form-group">
                            <label class="control-label" for="logo">
                                <strong>Logo URL</strong>
                            </label>
                            <div class="input-group">
                                <input type="text" class="form-control text-primary" name="logo" value="<?php echo htmlspecialchars($logo); ?>" placeholder="Enter Profile URL">
                            </div>
                        </div>
                        
                        <div class="form-group d-flex justify-content-between align-items-center mt-4">
                            <button class="btn btn-success btn-icon-split" name="submit" type="submit">
                                <span class="icon text-white-50"><i class="fas fa-check"></i></span>
                                <span class="text">Submit</span>
                            </button>
                            
                            <?php if (!empty($logo)): ?>
                            <div>
                                <img width="100" src="<?php echo htmlspecialchars($logo); ?>" alt="logo image" class="img-thumbnail" style="background: rgba(255,255,255,0.1);" />
                            </div>
                            <?php endif; ?>
                        </div>
                    </form>

                </div>
            </div>

        </div>
    </div>
</div>

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

<script>
$(document).ready(function () {
    $("#flash-msg").delay(3000).fadeOut("slow");
});
</script>

</body>
</html>