<?php
header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');

require_once __DIR__.'/db_connect.php';

// accepte GET ou POST
$agence = trim($_REQUEST['agence'] ?? '');
$dstNum = trim($_REQUEST['dstNum'] ?? ''); // ← corrige le nom du paramètre

if ($agence === '' || $dstNum === '') {
    echo json_encode(['success'=>false,'message'=>'agence ou dstNum manquant']);
    exit;
}

try {
    $sql = "UPDATE tbldocstocks 
            SET dst_sync_ack = 1, 
                dstDtReception = NOW(),  
	            dstRespReception ='$agence', 
                dst_sync_date = NOW(), 
                dst_sync_par ='$agence'
            WHERE dstNum = '$dstNum' "; // évite de compter 2 fois

    $stmt = $pdo->prepare($sql);
   // $stmt->bindValue(':par', $agence);
   // $stmt->bindValue(':dst', $dstNum);
    $stmt->execute();

    $nb = $stmt->rowCount(); // nombre de lignes affectées

    echo json_encode([
        'success' => $nb > 0,
        'updated' => $nb,
        'dstNum'  => $dstNum,
        'req' =>$sql
    ], JSON_UNESCAPED_UNICODE);

} catch (Exception $e) {
    http_response_code(500);
    echo json_encode(['success'=>false,'message'=>$e->getMessage()]);
}