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

    include 'include/database.php';

    $connection = connect($database);

    $canAdmin = canAdministrate($connection, $_SESSION['login']);
    $canEdit = canEditValues($connection, $_SESSION['login']);
    $canDelete = canDeleteValues($connection, $_SESSION['login']);

    if ($canAdmin) {
        $procs = getCommandProcessors($connection);
    } else {
        header( 'Location: /' );
        $connection = null;
        die;
    }

    $connection = null;

    // TODO: get rid of boxWrap
    echo '
        <div id="boxWrap">
        <div id="pickTable">
            <div>
                <label for="executable">New Command Processor:</label>
                <input id="executable">
            </div>

            <button onclick="saveCmdProc()">Save</button>
            <button onclick="resetExe()">Clear</button>
        </div>
    ';
    
    $proc_table = "
        <table class=\"center\">
        
            <tr>
                <th></th>
                <th>Command Processor</th>
            </tr>
    ";
    
    foreach ($procs as $proc) {
        $proc_table .= "
            <tr title=\"{$proc['id']}\">
                <td>
                    <button onclick=\"deleteExe({$proc['id']}, '{$proc['name']}')\">Delete</button>
                    <button onclick=\"editExe({$proc['id']}, '{$proc['name']}')\">Edit</button>
                </td>
                <td id=\"executable\">{$proc['name']}</td>
            </tr>
        ";
    }
    
    $proc_table .= "
        </table>
        </div>
    ";
    
    echo $proc_table;
?>