<?php
header('Content-Type: application/json; charset=utf-8');
require_once 'config/database.php';

$type = isset($_GET['type']) ? strtolower($_GET['type']) : '';

try {
    // Accepter les types en minuscule et en pluriel
    if ($type === 'retrait' || $type === 'retraits') {
        $stmt = $pdo->query("SELECT NomPrenom, Montant, Date_Retrait FROM cahier_des_charges ORDER BY Date_Retrait DESC");
    } elseif ($type === 'depot' || $type === 'depots') {
        $stmt = $pdo->query("SELECT NomPrenom, Date_Depot FROM cahier_des_charges WHERE Date_Depot IS NOT NULL ORDER BY Date_Depot DESC");
    } else {
        echo json_encode(['status' => 'ERR', 'message' => 'Type inconnu']);
        exit;
    }

    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
    echo json_encode(['status' => 'OK', 'data' => $rows]);

} catch (PDOException $e) {
    echo json_encode(['status' => 'ERR', 'message' => $e->getMessage()]);
}
?>