<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization");

if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    http_response_code(200);
    exit();
}

header("Content-Type: application/json");

$host = "192.168.0.4";
$dbname = "DemandeConge";
$username = "wpuser";
$password = "-2b/)ru5/Bi8P[7_";

$conn = new mysqli($host, $username, $password, $dbname);
if ($conn->connect_error) {
    die(json_encode(["success" => false, "message" => "Erreur DB : " . $conn->connect_error]));
}

// Récupérer l'ID
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($id <= 0) {
    echo json_encode(["success" => false, "message" => "ID collaborateur invalide"]);
    exit;
}

try {
    $stmt = $conn->prepare("
        SELECT id, Nom, Prenom, Email, Matricule, Telephone, Adresse
        FROM CollaborateurAD 
        WHERE id = ? AND Actif = 1
    ");
    $stmt->bind_param("i", $id);
    $stmt->execute();
    $result = $stmt->get_result();
    $employee = $result->fetch_assoc();

    if ($employee) {
        echo json_encode(["success" => true, "employee" => $employee]);
    } else {
        echo json_encode(["success" => false, "message" => "Collaborateur non trouvé"]);
    }
} catch (Exception $e) {
    echo json_encode(["success" => false, "message" => "Erreur DB: " . $e->getMessage()]);
}

$conn->close();
?>
