package GUI::AlertReport;

use System;
use Util;
use Html;
use State;
use strict;

sub state {
  my($q) = @_;
  print Html->body(undef, { style => "<style type=\"text/css\">a:link {text-decoration:none; }</style>"});
  my $LB = Labels->read("GUI::AlertReport");

  print Html->header2("ROOT.details");
  print "
<script>
function win1(a) {
  var b = '$Http::WEBPROC?GO=GUI::Instr::rStart&Roption=2&file=' + a;
  var O = window.open(b,'al','menubar=no,resizable=yes,scrollbars=yes,width=450,height=600');
  O.focus();
}
</script>

   <table border=1 cellspacing=0 cellpadding=2 width=90% bgcolor=white>
     <tr><td></td>
         <th width=10%><img src=/gif/al_alert.gif alt=$LB->{warning}></th>
         <th width=10%><img src=/gif/al_crit.gif alt=$LB->{error}></th>
         <th width=10%><img src=/gif/al_down.gif alt=$LB->{down}></th>
     <tr bgcolor=#666699>
         <td ><font color=white><b>&nbsp;$LB->{devcat}</td>
         <th width=10%><font color=white>$LB->{dev_alerts}</td>
         <th width=10%><font color=white>$LB->{dev_alerts}</td>
         <th width=10%><font color=white>$LB->{dev_alerts}</td>
            ";
  my $L = GUI::AlertReport->state_(1);
  my $x;
  for ($x=0; $x <= $#$L; $x++) {
     my $l = $L->[$x];
     if ($l->[0]) {
       print "<tr><td colspan=4><small>$l->[1]</td>";
     } else {
       print "<tr><td colspan=4>&nbsp;</td>" if ($x > 0);
       print "<tr bgcolor=#F0F0F0><td>&nbsp;<b>$l->[1]</td><td width=10%><b><center>$l->[2]&nbsp;</td><td width=10%><b><center>$l->[3]&nbsp;</td><td width=10%><b><center>$l->[4]&nbsp;</td>";
     }
  }
  print "</table>";
  #print Html->readHtdocs("alertreport_footer");
  
}

sub state_ {
  my($class, $detail) = @_;
  my(@L);
  my $renv = System->get_renv();
  my $window = $renv->{window};
  
  my @MAP = (0,'<img src=/gif/al_alert3.gif>', 
               '<img src=/gif/al_crit3.gif>', 
               '<img src=/gif/al_down3.gif>',
             );

  my ($x);
  my $State = State->read();
  my $Comp  = $State->components();
  my (%A, %S, %B, %D);
  foreach my $el (keys %$Comp) {
     my $sev = $Comp->{$el}[0];
     my($type, $wwn, $rest) = split(/\:/, $el);
     if ($sev > $A{"$type:$wwn"}) {
        $A{"$type:$wwn"} = $sev;
     }
     if ($sev > 0) {
       $S{$type}[$sev]++ ;
       $D{$type}[$sev] .= "&nbsp;&nbsp;&nbsp;" . $MAP[$sev] . "&nbsp;&nbsp;" . 
              ($window eq "N" ? $Comp->{$el}[1] : 
                  "<a href=\"/instr_frame2.html&1=$Http::WEBPROC?GO=GUI::Instr::rStart&file=$type:$wwn\">" . $Comp->{$el}[1] . "</a>" )  . "<br>";
     } 
  }
#  javascript:win1('$type:$wwn')

  foreach my $el (keys %A) {
     my($type, $wwn) = split(/\:/, $el);
     my $sev = $A{$el};
     $B{$type}[$sev]++;
  }
  
  my $cnt = -1;
  foreach my $cat (sort keys %B) {
      my $cat0 = Util->abb("$cat.medium");
      my $warn = $B{$cat}[1];
      my $err  = $B{$cat}[2];
      my $da = " / ";
      my $down = $B{$cat}[3];
      if ($detail) {
        if ($S{$cat}[1] > 0) {
          $warn .= "0" if (!defined($warn));
          $warn .= "$da$S{$cat}[1]";
        }
        if ($S{$cat}[2] > 0) {
          $err .= "0" if (!defined($err));
          $err .= "$da$S{$cat}[2]" if ($S{$cat}[2] > 0);
        }
        if ($S{$cat}[3] > 0) {
          $down .= "0" if (!defined($down));
          $down .= "$da$S{$cat}[3]" if ($S{$cat}[3] > 0);
        }
      }
      push(@L, [0,$cat0, $warn, $err, $down]);
      $cnt++;
      if ($detail) {
        push(@L, [3,$D{$cat}[3]]) if ($D{$cat}[3]);
        push(@L, [2,$D{$cat}[2]]) if ($D{$cat}[2]);
        push(@L, [1,$D{$cat}[1]]) if ($D{$cat}[1]);
      }
  }
  return \@L;
}


  
  

1;
