<?php
require_once __DIR__ . '/guard.php';
require_login();

$file   = __DIR__ . '/../data/timers.json';
$timers = file_exists($file) ? json_decode(file_get_contents($file), true) : [];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    // Tilføj ny timer
    if (isset($_POST['add'])) {
        $text     = trim($_POST['text'] ?? '');
        $interval = max(1, (int)($_POST['interval'] ?? 15));
        $enabled  = isset($_POST['enabled']);

        if ($text) {
            $timers[] = [
                'text'     => $text,
                'interval' => $interval,
                'enabled'  => $enabled,
            ];
        }
    }

    // Gem eksisterende timers (bulk update)
    if (isset($_POST['save'])) {
        $new = [];
        $c   = (int)($_POST['count'] ?? 0);

        for ($i = 0; $i < $c; $i++) {
            $text     = $_POST['text_' . $i] ?? '';
            $interval = max(1, (int)($_POST['interval_' . $i] ?? 15));
            $enabled  = isset($_POST['enabled_' . $i]);

            if ($text) {
                $new[] = [
                    'text'     => $text,
                    'interval' => $interval,
                    'enabled'  => $enabled,
                ];
            }
        }

        $timers = $new;
    }

    // Slet alle
    if (isset($_POST['clear'])) {
        $timers = [];
    }

    file_put_contents($file, json_encode($timers, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
    header('Location: timers.php');
    exit;
}
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Timers</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
<div class="wrap">
    <div class="card">
        <h2>⏱️ Timers</h2>
        <p><a class="btn" href="index.php">← Tilbage</a></p>

        <!-- Tilføj ny timer -->
        <form method="post" style="margin-bottom:1rem">
            <label>
                Besked
                <input name="text" placeholder="Husk at følge kanalen 💜">
            </label>
            <br>
            <label>
                Interval (minutter)
                <input type="number" name="interval" min="1" value="15">
            </label>
            <br>
            <label>
                <input type="checkbox" name="enabled" checked>
                Aktiveret
            </label>
            <br>
            <button class="btn" name="add" value="1">Tilføj</button>
        </form>

        <!-- Redigér eksisterende -->
        <form method="post">
            <table>
                <tr>
                    <th>#</th>
                    <th>Besked</th>
                    <th>Interval</th>
                    <th>Aktiv</th>
                </tr>
                <?php foreach ($timers as $i => $t): ?>
                    <tr>
                        <td><?php echo $i + 1; ?></td>
                        <td>
                            <input
                                name="text_<?php echo $i; ?>"
                                value="<?php echo htmlspecialchars($t['text']); ?>"
                            >
                        </td>
                        <td>
                            <input
                                type="number"
                                min="1"
                                name="interval_<?php echo $i; ?>"
                                value="<?php echo (int)$t['interval']; ?>"
                            >
                        </td>
                        <td>
                            <input
                                type="checkbox"
                                name="enabled_<?php echo $i; ?>"
                                <?php echo !empty($t['enabled']) ? 'checked' : ''; ?>
                            >
                        </td>
                    </tr>
                <?php endforeach; ?>
            </table>

            <input type="hidden" name="count" value="<?php echo count($timers); ?>">
            <br>
            <button class="btn" name="save" value="1">Gem</button>
            <button class="btn" name="clear" value="1" onclick="return confirm('Slet alle?')">Slet alle</button>
        </form>
    </div>
</div>
</body>
</html>
