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

    include 'include/database.php';

    $connection = connect($database);

    $canViewTable = canViewTables($connection, $_SESSION['login']);
    $canViewLink = canViewLinks($connection, $_SESSION['login']);
    $canEdit = canEditValues($connection, $_SESSION['login']);
    $canDelete = canDeleteValues($connection, $_SESSION['login']);
    $canReset = canResetAlerts($connection, $_SESSION['login']);

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

    if ($canViewTable) {
        $properties = getRoomProperties($connection, $room_id);
    } else {
        $properties = array();
    }

    $connection = null;

    echo "
            <h2>{$_GET['campus']}/{$_GET['building']}/{$_GET['room']}</h2>
    ";

    if ($canViewTable) {
        $propertyTable = '
            <table class="center">
        ';

        $current_device = null;

        foreach ($properties as $property) {
            if ($property['device'] !== $current_device) {
                $current_device = $property['device'];

                $propertyTable .= "
                <tr>
                    <th title=\"{$property['device_id']}\">
                        <button onclick=\"deleteDevice({$property['device_id']}, '{$property['device']}')\"";

                if (!$canDelete) {
                    $propertyTable .= ' disabled';
                }

                $propertyTable .= ">del</button>
                        <input id=\"{$property['device_id']}\" oninput=\"deviceBlurred({$property['device_id']})\" value=\"{$property['device']}\" type=\"text\"";

                if (!$canEdit) {
                    $propertyTable .= ' disabled';
                }

                $propertyTable .= '/>
                    </th>
                    <th>Property</th>
                    <th>Prefix</th>
                    <th>Value</th>
                    <th>Suffix</th>
                    <th>Alert Config</th>
                    <th>Alert Value</th>
                    <th>Type</th>
                    <th>Alert Type</th>
                    <th>Capitalize</th>
                    <th>Allow Reset</th>
                    <th>Reset</th>
                </tr>
                ';
            }

            $propertyTable .= "
                <tr>
                    <td title=\"{$property['id']}\"";

            if ($property['alert_status'] == True && $property['alert_color'] !== null) {
                $propertyTable .= " style=\"background-color:{$property['alert_color']};\"";
            }

            $propertyTable .= ">
                        <button onclick=\"deleteProperty({$property['id']}, {$property['device_id']}, '{$property['device']}', '{$property['name']}')\"";

            if (!$canDelete) {
                $propertyTable .= ' disabled';
            }

            $propertyTable .= ">del</button>
                        Status
                    </td>
                    <td><input id=\"{$property['id']}ALTNAME\" oninput=\"propChange('ALTNAME', {$property['id']})\" value=\"{$property['name']}\" type=\"text\"";

            if (!$canEdit) {
                $propertyTable .= ' disabled';
            }

            $propertyTable .= "></input></td>
                    <td><input id=\"{$property['id']}PREFIX\" oninput=\"propChange('PREFIX', {$property['id']})\" value=\"{$property['prefix']}\" type=\"text\"";
                    
            if (!$canEdit) {
                $propertyTable .= ' disabled';
            }

            $propertyTable .= "></input></td>
                    <td>";

            if ($canViewLink && $property['type'] === 'LINK') {
                $propertyTable .= "<a href=\"{$property['value']}\" target=\"_blank\">{$property['value']}</a>";
            } else if ($canViewLink && $property['type'] === 'TELNET') {
                $propertyTable .= "<a href=\"telnet://{$property['value']}\">{$property['value']}</a>";
            } else {
                $propertyTable .= "{$property['value']}";
            }
                    
            $propertyTable .= "</td>
                    <td><input id=\"{$property['id']}SUFFIX\" oninput=\"propChange('SUFFIX', {$property['id']})\" value=\"{$property['suffix']}\" type=\"text\"";
                    
            if (!$canEdit) {
                $propertyTable .= ' disabled';
            }

            $propertyTable .= "></input></td>
                    <td><input id=\"{$property['id']}ALERTCONFIG\" oninput=\"propChange('ALERTCONFIG', {$property['id']})\" value=\"{$property['alert_config']}\" type=\"text\"";
                    
            if (!$canEdit) {
                $propertyTable .= ' disabled';
            }

            $propertyTable .= "></input></td>
                    <td><input id=\"{$property['id']}ALERTVALUE\" oninput=\"propChange('ALERTVALUE', {$property['id']})\" value=\"{$property['alert_value']}\" type=\"text\"";

            if (!$canEdit) {
                $propertyTable .= ' disabled';
            }

            $propertyTable .= "></input></td>
                    <td><input id=\"{$property['id']}TYPE\" oninput=\"propChange('TYPE', {$property['id']})\" value=\"{$property['type']}\" type=\"text\"";

            if (!$canEdit) {
                $propertyTable .= ' disabled';
            }

            $propertyTable .= "></input></td>
                    <td><input id=\"{$property['id']}ALERTTYPE\" oninput=\"propChange('ALERTTYPE', {$property['id']})\" value=\"{$property['alert_type']}\" type=\"text\"";

            if (!$canEdit) {
                $propertyTable .= ' disabled';
            }

            $propertyTable .= "></input></td>
                    <td><input id=\"{$property['id']}CAPITALIZE\" onchange=\"handleCheck(this, {$property['id']}, 'CAPITALIZE')\" type=\"checkbox\"";

            if ($property['capitalize']) {
                $propertyTable .= ' checked';
            }
            if (!$canEdit) {
                $propertyTable .= ' disabled';
            }

            $propertyTable .= "></input></td>
                    <td><input id=\"{$property['id']}ALLOWRESET\" onchange=\"handleCheck(this, {$property['id']}, 'ALLOWRESET')\" type=\"checkbox\"";

            if ($property['allow_reset']) {
                $propertyTable .= ' checked';
            }
            if (!$canEdit) {
                $propertyTable .= ' disabled';
            }

            $propertyTable .= "></input></td>
                    <td><button id=\"{$property['id']}\" onclick=\"resetAlert({$property['id']})\"";

            if (!$canReset) {
                $propertyTable .= " disabled";
            }

            $propertyTable .= ">Reset</button></td>
                </tr>
            ";
        }

        $propertyTable .= '
            </table>
        ';

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