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

$id = (int)($_GET['id'] ?? 0);
if ($id) {
    // remove uploaded files belonging to this job's applications
    $stmt = $pdo->prepare('SELECT cv_path, doc_path FROM applications WHERE job_id = ?');
    $stmt->execute([$id]);
    foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
        foreach (['cv_path', 'doc_path'] as $col) {
            if (!empty($row[$col])) {
                $p = UPLOAD_DIR . basename($row[$col]);
                if (is_file($p)) @unlink($p);
            }
        }
    }
    $pdo->prepare('DELETE FROM jobs WHERE id = ?')->execute([$id]);
}
header('Location: index.php?msg=' . urlencode('Job post deleted.'));
exit;
