<?php

//<-----------------------------------------------------------> 
//<-------------------COPIA AI BRUXÃO-------------------------> 
//<-------------CONTATO: https://t.me/eonbry------------------> 
//<-------------DEVELOPER: EMANUEL NASCIMENTO-----------------> 
//<----------------------------------------------------------->




// Backend logic (SQLite) - safe, minimal, similar to original behavior
$db_path = "./api/db/eonbrydemo.db";
$db = new SQLite3($db_path);

// Table and default row
$table_name = "demo";
$db->exec("CREATE TABLE IF NOT EXISTS $table_name (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    demo_url TEXT,
    demo_name TEXT
)");

$res = $db->query("SELECT COUNT(*) as count FROM $table_name");
$row = $res->fetchArray(SQLITE3_ASSOC);
$numRows = intval($row["count"]);

// Ensure default row exists
if ($numRows == 0) {
    $db->exec("INSERT INTO $table_name (id, demo_url, demo_name) VALUES (1, '', '')");
}

$res = $db->query("SELECT * FROM $table_name WHERE id='1'");
$row = $res->fetchArray(SQLITE3_ASSOC);
$demo_url = $row["demo_url"] ?? '';
$demo_name = $row["demo_name"] ?? '';

// Handle POST update
$updated = false;
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST["submit"])) {
    $demo_url = $_POST["demo_url"] ?? '';
    $demo_name = $_POST["demo_name"] ?? '';
    // Basic escaping to avoid breaking the query; keep behavior aligned with original project
    $demo_url_esc = SQLite3::escapeString($demo_url);
    $demo_name_esc = SQLite3::escapeString($demo_name);
    $db->exec("UPDATE $table_name SET demo_url='$demo_url_esc', demo_name='$demo_name_esc' WHERE id='1'");
    $updated = true;
    // Refresh values from DB
    $res = $db->query("SELECT * FROM $table_name WHERE id='1'");
    $row = $res->fetchArray(SQLITE3_ASSOC);
    $demo_url = $row["demo_url"] ?? '';
    $demo_name = $row["demo_name"] ?? '';
}

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

<style>
/* === FUTURISTIC DESIGN (adapted) === */
:root{
    --primary: #00D4FF; --primary-dark:#0099CC; --secondary:#FF6B9D;
    --accent:#00FF88; --surface:#0A0F1C; --surface-light:#1A1F2C;
    --surface-lighter:#2A2F3C; --text-primary:#FFFFFF; --text-secondary:#B0B7C3;
    --border: rgba(255,255,255,0.08);
    --glow: 0 0 20px rgba(0, 212, 255, 0.18);
}
.main-content { padding: 20px; background: transparent !important; }
.card { background: var(--surface-light); border: 1px solid var(--border); border-radius: 18px; box-shadow: 0 8px 40px rgba(0,0,0,0.45); color: var(--text-primary); overflow: hidden; position: relative; }
.card::before{ content:''; position:absolute; top:0; left:0; right:0; height:4px; background: linear-gradient(90deg, var(--primary), var(--accent), var(--secondary)); }
.card-header{ padding:18px; font-weight:700; background: linear-gradient(135deg,var(--surface-light),var(--surface-lighter)); border-bottom:1px solid var(--border); }
.card-body{ padding:22px; }
.form-control{ background:var(--surface-lighter); border:1px solid var(--border); color:var(--text-primary); border-radius:12px; padding:12px 14px; width:100%; }
.form-control::placeholder{ color: #7f8a99; }
.btn-primary{ background: linear-gradient(135deg,var(--primary),var(--primary-dark)); border:none; color:#fff; padding:10px 18px; border-radius:12px; box-shadow: var(--glow); }
.label-strong{ font-weight:700; color:var(--text-secondary); }
.helper{ font-size:0.9rem; color:var(--text-secondary); margin-top:6px; }
.container-small{ max-width:920px; margin: 0 auto; }
.note{ padding:12px; background: rgba(0,0,0,0.06); border-radius:10px; color:var(--text-secondary); margin-top:10px; }

/* Responsive */
@media (max-width:768px){ .container-small{ padding: 10px; } .card-body{ padding:16px; } }
</style>

<!-- Optional particles script (simple) -->
<script>
document.addEventListener('DOMContentLoaded', function(){
    // small subtle animated dots background
    const part = document.createElement('div');
    part.style.position = 'absolute';
    part.style.top = '0'; part.style.left = '0'; part.style.right = '0'; part.style.bottom = '0';
    part.style.pointerEvents = 'none'; part.style.zIndex = '0'; part.style.opacity = '0.08';
    document.querySelector('main')?.appendChild(part);
});
</script>

<main role="main" class="col-12 pt-4 px-4 main-content">
  <div class="container-small">
    <div class="card">
      <div class="card-header">
        <h4 style="margin:0; font-size:1.1rem; color:var(--text-primary);"><i class="fa fa-play-circle" style="margin-right:10px; color:var(--primary)"></i> Configuração da Lista Demo</h4>
      </div>
      <div class="card-body">
        <p class="helper">Adicione aqui o link da lista demo que será usada pelo aplicativo.</p>
        <form method="post" novalidate>
          <div style="margin-top:12px;">
            <label class="label-strong" for="demo_url">Link da Lista Demo</label>
            <div style="margin-top:6px;" class="input-group">
              <input type="text" id="demo_url" name="demo_url" class="form-control" placeholder="Ex: http://.../get.php?username=demo&password=demo&type=m3u_plus&output=mpegts" value="<?php echo htmlspecialchars($demo_url, ENT_QUOTES); ?>">
            </div>
            <div class="note">Use um link válido de lista M3U (ou similar) compatível com seu aplicativo.</div>
          </div>

          <div style="margin-top:16px;">
            <label class="label-strong" for="demo_name">Nome da Lista</label>
            <div style="margin-top:6px;" class="input-group">
              <input type="text" id="demo_name" name="demo_name" class="form-control" placeholder="Ex: Lista Demo BR" value="<?php echo htmlspecialchars($demo_name, ENT_QUOTES); ?>">
            </div>
          </div>

          <div style="margin-top:18px; display:flex; gap:12px; align-items:center;">
            <button type="submit" name="submit" class="btn-primary"><i class="fa fa-check" style="margin-right:8px"></i> Salvar</button>
            <?php if ($demo_url): ?>
              <a href="<?php echo htmlspecialchars($demo_url, ENT_QUOTES); ?>" target="_blank" class="btn-primary" style="opacity:0.85; background:linear-gradient(135deg,var(--secondary),#FF416C);">Abrir Lista</a>
            <?php endif; ?>
          </div>
        </form>

        <?php if ($updated): ?>
          <script>
            document.addEventListener('DOMContentLoaded', function(){
              if (typeof Swal !== 'undefined') {
                Swal.fire({ toast:true, position:'bottom', showConfirmButton:false, timer:2000, icon:'success', title:'Lista Demo Atualizada com Sucesso!' });
              } else {
                alert('Lista Demo Atualizada com Sucesso!');
              }
            });
          </script>
        <?php endif; ?>

      </div>
    </div>
  </div>
</main>

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