<?php
  require 'api.php';
  //$date = "2023-02-06";
  switch ($_SERVER['REQUEST_METHOD'])
  {
    case 'GET':
      $noAtelier = $_GET["NoAtelier"];
      $request = "SELECT Id, HeureDebut, HeureFin FROM atelier_heures1 WHERE 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_heures1 (NoAtelier, HeureDebut) VALUES (:noatelier, :heuredebut)";
	  $statement = Connection::getStatement($request);
	  $statement->bindParam(":noatelier", $data->NoAtelier);
	  $statement->bindParam(":heuredebut", $data->HeureDebut);
	  $statement->execute();
	  echo Connection::getConnection()->lastInsertId();
	break;
	case 'PUT':
	  $json = file_get_contents('php://input');
	  $data = json_decode($json);
	  $first = true;
	  $request = "UPDATE atelier_heures1 SET";
	  if (isset($data->HeureDebut))
	  {
		  $first = false;
		  $request .= " HeureDebut = '".$data->HeureDebut."'";
	  }
	  if (isset($data->HeureFin))
	  {
		  if (!$first)
		  {
			$request .= ",";  
		  }
		  $first = false;
		  $request .= " HeureFin = '".$data->HeureFin."'";
	  }
	  $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_heures1 WHERE Id = :id";
	  $statement = Connection::getStatement($request);
	  $statement->bindParam(":id", $id);
	  $statement->execute();
	break;
  }
?>