<?php
ini_set("display_errors", 0);
ini_set("display_startup_errors", 0);
error_reporting(32767);

$jsondata = file_get_contents("./includes/ansibo.json");
$data = json_decode($jsondata, true);
$json = $data["info"];
$col = $json["aa"];

// Define os atributos "selected" para as opções
$langs = array_fill(0, 15, "");
$langs[$json["aa"]] = "selected";

$message = "<div class=\"alert alert-primary\" id=\"flash-msg\"><h4><i class=\"icon fa fa-check\"></i>Theme Updated!</h4></div>";

if (isset($_POST["submit"])) {
    $jsonData = file_get_contents("./includes/ansibo.json");
    $date = date("d-m-Y H:i:s");
    $time = time();
    $mug = base64_encode($date);
    $mug1 = sha1($mug);
    $mug2 = $json["ii"] + 1;
    $arrayData = json_decode($jsonData, true);

    $replacementData = [
        "info" => [
            "aa" => $_POST["aa"],
            "bb" => $date,
            "cc" => $time,
            "dd" => $mug,
            "ff" => $mug2,
            "gg" => $mug1
        ]
    ];

    $newArrayData = array_replace_recursive($arrayData, $replacementData);
    $newJsonData = json_encode($newArrayData, JSON_UNESCAPED_UNICODE);
    file_put_contents("./includes/mugibo.json", $newJsonData);
    header("Location: colours.php?message=" . urlencode($message));
    exit(); // Adiciona exit após header
}

include "includes/header.php";
echo "<!-- Begin Page Content -->\n<div class=\"container-fluid\">\n";

if (isset($_GET["message"])) {
    echo $_GET["message"];
}
?>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
<link rel="stylesheet" href="https://pro.fontawesome.com/releases/v6.0.0-beta3/css/all.css">

<h1 class="h3 mb-1 text-gray-800">Colour Changes</h1>

<div class="row">
    <div class="col-lg-12">
        <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-paintbrush"></i> Colours</h6>
            </div>
            <div class="card-body">
                <form method="post">
                    <div class="form-group">
                        <label class="control-label requiredField" for="aa">
                            <strong>Pick Your Colour</strong>
                        </label>
                        <select class="select form-control text-primary" id="select" name="aa">
                            <option value="1" <?= $langs[1] ?>>Blue</option>
                            <option value="2" <?= $langs[2] ?>>Dark-Blue</option>
                            <option value="3" <?= $langs[3] ?>>Red</option>
                            <option value="4" <?= $langs[4] ?>>Orange</option>
                            <option value="5" <?= $langs[5] ?>>Green</option>
                            <option value="6" <?= $langs[6] ?>>Teal</option>
                            <option value="7" <?= $langs[7] ?>>Cyan</option>
                            <option value="8" <?= $langs[8] ?>>Silver</option>
                            <option value="9" <?= $langs[9] ?>>Gray</option>
                            <option value="11" <?= $langs[11] ?>>Black</option>
                            <option value="12" <?= $langs[12] ?>>Purple</option>
                            <option value="13" <?= $langs[13] ?>>Yellow</option>
                            <option value="14" <?= $langs[14] ?>>Dark Black</option>
                        </select>
                    </div>
                    <button class="btn btn-success btn-icon-split" name="submit" type="submit">
                        <span class="icon text-white-50"><i class="fas fa-check"></i></span>
                        <span class="text">Submit</span>
                    </button>
                </form>
            </div>
        </div>
    </div>
</div>

<br><br><br>

<?php
include "includes/footer.php";
require "includes/ans.php";
echo "</body>\n";
?>
