<?php
require_once __DIR__ . '/../includes/auth.php';
require_once __DIR__ . '/../includes/functions.php';
$u = require_login();
$pdo = get_db();

$jobId = (int)($_GET['job_id'] ?? 0);
$stmt = $pdo->prepare('SELECT * FROM jobs WHERE id = ?');
$stmt->execute([$jobId]);
$job = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$job) { die('Job not found.'); }

$stmt = $pdo->prepare('SELECT * FROM applications WHERE job_id = ? ORDER BY applied_at DESC');
$stmt->execute([$jobId]);
$apps = $stmt->fetchAll(PDO::FETCH_ASSOC);
$csrf = csrf_token();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Applicants — <?= h($job['title']) ?></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
<link href="assets/admin.css" rel="stylesheet">
</head>
<body>
<?php include __DIR__ . '/includes/topbar.php'; ?>
<div class="admin-wrap">
  <a href="index.php" class="small">&larr; Back to all jobs</a>
  <h5 class="mt-2 mb-3">Applicants for: <?= h($job['title']) ?> <span class="text-muted">(<?= count($apps) ?>)</span></h5>

  <?php if (isset($_GET['msg'])): ?>
    <div class="alert alert-success py-2"><?= h($_GET['msg']) ?></div>
  <?php endif; ?>

  <div class="card-plain p-3">
    <?php if (!$apps): ?>
      <p class="text-muted mb-0">No applications received yet for this job.</p>
    <?php else: ?>
    <div class="table-responsive">
    <table class="admin-table">
      <thead><tr><th>Name</th><th>Contact</th><th>Applied</th><th>Cover Note</th><th>Files</th><th>Actions</th></tr></thead>
      <tbody>
      <?php foreach ($apps as $a): ?>
        <tr>
          <td><?= h($a['applicant_name']) ?></td>
          <td><?= h($a['email']) ?><?= $a['phone'] ? '<br><span class="text-muted small">' . h($a['phone']) . '</span>' : '' ?></td>
          <td class="small text-muted"><?= h($a['applied_at']) ?></td>
          <td>
            <button type="button" class="btn btn-sm btn-outline-gold" data-bs-toggle="modal" data-bs-target="#coverNoteModal<?= (int)$a['id'] ?>">
              <i class="fas fa-eye"></i> View
            </button>
          </td>
          <td>
            <?php if ($a['cv_path']): ?>
              <a class="btn btn-sm btn-outline-gold mb-1" href="download.php?type=cv&id=<?= (int)$a['id'] ?>"><i class="fas fa-file-arrow-down"></i> CV</a>
            <?php endif; ?>
            <?php if ($a['doc_path']): ?>
              <a class="btn btn-sm btn-outline-gold mb-1" href="download.php?type=doc&id=<?= (int)$a['id'] ?>"><i class="fas fa-file-arrow-down"></i> Other Doc</a>
            <?php endif; ?>
          </td>
          <td>
            <?php if ($u['role'] === 'superadmin'): ?>
              <form method="post" action="applicant-delete.php" onsubmit="return confirm('Delete this application permanently? This will also remove the uploaded CV/documents. This cannot be undone.');">
                <input type="hidden" name="csrf" value="<?= h($csrf) ?>">
                <input type="hidden" name="id" value="<?= (int)$a['id'] ?>">
                <input type="hidden" name="job_id" value="<?= (int)$jobId ?>">
                <button type="submit" class="btn btn-sm btn-outline-danger"><i class="fas fa-trash"></i></button>
              </form>
            <?php endif; ?>
          </td>
        </tr>

        <!-- Cover note modal -->
        <div class="modal fade" id="coverNoteModal<?= (int)$a['id'] ?>" tabindex="-1" aria-hidden="true">
          <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
              <div class="modal-header">
                <h6 class="modal-title">Cover Note — <?= h($a['applicant_name']) ?></h6>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
              </div>
              <div class="modal-body" style="white-space:pre-wrap;line-height:1.6">
                <?= $a['cover_note'] ? nl2br(h($a['cover_note'])) : '<span class="text-muted">No cover note provided.</span>' ?>
              </div>
            </div>
          </div>
        </div>
      <?php endforeach; ?>
      </tbody>
    </table>
    </div>
    <?php endif; ?>
  </div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
