<?php

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


include "includes/header.php";

$jsonFilex = './a/rtx/logo_filenames.json';
$jsonDatax = file_get_contents($jsonFilex);
$imageDatax = json_decode($jsonDatax, true);
$filenamex = $imageDatax[0]['ImageName'];
$uploadmethord = $imageDatax[0]['Upload_type'];

if ($uploadmethord == "by_file") {
    $string = $filenamex;
    $firstLetterRemoved = substr($string, 1);
    $imageFilex = "$firstLetterRemoved";
    $methord = "Upload por Arquivo";
} elseif ($uploadmethord == "by_url") {
    $imageFilex = "$filenamex";
    $methord = "Upload por URL";
} else {
    $imageFilex = "https://c4.wallpaperflare.com/wallpaper/159/71/731/errors-minimalism-typography-red-wallpaper-preview.jpg";
    $methord = "";
}

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'];
    $folderPath = './rtx/logo/ADM/';
    
    $files = scandir($folderPath);
    
    foreach ($files as $file) {
        if ($file !== '.' && $file !== '..') {
            $filePath = $folderPath . $file;
            if (!in_array($file, $selectedFiles)) {
                unlink($filePath);
            }
        }
    }
    
    if (isset($_FILES['image'])) {
        $file = $_FILES['image'];
        $fileType = $file['type'];
        $fileTemp = $file['tmp_name'];

        $allowedTypes = ['image/jpeg', 'image/png', 'image/gif'];
        if (in_array($fileType, $allowedTypes)) {
            $uploadPath = './rtx/logo/ADM/';
            $fileName = uniqid() . '.' . pathinfo($file['name'], PATHINFO_EXTENSION);
            $destination = $uploadPath . $fileName;

            if (move_uploaded_file($fileTemp, $destination)) {
                echo "<script>window.location.href='mRTXBGlogo.php';</script>";
                
                $jsonFilePath = './a/rtx/logo_filenames.json';
                $jsonData = json_encode([["ImageName" => "../rtx/logo/ADM/" . $fileName, 'Upload_type' => 'by_file']]);
                file_put_contents($jsonFilePath, $jsonData);
            } else {
                echo 'Falha ao mover o arquivo enviado.';
            }
        } else {
            echo 'Tipo de arquivo inválido. Apenas imagens JPEG, PNG e GIF são permitidas.';
        }
    }
}

if (isset($_POST['url-submit'])) {
    $imageUrl = $_POST['image-url'];

    if (filter_var($imageUrl, FILTER_VALIDATE_URL)) {
        $jsonFilePath = './a/rtx/logo_filenames.json';

        $newImageData = [
            'ImageName' => $imageUrl,
            'Upload_type' => 'by_url'
        ];

        $jsonData = file_get_contents($jsonFilePath);
        $imageData = json_decode($jsonData, true);
        $imageData[0] = $newImageData;
        $jsonData = json_encode($imageData);

        if (file_put_contents($jsonFilePath, $jsonData)) {
            echo "<script>window.location.href='mRTXBGlogo.php';</script>";
        } else {
            echo 'Falha ao salvar os dados da imagem no arquivo JSON.';
        }
    } else {
        echo 'URL inválida.';
    }
}
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<title>Alterar Logotipo</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<style>
/* DESIGN MODERNO E ELEGANTE - APENAS CORES E ESTILO VISUAL */
:root {
    --primary-dark: #0d1117;
    --primary-blue: #1f6feb;
    --secondary-gray: #161b22;
    --accent-purple: #8957e5;
    --text-light: #f0f6fc;
    --border-color: #30363d;
    --success-green: #238636;
    --hover-blue: #2d81e0;
}

/* Fundo com gradiente sutil */
body {
    background: linear-gradient(135deg, var(--primary-dark) 0%, var(--secondary-gray) 100%) fixed !important;
    color: var(--text-light) !important;
    font-family: 'Segoe UI', system-ui, sans-serif !important;
}

/* Container principal */
.container-fluid {
    background: transparent !important;
    max-width: 1200px !important;
    margin: 0 auto !important;
    padding: 20px !important;
}

/* Título principal */
.h3.mb-1.text-gray-800 {
    color: var(--text-light) !important;
    font-size: 28px !important;
    font-weight: 700 !important;
    margin: 25px 0 !important;
    padding-bottom: 15px !important;
    border-bottom: 2px solid var(--primary-blue) !important;
    position: relative !important;
    text-align: center !important;
}

