<?php
include 'includes/db.php';

$result = $conn->query("
    SELECT * 
    FROM jobs 
    WHERE status='open' 
    ORDER BY created_at DESC
");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Career Opportunities | Sigal Laboratories</title>

<style>
.container {
  max-width: 1000px;
  margin: 40px auto;
  padding: 0 20px;
  font-family: 'Segoe UI', Tahoma, sans-serif;
}

h2 {
  text-align: center;
  margin-bottom: 30px;
}

.job-table {
  width: 100%;
  border-collapse: collapse;
}

.job-table th, .job-table td {
  text-align: left;
  padding: 12px 8px;
  border-bottom: 1px solid #ddd;
  vertical-align: top;
}

.job-table th {
  background-color: #f4f4f4;
  font-weight: bold;
}

.job-status {
  padding: 4px 12px;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
}

.job-status.open {
  background: #e6f7fb;
  color: #25A9C2;
}

.job-status.expired {
  background: #fdecea;
  color: #c0392b;
}

.apply-btn {
  display: inline-block;
  background: #25A9C2;
  color: #fff;
  padding: 6px 14px;
  border-radius: 6px;
  text-decoration: none;
  font-weight: 600;
  margin-top: 4px;
}

.apply-btn:hover {
  background: linear-gradient(90deg, #005cb9, #003b8e);
}

.no-jobs {
  text-align: center;
  color: #666;
  background: #f8f9fb;
  padding: 40px;
  border-radius: 8px;
  margin-top: 20px;
}

/* Responsive Table */
@media (max-width: 768px) {
  .job-table, .job-table th, .job-table td {
    display: block;
    width: 100%;
  }

  .job-table th {
    background: none;
    font-weight: bold;
  }

  .job-table td {
    border: none;
    padding: 6px 0;
  }

  .job-table tr {
    margin-bottom: 20px;
    border-bottom: 1px solid #ddd;
  }
}
</style>
</head>

<body>

<?php include 'includes/header.php'; ?>

<div class="container">
  <h2>Current Job Openings</h2>

<?php if ($result && $result->num_rows > 0): ?>

  <table class="job-table">
    <thead>
      <tr>
        <th>Title</th>
        <th>Department</th>
        <th>Deadline</th>
        <th>Description</th>
        <th>Vacant Number</th>
        <th>Status / Action</th>
      </tr>
    </thead>
    <tbody>
      <?php while($row = $result->fetch_assoc()): ?>
        <?php
          $today = date('Y-m-d');
          $isExpired = (!empty($row['deadline']) && $row['deadline'] < $today);
          
          // Prepare description safely, keep HTML
          $description = $row['description'];
          if (strlen(strip_tags($description)) > 120) {
              $description = strip_tags($description); // remove HTML tags for truncating
              $description = mb_substr($description, 0, 120) . '...';
          }
        ?>
        <tr>
          <td><?= htmlspecialchars($row['title']); ?></td>
          <td><?= htmlspecialchars($row['department']); ?></td>
          <td><?= !empty($row['deadline']) ? htmlspecialchars($row['deadline']) : '-'; ?></td>
          <td><?= $description; ?></td>
          <td><?= htmlspecialchars($row['vacantnumber']); ?></td>
          <td>
            <?php if($isExpired): ?>
              <span class="job-status expired">Expired</span>
            <?php else: ?>
              <span class="job-status open">Open</span>
              <br>
              <a href="apply.php?id=<?= (int)$row['id']; ?>" class="apply-btn">Apply Now</a>
            <?php endif; ?>
          </td>
        </tr>
      <?php endwhile; ?>
    </tbody>
  </table>

<?php else: ?>

  <div class="no-jobs">
    <h3>No job openings are currently available.</h3>
    <p>Please check back later for new opportunities at Sigal Laboratories.</p>
  </div>

<?php endif; ?>

</div>

</body>
</html>


<?php include 'includes/footer.php'; ?>
