<?php
  require 'api.php';
  //$date = "2023-02-06";
  switch ($_SERVER['REQUEST_METHOD'])
  {
    case 'GET':
      $noAtelier = $_GET["NoAtelier"];
      $request = "SELECT ata.Id, ata.NoArticle, ata.Quantite, a.Description FROM atelier_article1 AS ata, articles AS a WHERE ata.NoArticle = a.NoArticleRR AND ata.NoAtelier = :noatelier";
      //echo $request;
		$statement = Connection::getStatement($query);
		$statement->bindParam(":noatelier", $noAtelier);
		$statement->execute();
		$emparray = $statement->fetchAll();
      //echo count($emparray);
      //print_r($emparray);
      $json = json_encode($emparray);
      //echo json_last_error_msg();
      echo $json;
	break;
	case 'POST':
	  $json = file_get_contents('php://input');
	  //echo $json;
	  $data = json_decode($json);
	  //print_r($data);
	  //echo "AAA";
	  $request = "INSERT INTO atelier_article1 (NoAtelier, NoArticle, Quantite) VALUES (:noatelier, :noarticle, :quantite)";
	  $statement = Connection::getStatement($request);
	  $statement->bindParam(":noatelier", $data->NoAtelier);
	  $statement->bindParam(":noarticle", $data->NoArticle);
	  $statement->bindParam(":quantite", $data->Quantite);
	  $statement->execute();
	  echo Connection::getConnection()->lastInsertId();
	  //echo $request;
	break;
	case 'PUT':
	  $json = file_get_contents('php://input');
	  $data = json_decode($json);
	  $first = true;
	  $request = "UPDATE atelier_article1 SET";
	  if (isset($data->NoArticle))
	  {
		  $first = false;
		  $request .= " NoArticle = '".$data->HeureDebut."'";
	  }
	  if (isset($data->Quantite))
	  {
		  if (!$first)
		  {
			$request .= ",";  
		  }
		  $first = false;
		  $request .= " Quantite = ".$data->Quantite;
	  }
	  $request .= " WHERE Id = " . $data->Id;
	  $statement = Connection::getStatement($request);
	  $statement->execute();
	  echo $request;
	  echo $data->Id;
	  //print_r($data);
	break;
	case 'DELETE':
      $id = $_GET["Id"];
	  $request = "DELETE FROM atelier_article1 WHERE Id = :id";
	  $statement = Connection::getStatement($request);
	  $statement->bindParam(":id", $id);
	  $statement->execute();
	break;
  }
?>