<?php
include "includes/header.php";
?>
<style>
  .custom-button {
    padding: 10px 20px;
  }
  
  .image-container {
    max-width: 400px;
    margin: 0 auto;
  }
  
  .image-container img {
    max-width: 100%;
    height: auto;
    display: block;
    margin: 0 auto;
  }
</style>
<div class="container-fluid">
    <h1 class="h3 mb-1 text-gray-800"> Trocar Logo</h1>
    <div class="card border-left-primary shadow h-100 card shadow mb-4">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary"><i class="fa fa-cogs"></i> Choose</h6>
        </div>
        <div class="card-body">
           <?php
						
						$jsonFilex = './a/rtx/logo_filenames.json';
        
                        $jsonDatax = file_get_contents($jsonFilex);
                            
                        $imageDatax = json_decode($jsonDatax, true);
                            
                        $filenamex = $imageDatax[0]['ImageName'];
                            
                        $imageFilex = "./rtx/logo/" . "$filenamex";
						
						echo '<h3>Atualmente em uso:</h3>';
                        echo '<div class="image-container"><img class="preview-image" src="' . $imageFilex . '" alt="Uploaded Image"></div>';
                        
                        
                        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/';
                                
                                $files = scandir($folderPath);
                                
                                foreach ($files as $file) {
                                    if ($file !== '.' && $file !== '..') {
                                        $filePath = $folderPath . $file;
                                
                                        if (in_array($file, $selectedFiles)) {
                                            
                                        } else {
                                            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/';
                                    $fileName = uniqid() . '.' . pathinfo($file['name'], PATHINFO_EXTENSION);
                                    $destination = $uploadPath . $fileName;
                        
                                    if (move_uploaded_file($fileTemp, $destination)) {
                                        echo "<script>window.location.href='logo.php';</script>";
                                        
                                        $jsonFilePath = './a/rtx/logo_filenames.json';
                                        $jsonData = json_encode([["ImageName" => $fileName]]);
                                        file_put_contents($jsonFilePath, $jsonData);
                                    } else {
                                        echo 'Failed to move the uploaded file.';
                                    }
                                } else {
                                    echo 'Invalid file type. Only JPEG, PNG, and GIF images are allowed.';
                                }
                            }
                        }
                        ?>
                        
                        <form method="post" enctype="multipart/form-data">
                            <label for="image">Selecione uma imagem:</label>
                            <input  type="file" name="image" id="image" accept="image/jpeg, image/png, image/gif">
                            <button class="custom-button btn btn-success btn-icon-split" type="submit" name="upload">Upload</button>
                        </form>

        </div>
</div>
</div>
<?php
include "includes/footer.php";
?>
</body>
</html>
