<?php
session_start();

// Configuração do caminho do log de auditoria
$log_file = 'log_trial.json';

// Quando o botão salvar é pressionado
if (isset($_POST['btn-save'])) {

    // Salvar modo trial
    $is_trial = isset($_POST['trial']) ? $_POST['trial'] : '';
    file_put_contents('trial_mode', $is_trial);

    // Ajustar permissões 777
    @chmod('trial_mode', 0777);

    // Se modo trial está ativo, salvar DNS e URL
    if ($is_trial == 'trial') {
        $app_dns_value = $_POST['app_dns'] ?? '';
        $app_url_value = $_POST['app_url'] ?? '';

        file_put_contents("app_dns", $app_dns_value);
        file_put_contents("app_url", $app_url_value);

        @chmod('app_dns', 0777);
        @chmod('app_url', 0777);

        // Log em JSON para auditoria
        $log_data = [
            'datetime' => date('Y-m-d H:i:s'),
            'dns' => $app_dns_value,
            'url' => $app_url_value,
            'mode' => $is_trial,
            'ip' => $_SERVER['REMOTE_ADDR']
        ];
        $existing_logs = [];
        if (file_exists($log_file)) {
            $existing_logs = json_decode(file_get_contents($log_file), true) ?? [];
        }
        $existing_logs[] = $log_data;
        file_put_contents($log_file, json_encode($existing_logs, JSON_PRETTY_PRINT));
        @chmod($log_file, 0777);
    }

    // Redirecionar para exibir o toast
    header("Location: ".$_SERVER['PHP_SELF']."?r=atualizado");
    exit();
}

// Carregar informações do painel
$IIIIIIIlI1lI = $_SESSION['id'];
$IIIIIIIlI1ll = new SQLite3('api/.anspanel.db');
$IIIIIIIlI1l1 = $IIIIIIIlI1ll->query('SELECT * FROM USERS WHERE id=\'1\'');
$IIIIIIIlI11I = $IIIIIIIlI1l1->fetchArray();
$IIIIIIIlI11l = $IIIIIIIlI11I['NAME'];
$IIIIIIIlI111 = $IIIIIIIlI11I['LOGO'];

echo '<!DOCTYPE html>'."\n";
echo '<html lang="en">'."\n";
echo "\n";
echo '<head>'."\n";
echo "\n";

$IIIIIIIllIII = file_get_contents('includes/eggzie.json');
$IIIIIIIllIIl = json_decode($IIIIIIIllIII, true);
$IIIIIIIllII1 = $IIIIIIIllIIl['info'];
$IIIIIIIllIlI = $IIIIIIIllII1['aa'];
$app_dns = @file_get_contents('app_dns');
$app_url = @file_get_contents('app_url');
$mode = @file_get_contents('trial_mode');

$checked = $mode == 'trial' ? "checked" : "";

?>

<?php include 'includes/header.php'; ?>

<div class="container-fluid">
    <?php if (isset($_GET['r'])): ?>
        <?php $result = $_GET['r']; ?>
        <?php if ($result == "atualizado"): ?>
            <script>
                const Toast = Swal.mixin({
                    toast: true,
                    position: 'bottom',
                    showConfirmButton: false,
                    timer: 2000,
                });
                Toast.fire({
                    icon: 'success',
                    title: 'Mensagem Atualizada com Sucesso!'
                });
            </script>
        <?php endif; ?>
    <?php endif; ?>

    <center><h1 class="h3 mb-1 text-gray-800">Ajuste de ChatBot</h1></center>

    <div class="row">
        <div class="col-lg-12">
            <div class="shadow h-100 card shadow mb-4">
                <div class="card-header py-3">
                    <center><h6 class="m-0 font-weight-bold text-primary"><i class="fa fa-bullhorn"></i> ChatBot</h6></center>
                </div>
                <div class="card-body">
                    <div>
                        <form method="POST">
                            <div class="form-check">
                                <input class="form-check-input" name="trial" type="checkbox" value="trial" id="trial_check" <?php echo $checked; ?>>
                                <label class="form-check-label" for="trial_check">
                                    Habilitar teste automático
                                </label>
                            </div>
                            <?php if ($mode == 'trial'): ?>
                                <b>Dados do chatBot</b><br><br>
                                <label>DNS</label>
                                <input class="form-control" type="text" name="app_dns" placeholder="Insira a DNS" value="<?php echo htmlspecialchars($app_dns); ?>" /><br>
                                <label>LINK</label>
                                <input class="form-control" type="text" name="app_url" placeholder="Link do chatBot" value="<?php echo htmlspecialchars($app_url); ?>" /><br>
                            <?php endif; ?>

                            <center><button name="btn-save" class="btn btn-success">Salvar</button></center>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

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