
<?php
session_start(); require __DIR__ . '/includes/header.php';
$settings = oldies_read_settings(); $chatFile = __DIR__ . '/../data/chat.json'; $data = admin_read_json($chatFile, []); if (!is_array($data)) $data = [];
foreach ($data as $k => $row) if (empty($data[$k]['id'])) $data[$k]['id'] = 'chat_' . substr(md5(($row['ts'] ?? time()) . '|' . ($row['name'] ?? '')),0,12);
admin_write_json($chatFile, $data);
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  if (isset($_POST['save_chat_settings']) && $CAN_EDIT) { $settings['chat_enabled'] = !empty($_POST['chat_enabled']); $settings['chat_show_in_nav'] = !empty($_POST['chat_show_in_nav']); oldies_save_settings($settings); admin_redirect_self(['saved'=>1]); }
  if (isset($_POST['clear_chat']) && $CAN_EDIT) { admin_write_json($chatFile, []); admin_redirect_self(['cleared'=>1]); }
  if (isset($_POST['delete_msg']) && $CAN_EDIT) { $deleteId = trim($_POST['delete_id'] ?? ''); $data = array_values(array_filter(admin_read_json($chatFile, []), fn($row) => (string)($row['id'] ?? '') !== $deleteId)); admin_write_json($chatFile, $data); admin_redirect_self(['deleted'=>1]); }
  if (isset($_POST['admin_send']) && $CAN_EDIT) { $msg = trim($_POST['msg'] ?? ''); $name = trim($_POST['name'] ?? 'Station Admin'); if ($msg !== '') { $data = admin_read_json($chatFile, []); if (!is_array($data)) $data = []; $data[] = ['id'=>'chat_' . substr(md5(uniqid('', true)),0,12),'role'=>'admin','name'=>$name,'msg'=>$msg,'time'=>date('H:i'),'ts'=>time()]; if (count($data)>200) $data = array_slice($data, -200); admin_write_json($chatFile, $data); } admin_redirect_self(['sent'=>1]); }
}
$modData = array_reverse(array_slice(admin_read_json($chatFile, []), -80));
?>
<section class="content-card"><h1 class="admin-title">💬 Live Chat</h1><?php foreach (['saved'=>'Saved.','cleared'=>'Chat cleared.','sent'=>'Message sent.','deleted'=>'Message deleted.'] as $k=>$v) if (!empty($_GET[$k])) echo '<div class="msg success">'.admin_h($v).'</div>'; ?><div class="page-builder-grid"><section class="content-card" style="text-align:left;"><form method="post"><input type="hidden" name="save_chat_settings" value="1"><label style="display:flex;gap:10px;align-items:center;margin:10px 0;"><input type="checkbox" name="chat_enabled" <?= !empty($settings['chat_enabled'])?'checked':'' ?>> Enable chat feature</label><label style="display:flex;gap:10px;align-items:center;margin:10px 0;"><input type="checkbox" name="chat_show_in_nav" <?= !empty($settings['chat_show_in_nav'])?'checked':'' ?>> Show chat in navigation</label><button class="admin-btn primary" type="submit" <?= !$CAN_EDIT ? 'disabled' : '' ?>>Save chat settings</button></form><div class="content-card chat-card" style="margin-top:16px;text-align:left;"><h3 style="margin-top:0;">Post as admin</h3><form method="post" class="form"><input type="hidden" name="admin_send" value="1"><label>Display name</label><input class="input-field" name="name" value="Oldies Admin"><label>Message</label><input class="input-field" name="msg" maxlength="240"><button class="admin-btn primary" type="submit" <?= !$CAN_EDIT ? 'disabled' : '' ?>>Send</button></form></div><?php if ($CAN_EDIT): ?><form method="post" onsubmit="return confirm('Clear chat for everyone?');" style="margin-top:16px;"><input type="hidden" name="clear_chat" value="1"><button class="admin-btn" type="submit">Clear Chat</button></form><?php endif; ?></section><section class="content-card" style="text-align:left;"><h3 style="margin-top:0;">Moderate messages</h3><?php if (!$modData): ?><p>No chat messages yet.</p><?php else: foreach ($modData as $row): ?><div class="chat-mod-row"><div class="chat-mod-copy"><div><strong><?= admin_h($row['name'] ?? 'Guest') ?></strong> <span style="opacity:.7;">(<?= admin_h($row['time'] ?? '') ?>)</span></div><div style="opacity:.92;margin-top:4px;"><?= admin_h($row['msg'] ?? '') ?></div></div><?php if ($CAN_EDIT): ?><form method="post" onsubmit="return confirm('Delete this message?');"><input type="hidden" name="delete_msg" value="1"><input type="hidden" name="delete_id" value="<?= admin_h($row['id'] ?? '') ?>"><button class="admin-btn" type="submit">Delete</button></form><?php endif; ?></div><?php endforeach; endif; ?></section></div></section>
<?php include __DIR__ . '/includes/footer.php'; ?>
