<?php
header('Content-Type: application/json');
include "../db.php";

// Disable warnings/notices from leaking into JSON
error_reporting(E_ERROR | E_PARSE);
ini_set('display_errors', 0);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $title = $_POST['title'] ?? '';
    $type = $_POST['type'] ?? '';
    $experience = $_POST['experience'] ?? '';
    $salary = $_POST['salary'] ?? '';
    $location = $_POST['location'] ?? '';
    $description = $_POST['description'] ?? '';
    $requirements = json_encode(explode("\n", trim($_POST['requirements'] ?? '')));

    $sql = "INSERT INTO job_listing (title, type, experience, salary, location, description, requirements)
            VALUES ('$title', '$type', '$experience', '$salary', '$location', '$description', '$requirements')";

    if ($conn->query($sql)) {
        echo json_encode([
            "status" => "success",
            "message" => "Job added successfully"
        ]);
    } else {
        echo json_encode([
            "status" => "error",
            "message" => "Database error: " . $conn->error
        ]);
    }
    $conn->close();
    exit;
}
?>
