<?php require_once __DIR__ . '/includes/admin_bootstrap.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  $items = ugfx_read_json($ugfx['storage']['chat_schedule'], []);
  $items[] = [
    'id' => time(),
    'title' => trim($_POST['title'] ?? ''),
    'message' => trim($_POST['message'] ?? ''),
    'links' => array_values(array_filter(array_map('trim', explode(PHP_EOL, $_POST['links'] ?? '')))),
    'interval_minutes' => (int) ($_POST['interval_minutes'] ?? 60),
    'enabled' => isset($_POST['enabled']),
    'last_sent' => null
  ];
  ugfx_write_json($ugfx['storage']['chat_schedule'], $items);
}
$schedules = ugfx_read_json($ugfx['storage']['chat_schedule'], []);
$adminTitle='Chat Tools'; include __DIR__ . '/includes/admin_header.php'; ?>
<div class="row g-4">
  <div class="col-lg-6"><div class="ugfx-card p-4"><div class="ugfx-micro-label">SCHEDULED CHAT PROMOS</div><h3>Create repeating promo</h3><form method="post" class="row g-3"><div class="col-12"><input class="form-control" name="title" placeholder="Title"></div><div class="col-12"><textarea class="form-control" name="message" rows="4" placeholder="Promo/system message"></textarea></div><div class="col-12"><textarea class="form-control" name="links" rows="3" placeholder="One link per line"></textarea></div><div class="col-md-6"><input class="form-control" type="number" name="interval_minutes" value="60" min="5"></div><div class="col-md-6 d-flex align-items-center"><div class="form-check"><input class="form-check-input" type="checkbox" name="enabled" checked id="enabled"><label class="form-check-label" for="enabled">Enabled</label></div></div><div class="col-12"><button class="btn ugfx-btn-primary">Save promo schedule</button></div></form></div></div>
  <div class="col-lg-6"><div class="ugfx-card p-4"><div class="ugfx-micro-label">CURRENT SCHEDULES</div><h3>Queued promos</h3><?php foreach ($schedules as $item): ?><div class="ugfx-card p-3 mb-3"><strong><?= htmlspecialchars($item['title'] ?? 'Promo') ?></strong><div class="text-secondary small">Every <?= (int) ($item['interval_minutes'] ?? 60) ?> minutes · <?= !empty($item['enabled']) ? 'Enabled' : 'Disabled' ?></div><div class="mt-2 text-secondary"><?= htmlspecialchars($item['message'] ?? '') ?></div><?php if (!empty($item['links'])): ?><div class="mt-2"><?php foreach ($item['links'] as $link): ?><div><a href="<?= htmlspecialchars($link) ?>" target="_blank" rel="noopener"><?= htmlspecialchars($link) ?></a></div><?php endforeach; ?></div><?php endif; ?></div><?php endforeach; ?></div></div>
</div>
<?php include __DIR__ . '/includes/admin_footer.php'; ?>
