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

<table border=0 align=center>

<?PHP

    include "include/commons.inc";
    include "include/pgsql.inc";
    $statuscodes[0] = "purple";
    $statuscodes[1] = "green";
    $statuscodes[2] = "orange";
    $statuscodes[3] = "red";

    $db = new DB_Pgsql;

    $query = "select host_name from host where host_id=".$db->prepInt($_REQUEST['host_id']);
    $db->query($query);
    $db->next_record();
    $hostname = $db->f("host_name");
    $db->disconnect();

    $query = "select service_name from service where service_id=".$db->prepInt($_REQUEST['service_id']);
    $db->query($query);
    $db->next_record();
    $servicename = $db->f("service_name");
    $db->disconnect();

?>

    <tr>
        <th align="center"><?PHP echo $hostname;?> - <?PHP echo $servicename;?></th>
    </tr>

    <tr>
        <td align="left" width=400>
<?PHP

    // Get the latest (or specified, if historical) host log entry
    $query = "select * from host_log where host_id=".$db->prepInt($_REQUEST['host_id'])." and service_id=".$db->prepInt($_REQUEST['service_id']);
    if (isset($log_datetime))
      $query .= " and log_datetime=".$db->prepInt($log_datetime);
    $query .= " order by log_datetime desc limit 1 offset 0";
    $db->query($query);
    $db->next_record();
    $host_logentry = $db->Record;
    $db->disconnect();
    // Get latest (or specified, if historical) statuslog entry
    $query = "select * from host_statuschange where host_id=".$db->prepInt($_REQUEST['host_id'])." and service_id=".$db->prepInt($_REQUEST['service_id']);
    if (isset($log_datetime))
      $query .= " and log_datetime=".$db->prepInt($log_datetime);
    $query .= " order by log_datetime desc limit 1 offset 0";
    $db->query($query);
    $db->next_record();
    $host_statuslogentry = $db->Record;
    $db->disconnect();

    // If we're looking at a historical record, find the next oldest statuschange entry..
    if (isset($log_datetime)) {
      $query = "select * from host_statuschange where host_id=".$db->prepInt($_REQUEST['host_id'])." and service_id=".$db->prepInt($_REQUEST['service_id']);
        $query .= " and log_datetime>".$db->prepInt($log_datetime);
      $query .= " order by log_datetime limit 1 offset 0";
      $db->query($query);
      if ($db->next_record())
        $host_nextstatuslogentry = $db->Record;
      $db->disconnect();
    }
    echo $statuscodes[$host_logentry["log_status"]]. date(" D M jS H:i:s", $host_logentry["log_datetime"]/1000)." - ".$host_logentry["log_title"]."<br><br>";

    switch ($host_logentry["log_status"]) {
        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 nl2br($host_logentry["log_text"]);

    echo "<hr>";

    if (isset($log_datetime) && isset($host_nextstatuslogentry))
      echo "<B>HOTE: This is a HISTORICAL ENTRY</b><br>Status unchanged for ".displayDuration(($host_nextstatuslogentry["log_datetime"]/1000) - ($host_statuslogentry["log_datetime"]/1000));
    else
      echo "Status unchanged in ".displayDuration(time() - ($host_statuslogentry["log_datetime"]/1000));
    echo "<form action=\"history.php\"><input type=hidden name=host_id value=".$_REQUEST['host_id']."><input type=hidden name=service_id value=".$_REQUEST['service_id']."><input type=submit name=submit value=\"History\"></form>";
?>
        </td>
    </tr>
</table>
<?PHP
  include "include/footer.inc";
?>
