<?php require_once __DIR__ . '/includes/admin_bootstrap.php';
ugfx_require_permission('schedule');
$schedule = ugfx_read_json($ugfx['storage']['schedule'], []);
$message = '';
$days = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $newSchedule = [];
    foreach ($days as $day) {
        $lines = preg_split('/
|
|
/', (string)($_POST['day_'.$day] ?? ''));
        $lines = array_map(fn($v) => trim((string)$v), $lines);
        $lines = array_pad(array_slice($lines, 0, 24), 24, 'FX Vibe Radio Flow');
        $newSchedule[$day] = $lines;
    }
    ugfx_write_json($ugfx['storage']['schedule'], $newSchedule);
    $schedule = $newSchedule;
    $message = 'Schedule updated for all days.';
}
$adminTitle='Schedule'; include __DIR__ . '/includes/admin_header.php'; ?>
<?php if ($message): ?><div class="ugfx-admin-alert"><?= htmlspecialchars($message) ?></div><?php endif; ?>
<div class="ugfx-card p-4 mb-4"><div class="ugfx-micro-label">SCHEDULE CONTROL</div><h3>Editable weekly schedule</h3><p class="text-secondary mb-0">Each box should contain 24 lines, one per hour from 00:00 through 23:00. Any missing lines are automatically filled with <strong>FX Vibe Radio Flow</strong>.</p></div>
<form method="post" class="ugfx-admin-form ugfx-admin-schedule-grid">
  <?php foreach ($days as $day): ?>
    <div class="ugfx-card p-4"><div class="ugfx-micro-label"><?= htmlspecialchars($day) ?></div><textarea class="form-control" name="day_<?= htmlspecialchars($day) ?>"><?= htmlspecialchars(implode(PHP_EOL, (array)($schedule[$day] ?? []))) ?></textarea></div>
  <?php endforeach; ?>
  <div class="d-flex gap-2 flex-wrap"><button class="btn ugfx-btn-primary" type="submit">Save weekly schedule</button></div>
</form>
<?php include __DIR__ . '/includes/admin_footer.php'; ?>