<?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']);
    
    $campuses = getCampusNames($connection);

	$connection = null;
	
    if (isset($_GET['select_all']) && $_GET['select_all'] === 'true') {
		array_unshift($campuses, array('name'=>'Select All'));
	} else {
        $_GET['select_all'] = 'false';
    }
	if (!isset($_GET['size'])) {
		$_GET['size'] = 3;
	}
    
    $campus_options = '';
    
	if (isset($_GET['editable']) && $_GET['editable'] == True) {
		$campus_options .= '
			<button type="button" onclick="deleteCampus()"';
			
		if (!$canDelete) {
			$campus_options .= ' disabled';
		}
		
		$campus_options .= '>del</button>
		';
		
		$campus_options .= '
			<button type="button" onclick="editCampus()"';
			
		if (!$canEdit) {
			$campus_options .= ' disabled';
		}
			
		$campus_options .= '>edit</button>
		';
	}
    
    $selected = array_shift($campuses);
	
    $campus_options .= "
        <select id=\"campusList\" onchange=\"selectClick('campusList', {$_GET['select_all']})\" size=\"{$_GET['size']}\">
            <option value=\"{$selected['name']}\" selected>{$selected['name']}</option>";
    
    foreach ($campuses as $campus) {
        $campus_options .= "
            <option value=\"{$campus['name']}\">{$campus['name']}</option>
        ";
    }
    
    $campus_options .= '
        </select>
    ';
    
    echo $campus_options;
?>