.h3.mb-1.text-gray-800::after {
    content: '';
    position: absolute !important;
    bottom: -2px !important;
    left: 50% !important;
    transform: translateX(-50%) !important;
    width: 100px !important;
    height: 2px !important;
    background: linear-gradient(90deg, var(--primary-blue), var(--accent-purple)) !important;
}

/* Card - Design moderno */
.card {
    background: rgba(22, 27, 34, 0.85) !important;
    border: 1px solid var(--border-color) !important;
    border-radius: 16px !important;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4) !important;
    backdrop-filter: blur(10px) !important;
    overflow: hidden !important;
    transition: transform 0.3s ease, box-shadow 0.3s ease !important;
}

.card:hover {
    transform: translateY(-5px) !important;
    box-shadow: 0 12px 40px rgba(31, 111, 235, 0.2) !important;
}

/* Cabeçalho do card */
.card-header {
    background: linear-gradient(135deg, 
        rgba(31, 111, 235, 0.1), 
        rgba(137, 87, 229, 0.1)) !important;
    border-bottom: 1px solid var(--border-color) !important;
    padding: 20px 25px !important;
}

.card-header h6 {
    color: var(--text-light) !important;
    font-size: 18px !important;
    font-weight: 600 !important;
    display: flex !important;
    align-items: center !important;
    gap: 10px !important;
}

.card-header .text-primary2 {
    color: var(--primary-blue) !important;
}

/* Corpo do card */
.card-body {
    color: var(--text-light) !important;
    padding: 30px !important;
}

/* Status atual */
.card-body h3 {
    color: var(--primary-blue) !important;
    font-size: 20px !important;
    font-weight: 600 !important;
    margin-bottom: 25px !important;
    padding: 15px !important;
    background: rgba(31, 111, 235, 0.1) !important;
    border-radius: 10px !important;
    border-left: 4px solid var(--accent-purple) !important;
    text-align: center !important;
}

/* Radio buttons - Design moderno */
input[type="radio"] {
    accent-color: var(--primary-blue) !important;
    transform: scale(1.2) !important;
    margin-right: 8px !important;
}

/* Labels dos radio buttons */
.method-selector {
    display: flex !important;
    gap: 20px !important;
    margin-bottom: 30px !important;
    justify-content: center !important;
    flex-wrap: wrap !important;
}

.method-option {
    display: flex !important;
    align-items: center !important;
    gap: 12px !important;
    padding: 15px 25px !important;
    background: rgba(48, 54, 61, 0.3) !important;
    border: 1px solid rgba(113, 145, 232, 0.3) !important;
    border-radius: 12px !important;
    cursor: pointer !important;
    transition: all 0.3s ease !important;
    min-width: 220px !important;
}

.method-option:hover {
    background: rgba(31, 111, 235, 0.2) !important;
    border-color: var(--primary-blue) !important;
    transform: translateY(-3px) !important;
}

.method-option.active {
    background: rgba(31, 111, 235, 0.3) !important;
    border-color: var(--primary-blue) !important;
    box-shadow: 0 5px 15px rgba(31, 111, 235, 0.2) !important;
}

.method-label {
    font-size: 16px !important;
    font-weight: 600 !important;
    color: var(--text-light) !important;
    display: flex !important;
    align-items: center !important;
    gap: 10px !important;
}

/* Preview da imagem */
.preview-image {
    border: 2px solid var(--border-color) !important;
    border-radius: 12px !important;
    padding: 15px !important;
    background: rgba(0, 0, 0, 0.3) !important;
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3) !important;
    margin: 25px auto !important;
    transition: all 0.3s ease !important;
    max-width: 100% !important;
    height: auto !important;
    display: block !important;
    max-height: 300px !important;
    object-fit: contain !important;
}

.preview-image:hover {
    border-color: var(--primary-blue) !important;
    transform: scale(1.01) !important;
}

/* Inputs e botões - Design moderno */
.form-group {
    margin-bottom: 25px !important;
}

.form-label {
    display: block !important;
    color: #8ba7ff !important;
    font-size: 14px !important;
    font-weight: 600 !important;
    margin-bottom: 10px !important;
    display: flex !important;
    align-items: center !important;
    gap: 8px !important;
    text-transform: uppercase !important;
    letter-spacing: 0.5px !important;
}

