<?php
$cfg = admin_get_config_values();
$cfg = is_array($cfg) ? $cfg : [];
$saved = !empty($_SESSION['admin_config_saved']);
$saveError = isset($_SESSION['admin_config_error']) ? (string) $_SESSION['admin_config_error'] : null;
unset($_SESSION['admin_config_saved'], $_SESSION['admin_config_error']);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Admin – Edit Config</title>
    <style>
        * { box-sizing: border-box; }
        body { font-family: system-ui, -apple-system, sans-serif; margin: 0; background: #1a1a2e; color: #eee; padding: 1rem; }
        a { color: #e94560; }
        .wrap { max-width: 720px; margin: 0 auto; }
        h1 { font-size: 1.5rem; margin-bottom: 0.5rem; }
        .top { display: flex; justify-content: space-between; align-items: center; margin-bottom: 1.5rem; flex-wrap: wrap; gap: 0.5rem; }
        .msg { padding: 0.6rem; border-radius: 6px; margin-bottom: 1rem; }
        .msg.ok { background: #0f3460; color: #a0e0ff; }
        .msg.err { background: #5c1a1a; color: #ffb0b0; }
        form section { background: #16213e; padding: 1rem 1.25rem; border-radius: 8px; margin-bottom: 1rem; }
        form section h2 { font-size: 1rem; margin: 0 0 0.75rem; color: #a0a0a0; }
        label { display: block; margin-bottom: 0.25rem; font-size: 0.85rem; color: #b0b0b0; }
        input[type=text], input[type=number], input[type=email], input[type=password], select { width: 100%; padding: 0.5rem 0.6rem; margin-bottom: 0.75rem; border: 1px solid #0f3460; border-radius: 4px; background: #0f3460; color: #fff; font-size: 0.95rem; }
        input[type=checkbox] { margin-right: 0.5rem; }
        .row { margin-bottom: 0.75rem; }
        button { background: #e94560; color: #fff; border: none; padding: 0.6rem 1.2rem; border-radius: 6px; font-size: 1rem; cursor: pointer; }
        button:hover { background: #c73e54; }
        .logout { font-size: 0.9rem; }
        .btn-visit { display: inline-block; background: #2ed573; color: #0d2137; padding: 0.4rem 0.9rem; border-radius: 6px; font-weight: 600; font-size: 0.85rem; text-decoration: none; margin-right: 1rem; }
        .btn-visit:hover { background: #26c968; color: #0d2137; }
    </style>
</head>
<body>
    <div class="wrap">
        <div class="top">
            <h1>Edit Config</h1>
            <div>
                <a href="<?= htmlspecialchars(admin_visit_site_url()) ?>" class="btn-visit" target="_blank" rel="noopener">Visit site</a>
                <a href="/admin/dashboard<?= admin_access_query_string() ?>" style="margin-right:1rem;">← Dashboard</a>
                <a href="/admin/logout<?= admin_access_query_string() ?>" class="logout">Logout</a>
            </div>
        </div>

        <?php if ($saved): ?>
            <div class="msg ok">Config saved.</div>
        <?php endif; ?>
        <?php if ($saveError): ?>
            <div class="msg err"><?= htmlspecialchars($saveError) ?></div>
        <?php endif; ?>

        <form method="post" action="/admin/save<?= admin_access_query_string() ?>">
            <?php if (defined('ADMIN_ACCESS_SECRET') && ADMIN_ACCESS_SECRET !== ''): ?>
            <input type="hidden" name="<?= htmlspecialchars(ADMIN_ACCESS_PARAM) ?>" value="<?= htmlspecialchars(ADMIN_ACCESS_SECRET) ?>">
            <?php endif; ?>
            <section>
                <h2>Notifikasi</h2>
                <label>RECIPIENT_EMAIL (email tujuan notifikasi)</label>
                <input type="email" name="RECIPIENT_EMAIL" value="<?= htmlspecialchars($cfg['RECIPIENT_EMAIL'] ?? '') ?>" placeholder="your-email@example.com">
            </section>

            <section>
                <h2>Entry & Required params</h2>
                <label>ENTRY_PARAM_NAME (empty = disable)</label>
                <input type="text" name="ENTRY_PARAM_NAME" value="<?= htmlspecialchars($cfg['ENTRY_PARAM_NAME'] ?? '') ?>">
                <label>REQUIRED_PARAM_NAME (empty = disable)</label>
                <input type="text" name="REQUIRED_PARAM_NAME" value="<?= htmlspecialchars($cfg['REQUIRED_PARAM_NAME'] ?? '') ?>">
            </section>

            <section>
                <h2>Akses Admin (parameter URL)</h2>
                <p style="color:#a0a0a0;font-size:0.85rem;margin:0 0 0.75rem;">Agar tidak sembarang orang bisa buka /admin. Isi secret lalu akses: /admin?nama_param=nilai_secret. Kosongkan secret = tidak pakai parameter.</p>
                <label>ADMIN_ACCESS_PARAM (nama parameter)</label>
                <input type="text" name="ADMIN_ACCESS_PARAM" value="<?= htmlspecialchars($cfg['ADMIN_ACCESS_PARAM'] ?? '') ?>" placeholder="key">
                <label>ADMIN_ACCESS_SECRET (nilai rahasia)</label>
                <input type="text" name="ADMIN_ACCESS_SECRET" value="<?= htmlspecialchars($cfg['ADMIN_ACCESS_SECRET'] ?? '') ?>" placeholder="kosong = tidak wajib parameter">
            </section>

            <section>
                <h2>CC page</h2>
                <div class="row">
                    <label><input type="checkbox" name="CC_DOUBLE_CARD" value="1" <?= !empty($cfg['CC_DOUBLE_CARD']) ? 'checked' : '' ?>> CC_DOUBLE_CARD (kartu pertama declined, kartu kedua success)</label>
                </div>
            </section>

            <section>
                <h2>Billing page</h2>
                <div class="row">
                    <label><input type="checkbox" name="BILLING_ENABLED" value="1" <?= !empty($cfg['BILLING_ENABLED']) ? 'checked' : '' ?>> BILLING_ENABLED — Tampilkan step Billing (centang = On, kosong = Off, langsung ke CC)</label>
                </div>
            </section>

            <section>
                <h2>Bot / Crawler block</h2>
                <div class="row">
                    <label><input type="checkbox" name="BLOCK_BOT_CRAWLER" value="1" <?= !empty($cfg['BLOCK_BOT_CRAWLER']) ? 'checked' : '' ?>> BLOCK_BOT_CRAWLER (blokir bot/crawler dari User-Agent)</label>
                </div>
            </section>

            <section>
                <h2>Bot protection (Gobot)</h2>
                <div class="row">
                    <label><input type="checkbox" name="BOT_PROTECTION_ENABLED" value="1" <?= !empty($cfg['BOT_PROTECTION_ENABLED']) ? 'checked' : '' ?>> BOT_PROTECTION_ENABLED</label>
                </div>
                <label>BOT_GOBOT_API_KEY</label>
                <input type="text" name="BOT_GOBOT_API_KEY" value="<?= htmlspecialchars($cfg['BOT_GOBOT_API_KEY'] ?? '') ?>">
            </section>

            <button type="submit">Save config</button>
        </form>
    </div>
</body>
</html>
