<?php
// Developer eonbry
ini_set("display_errors", 0);
ini_set("display_startup_errors", 0);
error_reporting(E_ALL);

// Tenta ler o arquivo JSON, com verificação para evitar erros se o arquivo estiver vazio
$jsondata = @file_get_contents("./api/update.json");
$data = json_decode($jsondata, true);

$avc = isset($data["app_info"]["android_version_code"]) ? $data["app_info"]["android_version_code"] : "";
$apkurl = isset($data["app_info"]["apk_url"]) ? $data["app_info"]["apk_url"] : "";

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

if (isset($_POST["submit"])) {
    $jsonData = @file_get_contents("./api/update.json");
    $arrayData = json_decode($jsonData, true);
    if (!is_array($arrayData)) {
        $arrayData = [];
    }

    $replacementData = [
        "app_info" => [
            "android_version_code" => $_POST["android_version_code"], 
            "apk_url" => $_POST["apk_url"]
        ]
    ];
    
    $newArrayData = array_replace_recursive($arrayData, $replacementData);
    $newJsonData = json_encode($newArrayData, JSON_PRETTY_PRINT);
    file_put_contents("./api/update.json", $newJsonData);
    
    // Redireciona com um parâmetro simples e para a execução do script
    header("Location: update.php?success=1");
    exit; 
}

include "includes/header.php";
?>
<style>
     html { scrollbar-width: thin; scrollbar-color: #00a8e1 #1a2a33; }
        /* Chrome, Edge e Safari */
        ::-webkit-scrollbar { width: 8px; height: 8px; }
        ::-webkit-scrollbar-track { background: #1a2a33; border-radius: 10px; }
        ::-webkit-scrollbar-thumb { background: #00a8e1; border-radius: 10px; }
        ::-webkit-scrollbar-thumb:hover { background: #0080b3; }
</style>
<div class="container-fluid">

    <h1 class="h3 mb-4 text-gray-800">Apk Update</h1>
    
    <?php 
    // Mostra a mensagem de sucesso se o redirecionamento ocorreu
    if (isset($_GET['success']) && $_GET['success'] == '1') {
        echo $message;
    }
    ?>
    
    <div class="row">

        <div class="col-lg-12">

            <div class="card border-left-primary shadow mb-4">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-primary"><i class="fa fa-refresh"></i> Apk Update</h6>
                </div>
                <div class="card-body">
                    
                    <form method="post" action="">
                        <div class="form-group">
                            <h6 class="form-text"><strong>Version Code</strong></h6>
                            <input type="text" class="form-control" placeholder="Version Code" name="android_version_code" value="<?php echo htmlspecialchars($avc); ?>">
                        </div>
                        
                        <div class="form-group">
                            <h6 class="form-text"><strong>Download Url</strong></h6>
                            <input type="text" class="form-control" placeholder="http://link to app.apk" name="apk_url" value="<?php echo htmlspecialchars($apkurl); ?>">
                        </div>
                        
                        <button class="btn btn-primary 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>
</div>

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

<script> 
$(document).ready(function () {
    $("#flash-msg").delay(3000).fadeOut("slow");
});
</script>
</body>
</html>