<?php
require_once("include/dbcommon.php"); // DataBase PHPRunner

// Require our Event class and datetime utilities
require 'fullcalendar/php/utils.php';


$language = $_SESSION['language'];
$search = '';
If ( isset($_SESSION['q_IN']) && $_SESSION['q_IN'] <> '' ) {  // Are you coming from Room Search?
	$search = ' and rooms.idrooms IN ('.$_SESSION['q_IN'].') ';
}

$sql = "
SELECT idrooms, code, color, catalog_code 
FROM rooms
join (
SELECT catalog_num, catalog_code
FROM catalog
join super_catalog on ( super_catalog_idsuper_catalog = idsuper_catalog
AND super_code = 'TYPE_ROOM')
where language = '$language'
) T2
where catalog_num = type $search 
order by catalog_code, code
";


$resql = db_query($sql,$conn);
// Accumulate an output array of event data arrays.
// $output_arrays = array();
$str = '[';

$group = '';
while ($data = db_fetch_array($resql)){
 
    If ($group <> $data['catalog_code']) {
        if ($group <> '' ) {
            $str = substr($str,0,strlen($str)-1); // erase last ","
            $str .= '  ] },';
        }
        $str .= '{ "id": "'.$data['catalog_code'].'", "title": "'.$data['catalog_code'].'", "children": [';
        $group = $data['catalog_code'];
        
    } 
    $str .= '{ "id": "'.$data['code'].'", "title": "'.$data['code'].'", "eventColor": "'.$data['color'].'" },';
    
}
$str = substr($str,0,strlen($str)-1); // erase last ","
$str .= '  ] } ]';

echo $str;
// Send JSON to the client.
// $str = json_encode($output_arrays);
// echo json_encode($output_arrays);

?>