<?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') {
    $id = $_POST['id'] ?? '';
    $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'] ?? '')));

    if (empty($id)) {
        echo json_encode([
            "status" => "error",
            "message" => "Missing job ID"
        ]);
        exit;
    }

    $sql = "UPDATE job_listing 
            SET title='$title', 
                type='$type', 
                experience='$experience', 
                salary='$salary', 
                location='$location', 
                description='$description', 
                requirements='$requirements'
            WHERE id=$id";

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

    $conn->close();
    exit;
}
?>
