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

$nomPrenom = isset($_REQUEST['nom_prenom']) ? trim($_REQUEST['nom_prenom']) : '';
$montant = isset($_REQUEST['montant']) ? trim($_REQUEST['montant']) : '';
$userRet = isset($_REQUEST['user_ret']) ? trim($_REQUEST['user_ret']) : '';

if (empty($nomPrenom) || empty($montant) || empty($userRet)) {
    die("ERR|Donnees manquantes");
}

try {
    // Verifier si le nom existe deja
    $checkStmt = $pdo->prepare("SELECT COUNT(*) FROM cahier_des_charges WHERE NomPrenom = ?");
    $checkStmt->execute([$nomPrenom]);
    if ($checkStmt->fetchColumn() > 0) {
        die("ERR|Ce nom existe deja");
    }

    $stmt = $pdo->prepare("INSERT INTO cahier_des_charges (NomPrenom, NomPrenomAR, Date_Retrait, Montant, Date_Depot, UserRet, UserDep, SiteID) VALUES (?, NULL, CURDATE(), ?, NULL, ?, NULL, 'GR09')");
    $stmt->execute([$nomPrenom, $montant, $userRet]);

    echo "OK|Retrait effectue";
} catch (PDOException $e) {
    die("ERR|Erreur SQL: " . $e->getMessage());
}
?>