.form-input, .custom-button {
    width: 100% !important;
    padding: 14px 18px !important;
    background: rgba(48, 54, 61, 0.6) !important;
    border: 1px solid var(--border-color) !important;
    color: var(--text-light) !important;
    border-radius: 10px !important;
    font-size: 14px !important;
    font-weight: 500 !important;
    transition: all 0.3s ease !important;
    max-width: 100% !important;
    margin-top: 5px !important;
}

.form-input:focus, .custom-button:focus {
    border-color: var(--primary-blue) !important;
    box-shadow: 0 0 0 3px rgba(31, 111, 235, 0.2) !important;
    outline: none !important;
}

.custom-button:hover {
    background: rgba(31, 111, 235, 0.2) !important;
    border-color: var(--primary-blue) !important;
}

/* Botão de submit especial */
.btn-success {
    background: linear-gradient(135deg, var(--primary-blue), var(--accent-purple)) !important;
    border: none !important;
    border-radius: 10px !important;
    padding: 16px 32px !important;
    font-weight: 600 !important;
    font-size: 15px !important;
    letter-spacing: 0.5px !important;
    text-transform: uppercase !important;
    transition: all 0.3s ease !important;
    margin-top: 20px !important;
    width: 100% !important;
    max-width: 250px !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    gap: 10px !important;
}

.btn-success:hover {
    background: linear-gradient(135deg, var(--accent-purple), var(--primary-blue)) !important;
    transform: translateY(-3px) !important;
    box-shadow: 0 8px 20px rgba(31, 111, 235, 0.3) !important;
}

.btn-success:active {
    transform: translateY(-1px) !important;
}

/* Área de upload de arquivo */
input[type="file"] {
    padding: 12px !important;
    background: rgba(9, 14, 30, 0.8) !important;
    border: 2px dashed rgba(113, 145, 232, 0.5) !important;
    border-radius: 12px !important;
    color: var(--text-light) !important;
    width: 100% !important;
    cursor: pointer !important;
    transition: all 0.3s ease !important;
}

input[type="file"]:hover {
    border-color: var(--primary-blue) !important;
    background: rgba(113, 145, 232, 0.1) !important;
}

/* Badge de método */
.method-badge {
    display: inline-flex !important;
    align-items: center !important;
    gap: 8px !important;
    padding: 6px 16px !important;
    background: rgba(82, 255, 184, 0.1) !important;
    color: #52ffb8 !important;
    border: 1px solid rgba(82, 255, 184, 0.3) !important;
    border-radius: 20px !important;
    font-size: 13px !important;
    font-weight: 600 !important;
    margin-left: 15px !important;
}

/* Mensagens de alerta */
.alert-success {
    background: linear-gradient(135deg, 
        rgba(35, 134, 54, 0.2), 
        rgba(35, 134, 54, 0.1)) !important;
    border: 1px solid rgba(35, 134, 54, 0.3) !important;
    color: #3fb950 !important;
    padding: 16px !important;
    border-radius: 10px !important;
    margin: 20px 0 !important;
    text-align: center !important;
    backdrop-filter: blur(5px) !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    gap: 10px !important;
}

.alert-danger {
    background: linear-gradient(135deg, 
        rgba(248, 81, 73, 0.2), 
        rgba(248, 81, 73, 0.1)) !important;
    border: 1px solid rgba(248, 81, 73, 0.3) !important;
    color: #f85149 !important;
    padding: 16px !important;
    border-radius: 10px !important;
    margin: 20px 0 !important;
    text-align: center !important;
    backdrop-filter: blur(5px) !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    gap: 10px !important;
}

/* Formulários */
#upload-form, #url-form {
    background: rgba(22, 27, 34, 0.5) !important;
    padding: 25px !important;
    border-radius: 12px !important;
    border: 1px solid var(--border-color) !important;
    margin: 20px 0 !important;
}

/* Dica de upload */
.upload-hint {
    font-size: 13px !important;
    color: #8ba7ff !important;
    margin-top: 8px !important;
    display: flex !important;
    align-items: center !important;
    gap: 8px !important;
    opacity: 0.8 !important;
}

