<?php
include "includes/header.php";

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

$currentThemeKey = "";

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

$themes = [
    "theme_d" => "Theme 1",
    "theme_2" => "Theme 2",
    "theme_3" => "Theme 3",
    "theme_4" => "Theme 4",
    "theme_5" => "Theme 5",
    "theme_6" => "Theme 6",
    "theme_7" => "Theme 7",
    "theme_8" => "Theme 8",
    "theme_9" => "Theme 9"
];

$currentThemeName = isset($themes[$currentThemeKey]) ? $themes[$currentThemeKey] : "Unknown Theme";

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $selectedOption = $_POST['options'] ?? '';

    if (array_key_exists($selectedOption, $themes)) {
        $data[0]["RTXSetting"] = "mLayout";
        $data[0]["PanalData"]  = $selectedOption;

        $jsonData = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
        file_put_contents('./api/theme_change/Setting.json', $jsonData);

        $currentThemeName = $themes[$selectedOption];
        $currentThemeKey  = $selectedOption;

        echo "<div class='alert alert-success'>Selected theme: <b>" . htmlspecialchars($currentThemeName, ENT_QUOTES, 'UTF-8') . "</b> activated successfully.</div>";
    } else {
        echo "<div class='alert alert-danger'>Invalid theme selected!</div>";
    }
}
?>
<style>
/* Global dark / neon theme for layouts page */
.esso-theme-layouts{
    padding: 26px 0 40px;
    background: radial-gradient(circle at top,#0ea5e9 0,#020617 38%,#000 80%);
}

.layout-wrapper{
    max-width: 1100px;
    margin: 0 auto;
}

.layout-card{
    background: radial-gradient(circle at top left,rgba(56,189,248,.16),transparent 55%),
                radial-gradient(circle at bottom right,rgba(96,165,250,.16),transparent 55%),
                linear-gradient(135deg,#020617,#020617,#0b1220);
    border-radius: 18px;
    padding: 22px 24px 26px;
    color: #e5e7eb;
    box-shadow: 0 20px 60px rgba(15,23,42,.9);
    border: 1px solid rgba(148,163,184,.45);
}

.layout-card h1{
    font-size: 1.35rem;
    letter-spacing: .12em;
    font-weight: 800;
    text-transform: uppercase;
    text-align:center;
    margin-bottom: 6px;
}

.layout-card h6{
    font-size:.9rem;
    text-align:center;
    margin-bottom:16px;
    color:#cbd5f5;
}

.form-theme{
    display:flex;
    flex-direction:column;
    align-items:center;
    margin-bottom:18px;
}

.form-theme select{
    width: 260px;
    padding: 8px 14px;
    border-radius:999px;
    border:1px solid rgba(148,163,184,.7);
    background:rgba(15,23,42,.96);
    color:#e5e7eb;
    box-shadow:0 0 0 1px rgba(15,23,42,.7);
}
.form-theme select:focus{
    outline:none;
    border-color:#38bdf8;
    box-shadow:0 0 0 0.12rem rgba(56,189,248,.45);
}

.custom-button{
    margin-top:14px;
    padding:8px 26px;
    border-radius:999px;
    border:none;
    font-weight:700;
    text-transform:uppercase;
    background:linear-gradient(90deg,#22c55e,#a3e635);
    color:#022c22;
    box-shadow:0 12px 32px rgba(22,163,74,.6);
    cursor:pointer;
    transition:transform .2s ease, box-shadow .2s ease, filter .2s ease;
}
.custom-button:hover{
    filter:brightness(1.05);
    transform:translateY(-1px);
    box-shadow:0 16px 40px rgba(22,163,74,.75);
}

/* Theme preview grid */
.image-row{
    display:flex;
    flex-wrap:wrap;
    justify-content:center;
    margin-top:10px;
    gap:16px;
}

.image-container{
    flex: 1 1 260px;
    max-width:260px;
    text-align:center;
    background:rgba(15,23,42,.95);
    border-radius:14px;
    padding:10px;
    border:1px solid rgba(148,163,184,.6);
    box-shadow:0 12px 28px rgba(15,23,42,.8);
    transition:transform .25s ease, box-shadow .25s ease, border-color .25s ease;
}
.image-container p{
    margin-bottom:6px;
    font-size:.9rem;
    font-weight:600;
    color:#e5e7eb;
}

.image-container img{
    width:100%;
    height:auto;
    border-radius:10px;
}

.image-container:hover{
    transform:translateY(-2px);
    box-shadow:0 18px 40px rgba(15,23,42,.9);
    border-color:#38bdf8;
}

/* Highlight selected theme */
.image-container.selected{
    animation: glowTheme 1.8s infinite;
    border-width:2px;
}

@keyframes glowTheme{
    0%{
        box-shadow:0 0 12px rgba(56,189,248,.7);
        border-color:#38bdf8;
    }
    50%{
        box-shadow:0 0 26px rgba(129,140,248,.9);
        border-color:#a855f7;
    }
    100%{
        box-shadow:0 0 12px rgba(56,189,248,.7);
        border-color:#38bdf8;
    }
}
</style>

<div class="container-fluid esso-theme-layouts">
    <div class="layout-wrapper">
        <div class="card border-left-primary shadow h-100 mb-4 layout-card">
            <h1><i class="fa fa-cogs"></i> CHOOSE PANEL THEME</h1>
            <h6>Current theme: <strong><?= htmlspecialchars($currentThemeName, ENT_QUOTES, 'UTF-8'); ?></strong></h6>

            <div class="card-body" style="background:transparent;padding:0;">
                <form class="form-theme" method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8'); ?>">
                    <select name="options" id="options">
                        <?php
                        foreach ($themes as $key => $label) {
                            $selected = ($key === $currentThemeKey) ? 'selected' : '';
                            echo "<option value='" . htmlspecialchars($key, ENT_QUOTES, 'UTF-8') . "' $selected>" . htmlspecialchars($label, ENT_QUOTES, 'UTF-8') . "</option>";
                        }
                        ?>
                    </select>
                    <input type="submit" class="btn btn-primary btn-icon-split custom-button" value="Apply Theme">
                </form>

                <div class="image-row">
                    <?php
                    foreach ($themes as $key => $label) {
                        $selectedClass = ($key === $currentThemeKey) ? 'selected' : '';
                        $imgIndex = str_replace('theme_', '', $key);
                        echo "<div class='image-container $selectedClass'>
                                <p>" . htmlspecialchars($label, ENT_QUOTES, 'UTF-8') . "</p>
                                <img src='./img_custom/layout/" . htmlspecialchars($imgIndex, ENT_QUOTES, 'UTF-8') . ".jpg' alt='" . htmlspecialchars($key, ENT_QUOTES, 'UTF-8') . "'>
                              </div>";
                    }
                    ?>
                </div>
            </div>
        </div>
    </div>
</div>

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