<?php

include "includes/header.php";

$jsonData = file_get_contents('./a/rtx/Setting.json');
$data = json_decode($jsonData, true);

$tema_atual = "";

foreach($data as $item){
    if($item["RTXSetting"] === "mLayout") {
        $tema_atual = $item["PanalData"];
        break;
    }
}

$temas = [
    "theme_d" => "Padrão",
    "theme_1" => "Tema 1",
    "theme_2" => "Tema 2",
    "theme_3" => "Tema 3",
    "theme_4" => "Tema 4",
    "theme_5" => "Tema 5"
];

$tema_atual_escolhido = isset($temas[$tema_atual]) ? $temas[$tema_atual] : "Tema Desconhecido";

?>
<style>
    .custom-button {
        padding: 10px 20px;
    }
    .image-row {
        display: flex;
        flex-wrap: wrap;
        justify-content: center;
        margin-bottom: 20px;
    }
    .image-container {
        flex: 1 1 300px;
        max-width: 300px;
        margin: 10px;
        text-align: center;
        background-color: #FFF;
        box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        border-radius: 10px;
        padding: 10px;
    }
    .image-container img {
        width: 100%;
        height: auto;
        border-radius: 10px;
    }
    label, select, input {
        background: #F8F9FC;
        padding: 10px 20px;
        margin-left: 10px;
        border: none;
        border-radius: 10px;
        box-shadow: 5px 5px 5px 0 rgba(0, 0, 0, 0.35);
    }
    form {
        display: flex;
        flex-direction: column;
        align-items: center;
    }
</style>

<div class="container-fluid">
    <!-- Page Heading -->
    <center><h1 class="h3 mb-1 text-gray-800">Escolha o Tema</h1></center>
    <!-- Custom codes -->
    <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> Tema atual ( <?= $tema_atual_escolhido; ?> )</h6>
        </div>
        <div class="card-body">
            <?php
            if ($_SERVER['REQUEST_METHOD'] === 'POST') {
                $selectedOption = $_POST['options'];
                $tema = $temas[$selectedOption];
                echo "O tema escolhido é: $tema";

                $jsonData = file_get_contents('./a/rtx/Setting.json');
                $data = json_decode($jsonData, true);

                $data[0]["RTXSetting"] = "mLayout";
                $data[0]["PanalData"] = $selectedOption;

                $jsonData = json_encode($data, JSON_PRETTY_PRINT);
                file_put_contents('./a/rtx/Setting.json', $jsonData);
            }
            ?>
            <form class="form-theme" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
                <select name="options" id="options">
                    <?php foreach ($temas as $key => $value): ?>
                        <option value="<?= $key ?>" <?= $key === $tema_atual ? 'selected' : '' ?>><?= $value ?></option>
                    <?php endforeach; ?>
                </select>
                <br><br>
                <input type="submit" class="btn btn-primary btn-icon-split custom-button" value="Ativar">
            </form>
            <br><br>
            <div class="image-row">
                <?php foreach ($temas as $key => $value): ?>
                    <div class="image-container">
                        <p><?= $value ?></p>
                        <img src="./rtx/layout/<?= substr($key, -1) ?>.png" alt="<?= $key ?>">
                    </div>
                <?php endforeach; ?>
            </div>
        </div>
    </div>
</div>
<?php
include "includes/footer.php";
?>
</body>
</html>
