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

    include 'include/database.php';

    $connection = connect($database);
    
    $canView = canViewTables($connection, $_SESSION['login']);
    $canReset = canResetAlerts($connection, $_SESSION['login']);
    
    if ($canView) {
        $alerts = getAlerts($connection);
    } else {
        $alerts = array();
    }
    
    $connection = null;

    if ($canView) {
        $hotlist = '
            <table id="hotList" class="center">
                <tr>
                    <th>Campus</th>
                    <th>Building</th>
                    <th>Room</th>
                    <th>Device</th>
                    <th>Property</th>
                    <th>Timestamp</th>
                    <th>Value</th>
                    <th>Alert Type</th>
                    <th>Reset</th>
                </tr>
        ';
        
        foreach ($alerts as $alert) {
            $hotlist .= "
                <tr title=\"{$alert['id']}\">
                    <td style=\"background-color:{$alert['color']};\">{$alert['campus']}</td>
                    <td>{$alert['building']}</td>
                    <td>{$alert['room']}</td>
                    <td>{$alert['device']}</td>
                    <td>{$alert['property']}</td>
                    <td>{$alert['last_updated']}</td>
                    <td>{$alert['prefix']}{$alert['value']}{$alert['suffix']}</td>
                    <td>{$alert['type']}</td>
            ";
            
            $hotlist .= "<td><button id=\"{$alert['id']}\" onclick=\"reset_alert({$alert['id']})\"";
            
            if (!$canReset) {
                $hotlist .= " disabled";
            }
            
            $hotlist .= ">Reset</button></td>
                </tr>
            ";
        }

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