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

$dns_list = $db->select('dns', 'id, title, url', '', 'title ASC');

$jsonFile = __DIR__ . "/api/config_test.json";
$jsonData = [];
if (file_exists($jsonFile)) {
    $jsonData = json_decode(file_get_contents($jsonFile), true);
}

if (isset($_POST['submit_directo'])) {
    $jsonSave = [
        'username'      => $_POST['username'] ?? '',
        'password'      => $_POST['password'] ?? '',
        'dns_url'       => $_POST['dns_url'] ?? '',
        'status'        => $_POST['status'] ?? 'Desactivado',
        'demo_duration' => intval($_POST['demo_duration'] ?? 3600)
    ];

    if (file_put_contents($jsonFile, json_encode($jsonSave, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES))) {
        echo "<script>alert('✅ Demo configuration saved successfully.'); window.location.href='" . basename($_SERVER["SCRIPT_NAME"]) . "';</script>";
    } else {
        echo "<script>alert('❌ Error saving config_test.json. Check api/ folder permissions.'); window.location.href='" . basename($_SERVER["SCRIPT_NAME"]) . "';</script>";
    }
    exit;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Demo Configuration</title>
</head>
<body>
<div class="page-container" style="padding:20px;">
    <div class="card-custom" style="background:#1a1a1a; padding:30px; border-radius:15px; color:white; max-width:600px; margin:auto;">
        <h2 style="border-bottom:1px solid #444; padding-bottom:10px;">⚡ Configure Auto Demo</h2>
        <form method="post" style="margin-top:20px;">
            <div class="form-group" style="margin-bottom:15px;">
                <label>👤 IPTV Username (Demo)</label>
                <input class="form-control" name="username" type="text" value="<?= $jsonData['username'] ?? '' ?>" required style="width:100%; padding:10px; margin-top:5px;">
            </div>
            <div class="form-group" style="margin-bottom:15px;">
                <label>🔑 IPTV Password (Demo)</label>
                <input class="form-control" name="password" type="text" value="<?= $jsonData['password'] ?? '' ?>" required style="width:100%; padding:10px; margin-top:5px;">
            </div>
            <div class="form-group" style="margin-bottom:15px;">
                <label>🌐 Select DNS</label>
                <select class="form-control" name="dns_url" required style="width:100%; padding:10px; margin-top:5px;">
                    <option value="">Select a DNS</option>
                    <?php foreach ($dns_list as $dns): ?>
                        <option value="<?= $dns['url'] ?>" <?= isset($jsonData['dns_url']) && $jsonData['dns_url'] == $dns['url'] ? 'selected' : '' ?>>
                            <?= $dns['title'] ?> (<?= $dns['url'] ?>)
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="form-group" style="margin-bottom:15px;">
                <label>⏳ Demo Duration</label>
                <select class="form-control" name="demo_duration" required style="width:100%; padding:10px; margin-top:5px;">
                    <option value="3600"  <?= isset($jsonData['demo_duration']) && $jsonData['demo_duration'] == 3600 ? 'selected' : '' ?>>1 Hour</option>
                    <option value="10800" <?= isset($jsonData['demo_duration']) && $jsonData['demo_duration'] == 10800 ? 'selected' : '' ?>>3 Hours</option>
                    <option value="21600" <?= isset($jsonData['demo_duration']) && $jsonData['demo_duration'] == 21600 ? 'selected' : '' ?>>6 Hours</option>
                    <option value="86400" <?= isset($jsonData['demo_duration']) && $jsonData['demo_duration'] == 86400 ? 'selected' : '' ?>>24 Hours</option>
                </select>
            </div>
            <div class="form-group" style="margin-bottom:20px;">
                <label>📊 Status</label>
                <select class="form-control" name="status" required style="width:100%; padding:10px; margin-top:5px;">
                    <option value="Activo" <?= isset($jsonData['status']) && $jsonData['status'] == 'Activo' ? 'selected' : '' ?>>Active ✅</option>
                    <option value="Desactivado" <?= isset($jsonData['status']) && $jsonData['status'] == 'Desactivado' ? 'selected' : '' ?>>Disabled ❌</option>
                </select>
            </div>
            <button class="btn btn-primary" name="submit_directo" type="submit" style="width:100%; padding:15px; font-weight:bold;">💾 Save Configuration</button>
        </form>
    </div>
</div>
<?php include('includes/footer.php'); ?>
</body>
</html>