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

    include 'include/database.php';

    $connection = connect($database);

    $canControl = canControlSystems($connection, $_SESSION['login']);

    $room_id = getRoomId($connection, $_GET['campus'], $_GET['building'], $_GET['room']);

    if ($canControl) {
        $buttons = getCommandButtons($connection, $room_id);
    } else {
        $buttons = array();
    }

    $connection = null;

    if ($canControl) {
        $current_group = null;
        $button_html = '
            <div id="buttons">
        ';
       
        foreach ($buttons as $button) {
            if ($button['group_name'] !== $current_group) {
                $current_group = $button['group_name'];

                $button_html .= "
                <h2 class=\"pad-top\">{$button['group_name']}</h2>
                ";
            }
            
            $button_html .= "
                <button onclick=\"cmdBtnClick({$button['id']})\" title=\"{$button['id']}\">{$button['name']}</button>
            ";
        }

        $button_html .= '
            </div>
        ';
        
        echo $button_html;
    } else {
        echo '<strong class="warning">You do not have permission to use these yet! Please email the administrator of this system for assistance...</strong>';
    }
?>