<?php
// api_get_events.php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
require_once 'config.php';

try {
    $sql = "SELECT * FROM evenement 
            WHERE statut IN ('a_venir', 'en_cours') 
            ORDER BY date_debut ASC";
    $stmt = $pdo->query($sql);
    $events = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    foreach ($events as &$event) {
        $event['date_debut_formatee'] = date('d/m/Y', strtotime($event['date_debut']));
        $event['image_url'] = !empty($event['image']) ? 'uploads/events/' . $event['image'] : '';
    }
    
    echo json_encode(['success' => true, 'events' => $events]);
} catch(Exception $e) {
    echo json_encode(['success' => false, 'events' => [], 'message' => $e->getMessage()]);
}
?>