connect_error) { echo json_encode(["success" => false, "message" => "Erreur connexion DB: " . $conn->connect_error]); exit(); } // Récup paramètre $userId = $_GET['user_id'] ?? null; if (!$userId) { echo json_encode(["success" => false, "message" => "ID utilisateur manquant"]); exit(); } // Fonction jours ouvrés function getWorkingDays($startDate, $endDate) { $workingDays = 0; $current = new DateTime($startDate); $end = new DateTime($endDate); while ($current <= $end) { $dayOfWeek = (int)$current->format('N'); if ($dayOfWeek < 6) { $workingDays++; } $current->modify('+1 day'); } return $workingDays; } try { // Requête multi-types $query = " SELECT dc.Id, dc.DateDebut, dc.DateFin, dc.Statut, dc.DateDemande, dc.Commentaire, dc.Validateur, dc.DocumentJoint, GROUP_CONCAT(tc.Nom ORDER BY tc.Nom SEPARATOR ', ') AS TypeConges FROM DemandeConge dc JOIN TypeConge tc ON FIND_IN_SET(tc.Id, dc.TypeCongeId) WHERE dc.EmployeeId = ? GROUP BY dc.Id, dc.DateDebut, dc.DateFin, dc.Statut, dc.DateDemande, dc.Commentaire, dc.Validateur, dc.DocumentJoint ORDER BY dc.DateDemande DESC "; $stmt = $conn->prepare($query); if (!$stmt) { throw new Exception("Erreur préparation SQL : " . $conn->error); } $stmt->bind_param("i", $userId); $stmt->execute(); $result = $stmt->get_result(); $requests = []; while ($row = $result->fetch_assoc()) { $workingDays = getWorkingDays($row['DateDebut'], $row['DateFin']); // Format dates $startDate = new DateTime($row['DateDebut']); $endDate = new DateTime($row['DateFin']); $submittedDate = new DateTime($row['DateDemande']); $dateDisplay = ($row['DateDebut'] === $row['DateFin']) ? $startDate->format('d/m/Y') : $startDate->format('d/m/Y') . ' - ' . $endDate->format('d/m/Y'); // Lien fichier si congé maladie $fileUrl = null; if (strpos($row['TypeConges'], 'Congé maladie') !== false && !empty($row['DocumentJoint'])) { $fileUrl = 'http://localhost/GTA/project/uploads/' . basename($row['DocumentJoint']); } $requests[] = [ 'id' => (int)$row['Id'], 'type' => $row['TypeConges'], // ex: "Congé payé, RTT" 'startDate' => $row['DateDebut'], 'endDate' => $row['DateFin'], 'dateDisplay' => $dateDisplay, 'days' => $workingDays, 'status' => $row['Statut'], 'reason' => $row['Commentaire'] ?: 'Aucun commentaire', 'submittedAt' => $row['DateDemande'], 'submittedDisplay' => $submittedDate->format('d/m/Y'), 'validator' => $row['Validateur'] ?: null, 'fileUrl' => $fileUrl ]; } echo json_encode([ "success" => true, "message" => "Demandes récupérées avec succès", "requests" => $requests, "total" => count($requests) ]); } catch (Exception $e) { echo json_encode([ "success" => false, "message" => "Erreur: " . $e->getMessage() ]); } $conn->close();