<?php
require_once __DIR__.'/../../../config/db.php';

$job = $conn->query("
  SELECT pj.id, v.numero, v.totalTTC, v.date_vente
  FROM print_jobs pj
  JOIN ventes v ON v.id=pj.vente_id
  WHERE pj.statut='pending'
  ORDER BY pj.id ASC
  LIMIT 1
")->fetch(PDO::FETCH_ASSOC);

if(!$job){
  echo json_encode(['status'=>'empty']);
  exit;
}

$conn->prepare("UPDATE print_jobs SET statut='printing' WHERE id=?")
     ->execute([$job['id']]);

echo json_encode(['status'=>'ok','job'=>$job]);
