<?php
	require_once '../../autoload.php';

	$result = array (
			"status"	=> 'KO',
			"results"	=> -1
	);
	
	$pdo = getPDO ();
		
	if ( $pdo )
	{
		$room_id 	= -1;
		$dep_id 	= -1;
		$name 		= "";
		
		if ( isset($_POST['room_id']) )		$room_id 	= $_POST['room_id'];
		if ( isset($_POST['dep_id']) )		$dep_id 	= $_POST['dep_id'];
		if ( isset($_POST['name']) )		$name 		= $_POST['name'];

		error_log ( "room_id : " .$room_id );

		$s0 = null;

		if ( $room_id == -1 )
		{
			$sql = "insert into room ( dep_id, name ) values (:dep_id, 'New room') returning room_id;";

			$s0 = $pdo->prepare ( $sql );

			$s0->bindParam ( ':dep_id', 	$dep_id 	);
		}
		else
		{
			$sql = 'update room';
 			$sql .= ' set name = :name';
			$sql .= ' where room_id = :room_id';

			$s0 = $pdo->prepare ( $sql );

			$s0->bindParam ( ':room_id', 	$room_id 	);
			$s0->bindParam ( ':name', 		$name 		);
		}	
		
		if ( $s0->execute () == true )
		{
			$result['status'] = 'OK';
			if ( $room_id == -1 )
				$result['results'] = $s0->fetchColumn ();
			else
				$result['results'] = $room_id;
		}
	}
	
	header('Content-type: application/json');
	
	echo json_encode ( $result );
?>