<?php
session_start();
require __DIR__ . '/includes/admin_lib.php';
include __DIR__ . '/includes/admin_header.php';
$settingsFile = __DIR__ . '/../data/settings.json';
$settings = smurf_load_settings($settingsFile);
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  if (!$CAN_EDIT) die('<h1>Access Denied</h1>');
  $settings['player_show_in_nav'] = !empty($_POST['player_show_in_nav']);
  $settings['player_use_worker'] = !empty($_POST['player_use_worker']);
  $settings['player_worker_url'] = trim((string)($_POST['player_worker_url'] ?? ''));
  smurf_save_settings($settingsFile, $settings);
  smurf_redirect_self(['saved'=>1]);
}
?>
<section class="content-card">
  <h1 class="admin-title">🪟 Pop Out Player</h1>
  <?php if (!empty($_GET['saved'])): ?><div class="msg success">Saved.</div><?php endif; ?>
  <section class="content-card" style="text-align:left; margin-top:12px;">
    <form method="post" class="form">
      <label style="display:block;margin:10px 0;"><input type="checkbox" name="player_show_in_nav" <?= !empty($settings['player_show_in_nav']) ? 'checked':''; ?>> Show Pop Out Player in navigation</label>
      <label style="display:block;margin:10px 0;"><input type="checkbox" name="player_use_worker" <?= !empty($settings['player_use_worker']) ? 'checked':''; ?>> Use Cloudflare Worker HTTPS stream in the player</label>
      <label>Cloudflare Worker Stream URL</label>
      <input class="input-field" type="text" name="player_worker_url" placeholder="https://your-worker.your-subdomain.workers.dev" value="<?= smurf_h($settings['player_worker_url'] ?? '') ?>">
      <p style="opacity:.82;margin:10px 0 0;">For HTTPS pages, the built-in player needs an HTTPS stream. Keep the raw HTTP stream for the external link, and paste your Worker URL here for the embedded player.</p>
      <?php if ($CAN_EDIT): ?><button class="admin-btn primary" type="submit">Save Settings</button><?php endif; ?>
      <a class="admin-btn" href="/popout-player.php" target="_blank" rel="noopener" style="margin-left:8px;">Open Player</a>
    </form>
  </section>
  <section class="content-card" style="text-align:left; margin-top:16px;">
    <h3 style="margin-top:0;">Cloudflare Worker Setup</h3>
    <ol style="padding-left:18px;">
      <li>Create a Cloudflare Worker and paste the file from <code>/cloudflare/stream-worker.js</code>.</li>
      <li>Set the Worker route or use the workers.dev URL.</li>
      <li>Paste that HTTPS URL above and enable <strong>Use Cloudflare Worker HTTPS stream</strong>.</li>
    </ol>
  </section>
</section>
<?php include __DIR__ . '/includes/admin_footer.php'; ?>
