<?php
    session_start();
	if ( empty($_SESSION['login']) ) {
        header( 'Location: login.php' );
        die;
	}

    include 'include/database.php';

    $connection = connect($database);

    $canDelete = canDeleteValues($connection, $_SESSION['login']);
    $canEdit = canEditValues($connection, $_SESSION['login']);
    
    $building_id = getBuildingId($connection, $_GET['campus'], $_GET['building']);
    $rooms = getRoomNames($connection, $building_id);

    $connection = null;
    
	if (isset($_GET['select_all']) && $_GET['select_all'] === 'true') {
		array_unshift($rooms, array('name'=>'Select All'));
	} else {
        $_GET['select_all'] = 'false';
    }
    
	if (!isset($_GET['size'])) {
		$_GET['size'] = 15;
	}
	
    $room_options = '';
    
	if (isset($_GET['editable']) && $_GET['editable'] == True) { 
		$room_options .= '
			<button type="button" onclick="deleteRoom()"';
			
		if (!$canDelete) {
			$room_options .= ' disabled';
		}
		
		$room_options .= '>del</button>
		';
		
		$room_options .= '
			<button type="button" onclick="editRoom()"';
			
		if (!$canEdit) {
			$room_options .= ' disabled';
		}
			
		$room_options .= '>edit</button>
		';
	}

    $selected = array_shift($rooms);
    
    $room_options .= "
        <select id=\"roomList\" onchange=\"selectClick('roomList', {$_GET['select_all']})\" size=\"{$_GET['size']}\">
            <option value=\"{$selected['name']}\" selected>{$selected['name']}</option>";
    
    foreach ($rooms as $room) {
        $room_options .= "
            <option value=\"{$room['name']}\">{$room['name']}</option>
        ";
    }
    
    $room_options .= '
        </select>
    ';
    
    echo $room_options;
?>