<?php


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



session_start();
include 'includes/conexao.php';

$id = intval($_GET['id'] ?? 0);
$app = $conexao->prepare("SELECT * FROM apps WHERE id = ?");
$app->execute([$id]);
$app = $app->fetch(PDO::FETCH_ASSOC);

if (!$app) {
    die("App não encontrado.");
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $nome = $_POST['nome'] ?? '';
    $url = $_POST['url'] ?? '';
    $info = $_POST['info'] ?? '';
    $arquivo_nome = $app['arquivo'] ?? null;

    // Verifica se um novo arquivo foi enviado
    if (!empty($_FILES['apk']['name'])) {
        $ext = pathinfo($_FILES['apk']['name'], PATHINFO_EXTENSION);
        $novo_nome = uniqid() . '.' . $ext;
        $caminho = 'uploads/' . $novo_nome;

        // Cria a pasta se não existir
        if (!is_dir('uploads')) {
            mkdir('uploads', 0755, true);
        }

        if (move_uploaded_file($_FILES['apk']['tmp_name'], $caminho)) {
            // Deleta o antigo, se existir
            if (!empty($arquivo_nome) && file_exists('uploads/' . $arquivo_nome)) {
                unlink('uploads/' . $arquivo_nome);
            }
            $arquivo_nome = $novo_nome;
        }
    }

    // Atualiza no banco
    $update = $conexao->prepare("UPDATE apps SET nome = ?, url = ?, info = ?, arquivo = ? WHERE id = ?");
    $update->execute([$nome, $url, $info, $arquivo_nome, $id]);

    $_SESSION['mensagem'] = "App atualizado com sucesso!";
    header("Location: visualizar.php");
    exit;
}

include 'includes/header.php';
?>

<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Editar App - <?= htmlspecialchars($app['nome']) ?></title>
<link href="https://fonts.googleapis.com/css2?family=Orbitron:wght@700&display=swap" rel="stylesheet" />
<style>
    body {
        margin: 0;
        font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        background: #0a0a10;
        color: #e0e0e0;
        min-height: 100vh;
        display: flex;
        justify-content: center;
        align-items: center;
        padding: 20px;
    }

    .glass-container {
        background: rgba(10, 10, 20, 0.85);
        backdrop-filter: blur(14px);
        border: 1px solid #00f0ff;
        border-radius: 15px;
        padding: 30px 40px;
        width: 100%;
        max-width: 480px;
        box-shadow: 0 0 30px rgba(0, 240, 255, 0.3);
    }

    .neon-title {
        color: #00f0ff;
        text-align: center;
        font-size: 2rem;
        margin-bottom: 30px;
        text-shadow: 0 0 10px rgba(0, 240, 255, 0.7);
        font-weight: 700;
        font-family: 'Orbitron', sans-serif;
        text-transform: uppercase;
        letter-spacing: 2px;
    }

    form {
        display: flex;
        flex-direction: column;
        gap: 20px;
    }

    label {
        font-weight: 700;
        color: #00f0ff;
        text-shadow: 0 0 5px rgba(0, 240, 255, 0.7);
        font-size: 0.95rem;
    }

    input[type="text"],
    input[type="url"],
    textarea,
    input[type="file"] {
        background: rgba(15, 15, 30, 0.9);
        border: 1.5px solid #00f0ff;
        border-radius: 10px;
        padding: 12px 15px;
        color: #c0f9f8;
        font-size: 1rem;
        font-weight: 500;
        transition: all 0.3s ease;
        resize: vertical;
        box-shadow: inset 0 0 12px rgba(0, 240, 255, 0.25);
    }

    input:focus,
    textarea:focus {
        outline: none;
        border-color: #00fff7;
        box-shadow: 0 0 20px #00f0ff, inset 0 0 15px #00f0ff;
        background: rgba(10, 10, 20, 1);
    }

    textarea {
        min-height: 120px;
        font-family: inherit;
    }

    button {
        background: rgba(0, 240, 255, 0.3);
        color: #00f0ff;
        border: 1.5px solid #00f0ff;
        padding: 14px 0;
        border-radius: 12px;
        font-weight: 700;
        font-size: 1.1rem;
        cursor: pointer;
        text-transform: uppercase;
        letter-spacing: 1.5px;
        box-shadow: 0 0 20px #00f0ff;
        transition: all 0.3s ease;
    }

    button:hover {
        background: #00f0ff;
        color: #0a1a1e;
        box-shadow: 0 0 35px #00f0ff, 0 0 60px #00f0ffaa;
    }

    .mensagem {
        margin-bottom: 20px;
        padding: 12px;
        background: #00fff744;
        border-radius: 10px;
        color: #004d4d;
        font-weight: 700;
        text-align: center;
        box-shadow: 0 0 12px #00ffcc88;
    }

    .apk-atual {
        text-align: center;
        font-size: 0.9rem;
        color: #ccc;
        margin-top: -10px;
    }
    
   
</style>
</head>
<body>
    <main class="glass-container">
        <h1 class="neon-title">Editar App</h1>

        <?php if (!empty($_SESSION['mensagem'])): ?>
            <div class="mensagem"><?= htmlspecialchars($_SESSION['mensagem']) ?></div>
            <?php unset($_SESSION['mensagem']); ?>
        <?php endif; ?>

        <form method="POST" enctype="multipart/form-data">
            <label for="nome">Nome do App</label>
            <input type="text" id="nome" name="nome" value="<?= htmlspecialchars($app['nome']) ?>" required>

            <label for="url">URL do App (opcional)</label>
            <input type="url" id="url" name="url" value="<?= htmlspecialchars($app['url']) ?>">

            <label for="info">Informações do App</label>
            <textarea id="info" name="info" required><?= htmlspecialchars($app['info']) ?></textarea>

            <label for="apk">Arquivo APK (opcional)</label>
            <input type="file" id="apk" name="apk" accept=".apk">

            <?php if (!empty($app['arquivo'])): ?>
                <p class="apk-atual">Atual: <a href="uploads/<?= htmlspecialchars($app['arquivo']) ?>" target="_blank" style="color:#00f0ff;">Baixar APK atual</a></p>
            <?php endif; ?>

            <button type="submit">Atualizar</button>
        </form>
    </main>
</body>
</html>
