<?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) {
        $room_id = getRoomIdCG($connection, $_GET['campus'], $_GET['building'], $_GET['room']);
        $devices = getDevicesCG($connection, $room_id);
    } else {
        $devices = array();
    }
    
    $devices_table = '
        <table id="devices">
    ';
    
    foreach ($devices as $device) {
        $properties = getDevicePropertiesCG($connection, $device['id']);
        $num_properties = count($properties);
        $rowspan = $num_properties + 1; // add one for header

        $devices_table .= "
            <tr>
                <th title=\"{$device['id']}\" rowspan=\"$rowspan\">
                    <input type=\"text\" id=\"{$device['id']}\" value=\"{$device['caption']}\" onblur=\"updateDeviceCaption({$device['id']})\" />
                    <button onclick=\"delDevice({$device['id']})\">Delete Device</button>
                    <button onclick=\"newProp({$device['id']})\">New Property</button>
                </th>
                <th>Tracker</th>
                <th>Property</th>
                <th>Variable Name\Index</th>
                <th>Initial Val</th>
                <th>Type</th>
                <th>Prefix</th>
                <th>Suffix</th>
                <th>Alert Config</th>
                <th>Alert Val</th>
                <th>Alert Type</th>
                <th>Allow Reset</th>
                <th>Capitalize</th>
            </tr>
        ";
        
        foreach($properties as $property) {
            $trackers = array("CHANNEL", "LEVEL", "NUMBER", "STRING");
            $types = array("INFO", "LINK", "NUMBER", "SOURCE", "STRING", "SUM", "TELNET");
            $alertconfigs = array("<", "<=", "=", "!=", ">=", ">");
        
            $translations = getPropertyTranslationsCG($connection, $property['id']);
            $num_translations = count($translations);
        
            $devices_table .= "
            <tr title=\"{$property['id']}\">
                <td>
                    <button onclick=\"delProp({$property['id']})\">Delete</button>
                    <button onclick=\"getTranslations({$property['id']})\">Trans ($num_translations)</button>
                    <input type=\"text\" id=\"PropCaption{$property['id']}\" onblur=\"updateProperty('{$property['id']}', 'NAME', 'PropCaption{$property['id']}')\" value=\"{$property['name']}\" />
                </td>

                <td>
                    <select id=\"PropTracker{$property['id']}\" onchange=\"updateProperty('{$property['id']}', 'TRACKER', 'PropTracker{$property['id']}')\">
                        <option></option>";
            
            foreach ($trackers as $tracker) {
                $devices_table .= "
                        <option";
                              
                if ($property['tracker'] === $tracker) {
                    $devices_table .= " selected";
                }
                                
                $devices_table .= ">$tracker</option>";
            }
            
            $devices_table .= "
                    </select>
                </td>
                
                <td>
                    <input type=\"text\" id=\"PropNameIndex{$property['id']}\" onblur=\"updateProperty('{$property['id']}', 'ALTNAME', 'PropNameIndex{$property['id']}')\" value=\"{$property['altname']}\" />
                </td>
                
                <td>
                    <input type=\"text\" id=\"PropInitialValue{$property['id']}\" onblur=\"updateProperty('{$property['id']}', 'VALUE', 'PropInitialValue{$property['id']}')\" value=\"{$property['value']}\" />
                </td>
                
                <td>
                    <select id=\"PropType{$property['id']}\" onchange=\"updateProperty('{$property['id']}', 'TYPE', 'PropType{$property['id']}')\">
                        <option></option>";

            foreach ($types as $type) {
                $devices_table .= "
                        <option";

                if ($property['type'] === $type) {
                    $devices_table .= " selected";
                }

                $devices_table .= ">$type</option>";
            }
                                
            $devices_table .= "
                    </select>
                </td>
                
                <td>
                    <input type=\"text\" id=\"PropPrefix{$property['id']}\" onblur=\"updateProperty('{$property['id']}', 'PREFIX', 'PropPrefix{$property['id']}')\" value=\"{$property['prefix']}\" />
                </td>
                
                <td>
                    <input type=\"text\" id=\"PropSuffix{$property['id']}\" onblur=\"updateProperty('{$property['id']}', 'SUFFIX', 'PropSuffix{$property['id']}')\" value=\"{$property['suffix']}\" />
                </td>
                
                <td>
                    <select id=\"PropAlertConfig{$property['id']}\" onchange=\"updateProperty('{$property['id']}', 'ALERTCONFIG', 'PropAlertConfig{$property['id']}')\">
                        <option></option>";

            foreach ($alertconfigs as $alert_config) {
                $devices_table .= "
                        <option";
                                
                if ($property['alert_config'] === $alert_config) {
                    $devices_table .= " selected";
                }
                                
                $devices_table .= ">$alert_config</option>";
            }
                                
            $devices_table .= "
                    </select>
                </td>
                
                <td>
                    <input type=\"text\" id=\"PropAlertValue{$property['id']}\" onblur=\"updateProperty('{$property['id']}', 'ALERTVALUE', 'PropAlertValue{$property['id']}')\" value=\"{$property['alert_value']}\" />
                </td>
                
                <td>
                    <input type=\"text\" id=\"PropAlertType{$property['id']}\" onblur=\"updateProperty('{$property['id']}', 'ALERTTYPE', 'PropAlertType{$property['id']}')\" value=\"{$property['alert_type']}\" />
                </td>
				
                <td>
                    <input type=\"checkbox\" id=\"PropAllowReset{$property['id']}\" onchange=\"setPropAllowReset('{$property['id']}', 'PropAllowReset{$property['id']}')\"";
				
            if ($property['allow_reset']) {
                $devices_table .= ' checked';
            }
            
            $devices_table .= ">
				</td>
                
                <td>
                    <input type=\"checkbox\" id=\"PropCapitalize{$property['id']}\" onchange=\"setPropCapitalize('{$property['id']}', 'PropCapitalize{$property['id']}')\"";
				
            if ($property['capitalize']) {
                $devices_table .= ' checked';
            }
            
            $devices_table .= ">
				</td>
            </tr>
            ";
        }
    }
    
    $devices_table .= '
        </table>
    ';
    
    $connection = null;
    
    echo $devices_table;
?>