<?PHP
    include "include/header.inc";

    include "include/pgsql.inc";
    $db = new DB_Pgsql;

    // get a list of services for displayed hosts..
    $query = "select distinct service.service_id, service.service_name from service, host_services, host where service.service_id=host_services.service_id and host_services.enabled='Y' and host.host_id=host_services.host_id and host.enabled='Y' order by service.service_id ";
    $db->query($query);
    $servicecount = 0;
    while ($db->next_record()) {
        $services[$servicecount++] = $db->f("service_id");
        $service_names[$db->f("service_id")] = $db->f("service_name");
    }
    $db->disconnect();
    $query = "select host_id, host_name from host where enabled='Y' order by host_id";
    $db->query($query);
    $hostcount = 0;
    while ($db->next_record()) {
        $hosts[$hostcount++] = $db->f("host_id");
        $host_names[$db->f("host_id")] = $db->f("host_name");
    }
    $db->disconnect();

    // Get last update time/date..
    $sql = "select log_datetime from host_log order by log_datetime desc limit 1";
    $db->query($sql);
    $db->next_record();
    $last_update = $db->f("log_datetime");
    $db->disconnect();
    
?>

    <div align="right">Last Update: <?PHP echo date("r", $last_update/1000); ?></div>
    
    <table border=0 align="center">
        <tr>
            <td>&nbsp;</td>
<?PHP
    for ($i=0; $i < $servicecount; $i++) {
        echo "<th>".$service_names[$services[$i]]."</th>\n";
    }

?>
        </tr>
<?PHP
    for ($i=0; $i < $hostcount; $i++) {
        echo "<tr><th>".$host_names[$hosts[$i]]."</th>\n";
        // Find each service
        for ($j=0; $j < $servicecount; $j++) {
            $query = "select * from host_statuschange where host_id=".$db->prepInt($hosts[$i])." and service_id=".$db->prepInt($services[$j])." order by log_datetime desc limit 1 offset 0";
            $db->query($query);
            if ($db->next_record()) {
                echo "<td align=\"center\"><a href=\"detail.php?host_id=".$hosts[$i]."&service_id=".$services[$j]."\">";
                switch ($db->f("log_newstatus")) {
                    case 1: echo "<img border=0 src=\"images/green.gif\">";
                            break;
                    case 2: echo "<img border=0 src=\"images/orange.gif\">";
                            break;
                    case 3: echo "<img border=0 src=\"images/red.gif\">";
                            break;
                }
                echo "</a></td>\n";

            } else {
                echo "<td align=\"center\">-</td>";
            }

            $db->disconnect();
        }

        echo "</tr>\n";
    }
?>
    </table>


<?PHP
  include "include/footer.inc";
?>
