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

    include 'include/database.php';

    $connection = connect($database);

    $canAdmin = canAdministrate($connection, $_SESSION['login']);

    if ($canAdmin) {
        $selectAll = 'Select All';
    
        $campus_id = ($_GET['campus'] === $selectAll) ? 0 : getCampusId($connection, $_GET['campus']);
        $building_id = ($_GET['building'] === $selectAll) ? 0 : getBuildingId($connection, $_GET['campus'], $_GET['building']);
        $room_id = ($_GET['room'] === $selectAll) ? 0 : getRoomId($connection, $_GET['campus'], $_GET['building'], $_GET['room']);
    
        $tasks = getTasks($connection, $campus_id, $building_id, $room_id);
    } else {
        header( 'Location: /' );
        $connection = null;
        die;
    }

    $connection = null;
    
    $task_table = "
        <table class=\"center\">
        
            <tr>
                <th>Task</th>
                <th>Group</th>
                <th>Button</th>
                <th>Activated</th>
                <th>Options</th>
            </tr>
    ";
    
    foreach ($tasks as $task) {
        $task_table .= "
            <tr title=\"{$task['id']}\">
                <td>{$task['name']}</td>
                <td>{$task['button_group']}</td>
                <td>{$task['button_name']}</td>
                <td>{$task['activated']}</td>
                <td><button onclick=\"deleteTask({$task['id']})\">Delete</button></td>
            </tr>
        ";
    }
    
    $task_table .= "
        </table>
    ";
    
    echo $task_table;
?>