<?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']);
    
    $campus_id = getCampusId($connection, $_GET['campus']);
    $buildings = getBuildingNames($connection, $campus_id);

    $connection = null;

	if (isset($_GET['select_all']) && $_GET['select_all'] === 'true') {
		array_unshift($buildings, array('name'=>'Select All'));
	} else {
        $_GET['select_all'] = 'false';
    }
    
	if (!isset($_GET['size'])) {
		$_GET['size'] = 10;
	}
    
    $building_options = '';

	if (isset($_GET['editable']) && $_GET['editable'] == True) {    
		$building_options .= '
			<button type="button" onclick="deleteBuilding()"';
			
		if (!$canDelete) {
			$building_options .= ' disabled';
		}
		
		$building_options .= '>del</button>
		';
		
		$building_options .= '
			<button type="button" onclick="editBuilding()"';
			
		if (!$canEdit) {
			$building_options .= ' disabled';
		}
			
		$building_options .= '>edit</button>
		';
	}
    
    $selected = array_shift($buildings);
    
    $building_options .= "
        <select id=\"buildingList\" onchange=\"selectClick('buildingList', {$_GET['select_all']})\" size=\"{$_GET['size']}\">
            <option value=\"{$selected['name']}\" selected>{$selected['name']}</option>";
    
    foreach ($buildings as $building) {
        $building_options .= "
            <option value=\"{$building['name']}\">{$building['name']}</option>
        ";
    }
    
    $building_options .= '
        </select>
    ';
    
    echo $building_options;
?>