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

$jobs = $pdo->query("
  SELECT j.*, (SELECT COUNT(*) FROM applications a WHERE a.job_id = j.id) AS app_count,
         u.name AS creator_name
  FROM jobs j LEFT JOIN users u ON u.id = j.created_by
  ORDER BY j.created_at DESC
")->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Jobs — Delmas Careers Admin</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">
  <div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="mb-0">Job Posts</h5>
    <a href="job-form.php" class="btn btn-gold btn-sm"><i class="fas fa-plus me-1"></i> New Job Post</a>
  </div>

  <?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 (!$jobs): ?>
      <p class="text-muted mb-0">No job posts yet. Click "New Job Post" to add one.</p>
    <?php else: ?>
    <div class="table-responsive">
    <table class="admin-table">
      <thead><tr><th>Title</th><th>Posted By</th><th>Deadline</th><th>Status</th><th>Applicants</th><th>Actions</th></tr></thead>
      <tbody>
      <?php foreach ($jobs as $job):
          $expired = is_job_expired($job['deadline']);
      ?>
        <tr>
          <td>
            <strong><?= h($job['title']) ?></strong><br>
            <span class="text-muted small"><?= h($job['department']) ?><?= $job['location'] ? ' · ' . h($job['location']) : '' ?></span>
          </td>
          <td class="small text-muted"><?= h($job['creator_name'] ?? '—') ?></td>
          <td><?= format_date_pretty($job['deadline']) ?></td>
          <td>
            <?php if ($job['status'] !== 'published'): ?>
              <span class="badge badge-draft">Draft</span>
            <?php elseif ($expired): ?>
              <span class="badge badge-closed">Closed</span>
            <?php else: ?>
              <span class="badge badge-open">Open</span>
            <?php endif; ?>
          </td>
          <td><a href="applicants.php?job_id=<?= (int)$job['id'] ?>"><?= (int)$job['app_count'] ?> applicant<?= $job['app_count'] == 1 ? '' : 's' ?></a></td>
          <td>
            <a href="job-form.php?id=<?= (int)$job['id'] ?>" class="btn btn-sm btn-outline-gold">Edit</a>
            <a href="job-delete.php?id=<?= (int)$job['id'] ?>" class="btn btn-sm btn-outline-danger"
               onclick="return confirm('Delete this job post and all its applications? This cannot be undone.');">Delete</a>
          </td>
        </tr>
      <?php endforeach; ?>
      </tbody>
    </table>
    </div>
    <?php endif; ?>
  </div>
</div>
</body>
</html>
