<?php 
session_start();
include 'auth.php'; 

if (!isset($_SESSION['admin_token'])) {
    $_SESSION['admin_token'] = bin2hex(random_bytes(32));
    }

    $json_dir = __DIR__ . '/api';
    $json_path = $json_dir . '/ibo.json';

    if (!file_exists($json_dir)) {
        mkdir($json_dir, 0755, true);
        }

        if (!file_exists($json_path)) {
            $initial = ["app_info" => ["android_version_code" => "3.8", "apk_url" => ""]];
                file_put_contents($json_path, json_encode($initial, JSON_PRETTY_PRINT));
                }

                $ibo_data = json_decode(file_get_contents($json_path), true);
                $base_file = basename($_SERVER["SCRIPT_NAME"]);

                if (isset($_POST['submit_update'])) {
                    if (!isset($_POST['token']) || !hash_equals($_SESSION['admin_token'], $_POST['token'])) {
                            header("Location: login.php"); exit();
                                }
                                    
                                        $ibo_data["app_info"]["android_version_code"] = $_POST['version_name'];
                                            $ibo_data["app_info"]["apk_url"] = $_POST['apk_link'];
                                                
                                                    file_put_contents($json_path, json_encode($ibo_data, JSON_PRETTY_PRINT));
                                                        $_SESSION['msg_success'] = "Configurações de Atualização Salvas!";
                                                            header("Location: $base_file"); exit;
                                                            }

                                                            include ('includes/header.php');
                                                            ?>

                                                            <style>
                                                                /* RESET PARA EVITAR ROLAGEM LATERAL */
                                                                    * { 
                                                                            box-sizing: border-box; 
                                                                                    max-width: 100%;
                                                                                        }

                                                                                            html, body {
                                                                                                    overflow-x: hidden; /* Mata qualquer tentativa de rolar para o lado */
                                                                                                            background-color: #020617 !important;
                                                                                                                    margin: 0;
                                                                                                                            padding: 0;
                                                                                                                                }

                                                                                                                                    .main-panel, .content, .wrapper {
                                                                                                                                            background-color: #020617 !important;
                                                                                                                                                    min-height: 100vh;
                                                                                                                                                        }

                                                                                                                                                            /* CONTAINER PRINCIPAL CENTRALIZADO */
                                                                                                                                                                .container-lux {
                                                                                                                                                                        display: flex;
                                                                                                                                                                                justify-content: center;
                                                                                                                                                                                        align-items: flex-start;
                                                                                                                                                                                                padding: 20px;
                                                                                                                                                                                                        width: 100%;
                                                                                                                                                                                                            }
                                                                                                                                                                                                                
                                                                                                                                                                                                                    /* CARD DO FORMULÁRIO ADAPTADO */
                                                                                                                                                                                                                        .form-lux {
                                                                                                                                                                                                                                background: #070e1b;
                                                                                                                                                                                                                                        border-radius: 28px;
                                                                                                                                                                                                                                                padding: 25px;
                                                                                                                                                                                                                                                        border: 1px solid #1e293b;
                                                                                                                                                                                                                                                                max-width: 600px; /* Reduzi um pouco para encaixar melhor em telas menores */
                                                                                                                                                                                                                                                                        width: 100%;
                                                                                                                                                                                                                                                                                box-shadow: 0 20px 50px rgba(0,0,0,0.5);
                                                                                                                                                                                                                                                                                        margin-top: 20px;
                                                                                                                                                                                                                                                                                            }

                                                                                                                                                                                                                                                                                                /* INPUTS QUE NÃO ESTOURAM O CARD */
                                                                                                                                                                                                                                                                                                    .input-lux {
                                                                                                                                                                                                                                                                                                            width: 100%; 
                                                                                                                                                                                                                                                                                                                    background: #0f172a !important; 
                                                                                                                                                                                                                                                                                                                            border: 1px solid #1e293b !important;
                                                                                                                                                                                                                                                                                                                                    color: #fff !important; 
                                                                                                                                                                                                                                                                                                                                            padding: 15px; 
                                                                                                                                                                                                                                                                                                                                                    border-radius: 16px; 
                                                                                                                                                                                                                                                                                                                                                            margin-bottom: 20px;
                                                                                                                                                                                                                                                                                                                                                                    font-size: 1rem;
                                                                                                                                                                                                                                                                                                                                                                            display: block;
                                                                                                                                                                                                                                                                                                                                                                                }

                                                                                                                                                                                                                                                                                                                                                                                    /* BOTÃO CENTRALIZADO E AJUSTADO */
                                                                                                                                                                                                                                                                                                                                                                                        .btn-save-lux {
                                                                                                                                                                                                                                                                                                                                                                                                width: 100%; 
                                                                                                                                                                                                                                                                                                                                                                                                        background: linear-gradient(135deg, #d946ef 0%, #a855f7 100%);
                                                                                                                                                                                                                                                                                                                                                                                                                color: #fff; 
                                                                                                                                                                                                                                                                                                                                                                                                                        padding: 18px; 
                                                                                                                                                                                                                                                                                                                                                                                                                                border-radius: 18px; 
                                                                                                                                                                                                                                                                                                                                                                                                                                        font-weight: 800; 
                                                                                                                                                                                                                                                                                                                                                                                                                                                text-transform: uppercase; 
                                                                                                                                                                                                                                                                                                                                                                                                                                                        border: none; 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                cursor: pointer;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                        box-shadow: 0 10px 20px rgba(217, 70, 239, 0.2);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                transition: 0.3s;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        margin-top: 10px;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                .btn-save-lux:hover { transform: translateY(-3px); opacity: 0.9; }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    .label-lux { 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            color: #d946ef; 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    font-size: 0.75rem; 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            font-weight: 800; 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    margin-bottom: 8px; 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            display: block; 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    text-transform: uppercase;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            padding-left: 5px;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        .info-box {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                background: rgba(59, 130, 246, 0.1);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        border: 1px solid rgba(59, 130, 246, 0.2);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                padding: 15px; 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        border-radius: 15px; 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                margin-bottom: 25px;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        color: #60a5fa; 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                font-size: 0.85rem;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        line-height: 1.4;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                /* TÍTULO RESPONSIVO */
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    .title-lux {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            font-weight: 900; 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    margin-bottom: 5px; 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            color: #fff;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    font-size: 1.4rem;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            @media (max-width: 480px) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    .form-lux { padding: 15px; border-radius: 20px; }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            .title-lux { font-size: 1.2rem; }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    .btn-save-lux { padding: 15px; font-size: 0.9rem; }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        </style>

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        <div class="container-lux">
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            <div class="form-lux">
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    <h2 class="title-lux">
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                <i class="fas fa-sync-alt" style="margin-right: 10px; color: #d946ef;"></i> Atualização
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        </h2>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                <p style="color: #64748b; margin-bottom: 25px; font-size: 0.9rem;">Controle de Versão e APK</p>

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        <div class="info-box">
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    <i class="fas fa-info-circle"></i> 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                Versão atual no sistema: <b><?= htmlspecialchars($ibo_data['app_info']['android_version_code']) ?></b>. 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            <br>A atualização só aparece se o número for maior que o do App.
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    </div>

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            <form method="post">
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        <input type="hidden" name="token" value="<?= $_SESSION['admin_token'] ?>">

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    <label class="label-lux">Nova Versão</label>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                <input type="text" name="version_name" class="input-lux" 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   placeholder="Ex: 4.0" 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      value="<?= htmlspecialchars($ibo_data['app_info']['android_version_code']) ?>" required>

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  <label class="label-lux">Link Direto do APK</label>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              <input type="text" name="apk_link" class="input-lux" 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 placeholder="https://..." 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    value="<?= htmlspecialchars($ibo_data['app_info']['apk_url']) ?>" required>

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                <button type="submit" name="submit_update" class="btn-save-lux">
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                Salvar Configurações
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            </button>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    </form>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        </div>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        </div>

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        <script>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            <?php if (isset($_SESSION['msg_success'])): ?>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    Swal.fire({
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                icon: 'success', title: 'Sucesso!', text: '<?= $_SESSION['msg_success'] ?>',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            background: '#020617', color: '#fff', confirmButtonColor: '#d946ef'
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    });
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            <?php unset($_SESSION['msg_success']); ?>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                <?php endif; ?>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                </script>

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                <?php include ('includes/footer.php');?>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                