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

/* =========================
   Archivo JSON
========================= */
$jsonFile = __DIR__ . "/api/config_demo.json";
$jsonData = [];

if (file_exists($jsonFile)) {
    $jsonData = json_decode(file_get_contents($jsonFile), true);
}

/* =========================
   Obtener DNS desde BD
========================= */
$dns_list = $db->select('dns', '*', '', 'title ASC');

/* =========================
   Guardar configuración
========================= */
if (isset($_POST['submit_demo'])) {

    // Preparar datos a guardar
    $saveData = [
        'username'      => $_POST['username'] ?? '',
        'password'      => $_POST['password'] ?? '',
        'dns_uid'       => $_POST['dns_uid'] ?? '',
        'payUrl'        => $_POST['payUrl'] ?? '',
        'status'        => $_POST['status'] ?? 'Desactivado',
        'demo_duration' => intval($_POST['demo_duration'] ?? 3600) // en segundos
    ];

    // Guardar JSON
    file_put_contents(
        $jsonFile,
        json_encode($saveData, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
    );

    echo "<script>
        alert('✅ Configuración de demo guardada correctamente');
        window.location.href='" . basename($_SERVER["SCRIPT_NAME"]) . "';
    </script>";
    exit;
}
?>

<div class="container-fluid pt-4 px-4">
    <div class="row g-4">
        <div class="col-12">
            <div class="bg-secondary rounded h-100 p-4">

                <h2 class="mb-4">Configuración Demo</h2>

                <form method="post">

                    <div class="mb-3">
                        <label class="form-label">Usuario</label>
                        <input type="text"
                               name="username"
                               class="form-control"
                               value="<?= htmlspecialchars($jsonData['username'] ?? '') ?>"
                               placeholder="Usuario para demo">
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Password</label>
                        <input type="text"
                               name="password"
                               class="form-control"
                               value="<?= htmlspecialchars($jsonData['password'] ?? '') ?>"
                               placeholder="Contraseña para demo">
                    </div>

                    <div class="mb-3">
                        <label class="form-label">DNS</label>
                        <select name="dns_uid" class="form-select">
                            <option value="">Seleccionar DNS</option>
                            <?php foreach ($dns_list as $dns): ?>
                                <option value="<?= $dns['uid'] ?>"
                                    <?= isset($jsonData['dns_uid']) && $jsonData['dns_uid'] == $dns['uid'] ? 'selected' : '' ?>>
                                    <?= htmlspecialchars($dns['title']) ?> ➤ <?= htmlspecialchars($dns['url']) ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Duración del Demo</label>
                        <select name="demo_duration" class="form-select">
                            <option value="3600"  <?= ($jsonData['demo_duration'] ?? '') == 3600 ? 'selected' : '' ?>>1 Hora</option>
                            <option value="7200"  <?= ($jsonData['demo_duration'] ?? '') == 7200 ? 'selected' : '' ?>>2 Horas</option>
                            <option value="10800" <?= ($jsonData['demo_duration'] ?? '') == 10800 ? 'selected' : '' ?>>3 Horas</option>
                            <option value="86400" <?= ($jsonData['demo_duration'] ?? '') == 86400 ? 'selected' : '' ?>>1 Día</option>
                        </select>
                    </div>

                    <!--<div class="mb-3">
                        <label class="form-label">Link / QR de Activación</label>
                        <input type="text"
                               name="payUrl"
                               class="form-control"
                               value="<?= htmlspecialchars($jsonData['payUrl'] ?? '') ?>"
                               placeholder="Opcional: link de pago o QR">
                    </div>-->

                    <div class="mb-3">
                        <label class="form-label">Estado</label>
                        <select name="status" class="form-select">
                            <option value="Activo" <?= ($jsonData['status'] ?? '') == 'Activo' ? 'selected' : '' ?>>Activo</option>
                            <option value="Desactivado" <?= ($jsonData['status'] ?? '') == 'Desactivado' ? 'selected' : '' ?>>Desactivado</option>
                        </select>
                    </div>

                    <button type="submit" name="submit_demo" class="btn btn-primary">
                        Guardar Configuración
                    </button>

                </form>

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

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