/* RESPONSIVIDADE */
@media (max-width: 768px) {
    .container-fluid {
        padding: 10px !important;
    }
    
    .card-body {
        padding: 20px !important;
    }
    
    .method-selector {
        flex-direction: column !important;
        align-items: center !important;
    }
    
    .method-option {
        width: 100% !important;
        max-width: 300px !important;
        justify-content: center !important;
    }
    
    .btn-success {
        max-width: 100% !important;
        padding: 14px 20px !important;
    }
    
    .preview-image {
        max-height: 200px !important;
    }
    
    .h3.mb-1.text-gray-800 {
        font-size: 24px !important;
    }
    
    .card-body h3 {
        font-size: 18px !important;
        flex-direction: column !important;
        gap: 10px !important;
    }
    
    .method-badge {
        margin-left: 0 !important;
        margin-top: 10px !important;
    }
}

@media (max-width: 480px) {
    .card {
        padding: 15px !important;
    }
    
    .card-header {
        padding: 15px !important;
    }
    
    .btn-success {
        padding: 12px 18px !important;
        font-size: 14px !important;
    }
    
    .method-label {
        font-size: 14px !important;
    }
}

/* ANIMAÇÕES */
@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.card {
    animation: fadeIn 0.5s ease-out !important;
}

/* Efeito de foco nos inputs */
.form-input:focus-within {
    border-color: var(--primary-blue) !important;
}

/* Placeholder estilizado */
::placeholder {
    color: #6b7899 !important;
    opacity: 0.7 !important;
}

/* Ícones */
.fa {
    font-size: 16px !important;
}
</style>

<script>
// JavaScript para controlar a visibilidade dos formulários
document.addEventListener('DOMContentLoaded', function() {
    const uploadRadio = document.getElementById('upload-radio');
    const urlRadio = document.getElementById('url-radio');
    const uploadForm = document.getElementById('upload-form');
    const urlForm = document.getElementById('url-form');
    const uploadOption = document.querySelector('.method-option:first-child');
    const urlOption = document.querySelector('.method-option:last-child');
    
    // Inicialmente mostrar apenas o formulário de upload
    uploadForm.style.display = 'block';
    urlForm.style.display = 'none';
    
    // Função para mostrar formulário de upload
    function showUploadForm() {
        uploadForm.style.display = 'block';
        urlForm.style.display = 'none';
        uploadOption.classList.add('active');
        urlOption.classList.remove('active');
    }
    
    // Função para mostrar formulário de URL
    function showUrlForm() {
        uploadForm.style.display = 'none';
        urlForm.style.display = 'block';
        urlOption.classList.add('active');
        uploadOption.classList.remove('active');
    }
    
    // Event listeners para os radio buttons
    uploadRadio.addEventListener('change', showUploadForm);
    urlRadio.addEventListener('change', showUrlForm);
    
    // Event listeners para as opções (clique na div)
    uploadOption.addEventListener('click', function() {
        uploadRadio.checked = true;
        showUploadForm();
    });
    
    urlOption.addEventListener('click', function() {
        urlRadio.checked = true;
        showUrlForm();
    });
    
    // Inicializar com upload ativo
    uploadOption.classList.add('active');
});
</script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
</head>
<body>

