<?php

//<----------------------------------------------------------->
//<-------------------COPIA AI BRUXÃO------------------------->
//<-------------CONTATO: https://t.me/eonbry------------------>
//<-------------DEVELOPER: EMANUEL NASCIMENTO----------------->
//<----------------------------------------------------------->


session_start();
if (!isset($_SESSION['eggzie_iboPlayer'])) {
    header("Location: login.php");
    exit();
}

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

$admin = $db->query("SELECT * FROM USERS WHERE id='1'")->fetchArray(SQLITE3_ASSOC);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $name = trim($_POST['name']);
    $username = trim($_POST['username']);

    // senha só atualiza se preenchida
   $password = $admin['PASSWORD'];
if (!empty($_POST['password'])) {
    $password = $_POST['password']; // senha em texto normal, igual seu sistema original
}


    // logo atual
    $logo = $admin['LOGO'];

    // upload opcional
    if (!empty($_FILES['logo']['name'])) {
        $file = "img/" . time() . "_" . basename($_FILES['logo']['name']);
        move_uploaded_file($_FILES['logo']['tmp_name'], $file);
        $logo = $file;
    }

    // salvar
    $stmt = $db->prepare("UPDATE USERS SET NAME=:n, USERNAME=:u, PASSWORD=:p, LOGO=:l WHERE id='1'");
    $stmt->bindValue(':n', $name);
    $stmt->bindValue(':u', $username);
    $stmt->bindValue(':p', $password);
    $stmt->bindValue(':l', $logo);
    $stmt->execute();

    $_SESSION['admin_name'] = $name;

    header("Location: profile.php?ok=1");
    exit();
}

include "includes/header.php"
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Update Login</title>

    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            background: #0b132b;
            color: #fff;
            font-family: Arial;
        }

        .card {
            margin-top: 40px;
            background: rgba(255,255,255,0.05);
            border: 1px solid rgba(255,255,255,0.15);
            border-radius: 10px;
        }

        .form-control {
            background: #ffffff;
            border-radius: 6px;
        }

        .btn-success {
            width: 150px;
            padding: 10px;
            font-weight: bold;
        }

        .logo-preview {
            margin-top: 15px;
            max-width: 130px;
        }

        #togglePass {
            margin-top: 10px;
        }
    </style>
</head>

<body>

<div class="container">
    <div class="card p-4">

        <h3 class="text-white">Update Login</h3>
        <hr>

        <?php if (isset($_GET['ok'])): ?>
            <div class="alert alert-success">Atualizado!</div>
        <?php endif; ?>

        <form method="POST" enctype="multipart/form-data">

            <div class="form-group">
                <label>Name</label>
                <input type="text" class="form-control" name="name" value="<?= $admin['NAME']; ?>" required>
            </div>

            <div class="form-group">
                <label>Username</label>
                <input type="text" class="form-control" name="username" value="<?= $admin['USERNAME']; ?>" required>
            </div>

            <div class="form-group">
                <label>Password</label>
                <input type="password" id="passField" class="form-control" name="password">
                <div>
                    <input type="checkbox" id="togglePass"> Show Password
                </div>
            </div>

            <div class="form-group">
    <label>Logo atual</label>
    <br>
    <img id="logoPreview" src="<?= $admin['LOGO']; ?>" class="logo-preview">
    <br><br>

    <label>Trocar logo</label>
    <input type="file" name="logo" id="logoInput" accept="image/*">
</div>

<script>
document.getElementById('logoInput').addEventListener('change', function(e) {
    const file = e.target.files[0];
    if (file) {
        const reader = new FileReader();
        reader.onload = function(evt) {
            document.getElementById('logoPreview').src = evt.target.result;
        };
        reader.readAsDataURL(file);
    }
});
</script>

            <button type="submit" class="btn btn-success">Submit</button>

        </form>

    </div>
</div>

<script>
document.getElementById('togglePass').addEventListener('change', function() {
    const field = document.getElementById('passField');
    field.type = this.checked ? "text" : "password";
});
</script>

<script>
setTimeout(() => {
    const alert = document.querySelector('.alert-success');
    if (alert) alert.style.display = 'none';
}, 2000);
</script>


</body>
</html>
