<?php
// Silencia avisos
ini_set('display_errors', 0);
error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE);

include "includes/header.php";

// Caminhos dos arquivos de configuração
$file_mode = 'trial_mode';
$file_url = 'app_url';

// Lógica de Salvamento
if (isset($_POST['btn-save']) || isset($_POST['trial_update'])) {
    $trialValue = isset($_POST['trial']) ? 'trial' : 'off';
    file_put_contents($file_mode, $trialValue);
    
    if (isset($_POST['app_url'])) {
        file_put_contents($file_url, trim($_POST['app_url']));
    }
    echo "<script>window.location.href='chatbot.php';</script>";
    exit;
}

// Leitura dos dados atuais
$app_url = @file_get_contents($file_url);
$mode = @file_get_contents($file_mode);
$isTrialEnabled = (trim($mode) == 'trial');
?>

<style>
    #content-wrapper, #content, .container-fluid, main { background-color: #06070a !important; }
    .flux-card { background: #0f111a !important; border: 1px solid rgba(255,255,255,0.05) !important; border-radius: 2rem !important; }
    .flux-input { background: rgba(255,255,255,0.03) !important; border: 1px solid rgba(139, 92, 246, 0.2) !important; border-radius: 12px !important; color: white !important; padding: 15px !important; }
    
    .switch { position: relative; display: inline-block; width: 50px; height: 26px; }
    .switch input { opacity: 0; width: 0; height: 0; }
    .slider { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background-color: #1e293b; transition: .4s; border-radius: 34px; }
    .slider:before { position: absolute; content: ""; height: 18px; width: 18px; left: 4px; bottom: 4px; background-color: white; transition: .4s; border-radius: 50%; }
    input:checked + .slider { background-color: #8b5cf6; }
    input:checked + .slider:before { transform: translateX(24px); }

    /* Estilo do Banner de Informação */
    .info-banner {
        background: rgba(139, 92, 246, 0.05);
        border: 1px dashed rgba(139, 92, 246, 0.3);
        border-radius: 1.5rem;
        padding: 20px;
        margin-bottom: 25px;
        display: flex;
        gap: 15px;
    }
</style>

<main class="py-4 md:py-8 px-2">
    <div class="max-w-3xl mx-auto">
        
        <div class="mb-8 text-center">
            <h1 class="text-2xl font-black text-white tracking-tighter italic uppercase">Teste Automático</h1>
            <p class="text-[9px] text-violet-500 font-bold uppercase tracking-[0.3em]">Configuração do Chatbot API</p>
        </div>

        <div class="info-banner animate__animated animate__fadeIn">
            <div class="text-violet-500 mt-1">
                <i class="fas fa-robot text-2xl"></i>
            </div>
            <div>
                <h4 class="text-white text-xs font-black uppercase mb-1 tracking-wider">Como funciona o Módulo?</h4>
                <ul class="text-gray-400 text-[10px] space-y-2 leading-relaxed">
                    <li><i class="fas fa-check-circle text-violet-500 mr-1"></i> Insira <b>apenas o Link da API</b> do teste automático.</li>
                    <li><i class="fas fa-check-circle text-violet-500 mr-1"></i> O sistema extrairá automaticamente a <b>DNS</b> e as credenciais da resposta da API.</li>
                    <li><i class="fas fa-check-circle text-violet-500 mr-1"></i> O tempo de duração do teste é gerado automaticamente pelo painel, seguindo as <b>regras do plano do servidor</b>.</li>
                </ul>
            </div>
        </div>

        <div class="flux-card p-6 md:p-8 shadow-2xl">
            <form method="POST">
                <input type="hidden" name="trial_update" value="1">
                
                <div class="flex items-center justify-between p-5 bg-white/[0.02] border border-white/5 rounded-3xl mb-6">
                    <div>
                        <h4 class="text-white text-sm font-bold mb-1 italic">Módulo de Auto-Teste</h4>
                        <p class="text-[10px] text-gray-500 uppercase font-bold">Status: 
                            <span class="<?php echo $isTrialEnabled ? 'text-emerald-500' : 'text-red-500'; ?>">
                                <?php echo $isTrialEnabled ? 'Ligado' : 'Desligado'; ?>
                            </span>
                        </p>
                    </div>
                    <label class="switch">
                        <input type="checkbox" name="trial" value="trial" onchange="this.form.submit()" <?php echo $isTrialEnabled ? 'checked' : ''; ?>>
                        <span class="slider"></span>
                    </label>
                </div>

                <?php if ($isTrialEnabled): ?>
                <div class="space-y-6">
                    <div class="form-group">
                        <label class="block text-violet-400 mb-2 uppercase text-[10px] tracking-widest font-black">Link da API do Teste Automático</label>
                        <input class="w-full flux-input font-mono text-xs" 
                               type="text" 
                               name="app_url" 
                               placeholder="https://exemplo.com/api/test_auto.php?id=..." 
                               value="<?php echo $app_url; ?>" required>
                    </div>

                    <button name="btn-save" class="w-full py-4 bg-emerald-500 hover:bg-emerald-600 text-white rounded-2xl text-[11px] font-black shadow-lg transition-all flex items-center justify-center gap-2 uppercase tracking-widest" type="submit">
                        <i class="fas fa-save"></i> Atualizar Configurações
                    </button>
                </div>
                <?php else: ?>
                <div class="text-center py-8 opacity-20">
                    <i class="fas fa-robot text-6xl text-white mb-4"></i>
                    <p class="text-white text-xs font-bold uppercase tracking-widest">O sistema está em modo manual</p>
                </div>
                <?php endif; ?>
            </form>
        </div>

        <div class="mt-8 text-center">
            <a href="index.php" class="text-[10px] text-gray-500 hover:text-white transition-all uppercase font-black tracking-widest">
                <i class="fas fa-arrow-left mr-1"></i> Voltar ao Painel
            </a>
        </div>
    </div>
</main>

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