<?php
// Silencia avisos para manter o visual limpo
ini_set('display_errors', 0);
error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE);

include "includes/header.php";

$jsonFilex = './a/rtx/qr_filenames.json';
$jsonDatax = file_get_contents($jsonFilex);
$imageDatax = json_decode($jsonDatax, true);

// Extração do QR Code atual
$filenamex = $imageDatax[0]['ImageName'];
$imageFilex = "./rtx/qrcode/" . $filenamex;

// Lógica de Upload
if (isset($_POST['upload'])) {
    $selectedFiles = ['logo.png', 'index.php', 'iimg.json', 'filenames.json', 'binding_dark.webp', 'bg.jpg', 'api.php', 'favicon.ico', 'logo_ne.png' , '.htaccess'];
    // Corrigido para o path de qrcode conforme sua lógica
    $folderPath = './rtx/qrcode/'; 
    
    if (!is_dir($folderPath)) {
        mkdir($folderPath, 0755, true);
    }

    $files = scandir($folderPath);
    foreach ($files as $file) {
        if ($file !== '.' && $file !== '..' && !in_array($file, $selectedFiles)) {
            unlink($folderPath . $file);
        }
    }

    if (isset($_FILES['image'])) {
        $file = $_FILES['image'];
        $fileType = $file['type'];
        $allowedTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];

        if (in_array($fileType, $allowedTypes)) {
            $fileName = uniqid() . '.' . pathinfo($file['name'], PATHINFO_EXTENSION);
            $destination = $folderPath . $fileName;

            if (move_uploaded_file($file['tmp_name'], $destination)) {
                $jsonData = json_encode([["ImageName" => $fileName]]);
                file_put_contents($jsonFilex, $jsonData);
                echo "<script>window.location.href='pageqrcode.php';</script>";
            }
        }
    }
}
?>

<style>
    /* REMOVE FUNDO BRANCO */
    #content-wrapper, #content, .container-fluid, main {
        background-color: #06070a !important;
    }

    .flux-card {
        background: #0f111a !important;
        border: 1px solid rgba(255,255,255,0.05) !important;
        border-radius: 2rem !important;
    }

    .qr-preview-container {
        background: white; /* QR Codes precisam de fundo branco para leitura */
        padding: 15px;
        border-radius: 1.5rem;
        display: inline-block;
        box-shadow: 0 0 25px rgba(139, 92, 246, 0.2);
        margin: 20px 0;
    }

    .qr-image {
        width: 200px;
        height: 200px;
        object-fit: contain;
        display: block;
    }

    label { color: #94a3b8; font-size: 13px; font-weight: 600; }
</style>

<main class="py-8 px-4">
    <div class="max-w-4xl mx-auto text-center md:text-left">
        
        <div class="mb-8">
            <h1 class="text-3xl font-black text-white tracking-tighter italic uppercase">Gerenciar QR Code</h1>
            <p class="text-[10px] text-violet-500 font-bold uppercase tracking-[0.4em]">VU FLUX • SISTEMA DE PAGAMENTO / ACESSO</p>
        </div>

        <div class="flux-card p-8 shadow-2xl">
            <div class="grid grid-cols-1 md:grid-cols-2 gap-8 items-center">
                
                <div class="text-center border-b md:border-b-0 md:border-r border-white/5 pb-8 md:pb-0 md:pr-8">
                    <h3 class="text-white font-bold mb-4 uppercase text-xs tracking-widest">QR Code em Uso</h3>
                    <div class="qr-preview-container">
                        <img class="qr-image" src="<?php echo $imageFilex; ?>" alt="QR Code">
                    </div>
                    <p class="text-[10px] text-gray-500 mt-2 font-mono"><?php echo $filenamex; ?></p>
                </div>

                <div>
                    <h3 class="text-white font-bold mb-4">Atualizar Código</h3>
                    <form method="post" enctype="multipart/form-data" class="space-y-6">
                        <div class="p-6 border-2 border-dashed border-white/10 rounded-3xl bg-white/[0.01] hover:border-violet-500/50 transition-colors">
                            <i class="fas fa-qrcode text-3xl text-violet-500 mb-3"></i>
                            <p class="text-[11px] text-gray-400 mb-4 uppercase">Arraste ou selecione a nova imagem do QR Code</p>
                            
                            <input class="block w-full text-sm text-gray-500 
                                file:mr-4 file:py-2 file:px-4 
                                file:rounded-full file:border-0 
                                file:text-xs file:font-black 
                                file:bg-violet-600 file:text-white 
                                hover:file:bg-violet-700 cursor-pointer" 
                                type="file" name="image" id="image" accept="image/*">
                        </div>

                        <button class="w-full py-4 bg-emerald-500 hover:bg-emerald-600 text-white rounded-2xl text-xs font-black shadow-lg transition-all flex items-center justify-center gap-2 uppercase tracking-widest" type="submit" name="upload">
                            <i class="fas fa-sync-alt"></i> Substituir QR Code
                        </button>
                    </form>
                </div>

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

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