<?php
// Include the database connection
include('dbconnect.php');

// Check if the 'id' parameter is provided in the URL
if (isset($_GET['id']) && !empty($_GET['id'])) {
    $applicant_id = $_GET['id'];  // Get the applicant ID from the query string

    // SQL query to get the applicant details
    $sql = "SELECT position, job_country, reference FROM job_applicants WHERE id = ?";

    // Prepare the SQL statement
    if ($stmt = $con->prepare($sql)) {
        // Bind the parameter (applicant_id)
        $stmt->bind_param("i", $applicant_id);

        // Execute the query
        $stmt->execute();

        // Store the result and check if we have a match
        $stmt->store_result();

        if ($stmt->num_rows > 0) {
            // Bind the result variables
            $stmt->bind_result($position, $job_country, $reference);
            $stmt->fetch();

            // Return the data as JSON
            echo json_encode([
                'position' => $position,
                'job_country' => $job_country,
                'reference' => $reference
            ]);
        } else {
            // If no data found for the given applicant id
            echo json_encode(['error' => 'Applicant not found']);
        }

        // Close the statement
        $stmt->close();
    } else {
        // If there is an error with the query
        echo json_encode(['error' => 'Failed to prepare the query']);
    }
} else {
    // If no applicant id is provided
    echo json_encode(['error' => 'No applicant id provided']);
}

// Close the database connection
$con->close();
?>