<div class="wrapper">
    <!-- Header -->
    <div class="header">
        <h1>
            <i class="fas fa-image"></i>
            Alterar Logotipo
        </h1>
        <div class="subtitle">
            Escolha uma imagem para definir como logotipo do sistema
        </div>
    </div>
    
    <!-- Main Card -->
    <div class="card">
        <div class="card-header">
            <i class="fas fa-camera"></i>
            <h3>Configurações do Logotipo</h3>
        </div>
        
        <div class="current-logo">
            <h3>
                <i class="fas fa-image"></i>
                Logotipo Atual
                <span class="method-badge">
                    <i class="fas fa-<?php echo ($uploadmethord == 'by_file') ? 'file-upload' : 'link'; ?>"></i>
                    <?php echo $methord; ?>
                </span>
            </h3>
            <img class="preview-image" src="<?php echo htmlspecialchars($imageFilex); ?>" alt="Logotipo Atual" onerror="this.src='https://c4.wallpaperflare.com/wallpaper/159/71/731/errors-minimalism-typography-red-wallpaper-preview.jpg'">
        </div>
        
        <div class="upload-method">
            <h4 style="color: #8ba7ff; margin-bottom: 15px; text-align: center;">
                <i class="fas fa-upload"></i>
                Selecione o método de upload:
            </h4>
            
            <div class="method-selector">
                <label class="method-option" id="upload-option">
                    <input type="radio" name="upload-type" id="upload-radio" checked>
                    <span class="method-label">
                        <i class="fas fa-file-upload"></i>
                        Enviar arquivo de imagem
                    </span>
                </label>
                
                <label class="method-option" id="url-option">
                    <input type="radio" name="upload-type" id="url-radio">
                    <span class="method-label">
                        <i class="fas fa-link"></i>
                        Usar URL da imagem
                    </span>
                </label>
            </div>
        </div>
        
        <!-- Upload Form -->
        <form method="post" enctype="multipart/form-data" id="upload-form" class="active">
            <div class="form-group">
                <label class="form-label" for="image">
                    <i class="fas fa-image"></i>
                    Selecionar imagem para upload:
                </label>
                <input class="form-input-file" type="file" name="image" id="image" accept="image/jpeg, image/png, image/gif" required>
                <div class="upload-hint">
                    <i class="fas fa-info-circle"></i>
                    Formatos suportados: JPEG, PNG, GIF
                </div>
            </div>
            
            <div class="form-actions">
                <button class="btn btn-success" type="submit" name="upload">
                    <i class="fas fa-upload"></i>
                    Fazer Upload
                </button>
            </div>
        </form>
        
        <!-- URL Form -->
        <form method="post" id="url-form">
            <div class="form-group">
                <label class="form-label" for="image-url">
                    <i class="fas fa-link"></i>
                    URL da imagem:
                </label>
                <input class="form-input" type="text" name="image-url" id="image-url" placeholder="https://exemplo.com/imagem.jpg" required>
                <div class="upload-hint">
                    <i class="fas fa-info-circle"></i>
                    Informe uma URL válida de uma imagem
                </div>
            </div>
            
            <div class="form-actions">
                <button class="btn btn-success" type="submit" name="url-submit">
                    <i class="fas fa-check"></i>
                    Usar URL
                </button>
            </div>
        </form>
    </div>
</div>

<script>
document.addEventListener("DOMContentLoaded", function() {
    const uploadRadio = document.getElementById('upload-radio');
    const urlRadio = document.getElementById('url-radio');
    const uploadForm = document.getElementById('upload-form');
    const urlForm = document.getElementById('url-form');
    const uploadOption = document.getElementById('upload-option');
    const urlOption = document.getElementById('url-option');
    
    // Function to show upload form
    function showUploadForm() {
        uploadForm.classList.add('active');
        urlForm.classList.remove('active');
        uploadOption.style.background = 'rgba(113, 145, 232, 0.1)';
        uploadOption.style.borderColor = '#7eb4ff';
        urlOption.style.background = 'rgba(9, 14, 30, 0.7)';
        urlOption.style.borderColor = 'rgba(113, 145, 232, 0.3)';
    }
    
    // Function to show URL form
    function showUrlForm() {
        uploadForm.classList.remove('active');
        urlForm.classList.add('active');
        urlOption.style.background = 'rgba(113, 145, 232, 0.1)';
        urlOption.style.borderColor = '#7eb4ff';
        uploadOption.style.background = 'rgba(9, 14, 30, 0.7)';
        uploadOption.style.borderColor = 'rgba(113, 145, 232, 0.3)';
    }
    
    // Event listeners for radio buttons
    uploadRadio.addEventListener('change', showUploadForm);
    urlRadio.addEventListener('change', showUrlForm);
    
    // Event listeners for option clicks
    uploadOption.addEventListener('click', function() {
        uploadRadio.checked = true;
        showUploadForm();
    });
    
    urlOption.addEventListener('click', function() {
        urlRadio.checked = true;
        showUrlForm();
    });
    
    // File input styling
    const fileInput = document.getElementById('image');
    fileInput.addEventListener('change', function(e) {
        if (this.files.length > 0) {
            this.style.borderColor = '#52ffb8';
            this.style.background = 'rgba(82, 255, 184, 0.1)';
        }
    });
    
    // URL input styling
    const urlInput = document.getElementById('image-url');
    urlInput.addEventListener('focus', function() {
        this.style.borderColor = '#7eb4ff';
    });
    
    urlInput.addEventListener('blur', function() {
        if (!this.value) {
            this.style.borderColor = 'rgba(113, 145, 232, 0.5)';
        }
    });
    
    // Initialize with upload form visible
    showUploadForm();
});
</script>

</body>
</html>