<?php
session_start();
require __DIR__ . '/auth.php';
require __DIR__ . '/includes/admin_lib.php';

$file = __DIR__ . '/../data/applications.txt';
$action = $_GET['action'] ?? '';
$idx = isset($_GET['i']) ? (int)$_GET['i'] : -1;

if ($action === 'delete' && $idx >= 0) {
  if (!$CAN_EDIT) smurf_admin_denied('You do not have permission to delete entries.');
  smurf_delete_block($file, $idx);
  header('Location: /admin/applications.php');
  exit;
}

if ($action === 'clear') {
  if (!$CAN_EDIT) smurf_admin_denied('You do not have permission to clear entries.');
  smurf_clear_file($file);
  header('Location: /admin/applications.php');
  exit;
}

$items = smurf_parse_blocks($file);
include __DIR__ . '/includes/admin_header.php';
?>
<section class="content-card">
  <h1 class="admin-title">📝 Join Applications</h1>
  <p style="opacity:.8;">File: <code>/data/applications.txt</code></p>

  <div style="margin:10px 0 18px; display:flex; gap:10px; flex-wrap:wrap;">
    <?php if ($CAN_EDIT): ?>
      <a class="admin-btn" href="/admin/applications.php?action=clear" onclick="return confirm('Clear ALL entries?');">Clear All</a>
    <?php endif; ?>
    <a class="admin-btn" href="/admin/index.php">Back</a>
  </div>

  <div class="content-card" style="text-align:left;">
    <?php if (empty($items)): ?>
      <p>No applications yet.</p>
    <?php else: ?>
      <?php foreach ($items as $n => $item): ?>
        <div class="content-card" style="text-align:left; margin-bottom:14px;">
          <div style="display:flex; justify-content:space-between; gap:12px; align-items:flex-start; flex-wrap:wrap;">
            <div style="flex:1; min-width:320px;">
              <h3 style="margin:0 0 8px;"><?= smurf_h((string)($item['Name'] ?? 'Applicant')) ?></h3>
              <?php if (!empty($item['Email'])): ?><div><strong>Email:</strong> <?= smurf_h((string)$item['Email']) ?></div><?php endif; ?>
              <?php if (!empty($item['Role'])): ?><div><strong>Role:</strong> <?= smurf_h((string)$item['Role']) ?></div><?php endif; ?>
              <?php if (!empty($item['Experience'])): ?><div><strong>Experience:</strong> <?= smurf_h((string)$item['Experience']) ?></div><?php endif; ?>
              <?php if (!empty($item['Why'])): ?><div style="margin-top:10px;"><strong>Why they want to join:</strong><br><?= nl2br(smurf_h((string)$item['Why'])) ?></div><?php endif; ?>
              <?php if (!empty($item['Demo'])): ?><?php $demoName=(string)$item['Demo']; $demoUrl='/uploads/demos/' . rawurlencode($demoName); ?><div style="margin-top:10px;"><strong>Demo:</strong> <?= smurf_h($demoName) ?></div><div style="margin-top:8px; display:flex; gap:10px; flex-wrap:wrap; align-items:center;"><audio controls preload="none" src="<?= smurf_h($demoUrl) ?>"></audio><a class="admin-btn" href="<?= smurf_h($demoUrl) ?>" target="_blank" rel="noopener">Listen to Demo</a><a class="admin-btn" href="<?= smurf_h($demoUrl) ?>" download>Download Demo</a></div><?php endif; ?>
            </div>
            <?php if ($CAN_EDIT): ?>
              <div><a class="admin-btn" href="/admin/applications.php?action=delete&i=<?= (int)$n ?>" onclick="return confirm('Delete this application?');">Delete</a></div>
            <?php endif; ?>
          </div>
        </div>
      <?php endforeach; ?>
    <?php endif; ?>
  </div>
</section>
<?php include __DIR__ . '/includes/admin_footer.php'; ?>
