#!/apps/bin/perl
#
# $Id: status.pl,v 1.4 2006/02/14 22:05:24 marki Exp $
##########################################################################
#   Brief Discription:
#
#   execution:  status
#
#    creates a html table of maintenance jobs ordered by those needed to
#    get performed next
#
#   created:  Fri Apr 17 22:00:20 EDT 1998
#
#   author of the script: arne 
#   
##########################################################################
use Time::CTime;
use CGI qw(:all);
use DBI;

$cgi_query = new CGI;
print $cgi_query->header(-expires=>'+3d');
print $cgi_query->start_form(-encoding=>'multipart/form-data');
print $cgi_query->start_html(-title=>'Hall D Maintenance Status',
				 -BGCOLOR=>'#bbbbbb');
# Changed to localhost by M. Wise, 3/4/2015 after switch to halldweb
#$hostname = "halldweb1.jlab.org";
$hostname = "localhost";
$database = "maintenance";
$user = "maintainer";
$password = "";
$dbh = DBI->connect("DBI:mysql:$database:$hostname", $user, $password);

$time_fmt = '%D';

$sql_cmd = "SELECT task, email1, email2, next_maintenance, last_maintenance, period FROM maintenance ORDER BY task";

do_sql();

$ntasks = 0;
@task = ();
@email1 = ();
@email2 = ();
@next_maintenance = ();
@last_maintenance = ();
@period = ();
while (@column = $sth->fetchrow_array) {
    $task[$ntasks] = $column[0];
    $email1[$ntasks] = $column[1];
    $email2[$ntasks] = $column[2];
    $next_maintenance[$ntasks] = $column[3];
    $last_maintenance[$ntasks] = $column[4];
    $period[$ntasks] = $column[5];
    $ntasks++;
}

&PAGE_HEADER;

&TABLE_HEADER;

for ($i=0; $i<$ntasks; $i++) {
    $per = $period[$i]/86400.;
    $last_day = strftime($time_fmt,(localtime($last_maintenance[$i]))); 
    $next_day = strftime($time_fmt,(localtime($next_maintenance[$i]))); 

    $task_nospaces = $task[$i];
    $task_nospaces =~ s/ /\%20/g;
    $task_nospaces =~ s/#/\%23/g;
    print $cgi_query->p("<TR>");
    print $cgi_query->p("<TD> <a href=long_task.pl?task=$task_nospaces> $task[$i] </a></TD>");
    print $cgi_query->p("<TD> $email1[$i] </TD>");
    print $cgi_query->p("<TD> $email2[$i] </TD>");
    print $cgi_query->p("<TD> $per </TD>");
    print $cgi_query->p("<TD> $next_day </TD>");
    print $cgi_query->p("<TD> <a href=update.pl?task=$task_nospaces>$last_day </a></TD>");
    print $cgi_query->p("</TR>");
}

&TABLE_END;

sub TABLE_HEADER {

    print $cgi_query->p("\<TABLE BORDER COLS=6 WIDTH=\"100\%\" NOSAVE\>");
    print $cgi_query->p("\<TR\>");
    print $cgi_query->p("\<TD ALIGN=\"LEFT\"\> TASK \<\/TD\>");
    print $cgi_query->p("\<TD ALIGN=\"LEFT\"\> Primary Contact \<\/TD\>");
    print $cgi_query->p("\<TD ALIGN=\"LEFT\"\> Secondary Contact \<\/TD\>");
    print $cgi_query->p("\<TD ALIGN=\"LEFT\"\> PERIOD (days) \<\/TD\>");
    print $cgi_query->p("\<TD ALIGN=\"LEFT\"\> NEXT MAINTENANCE\<\/TD\>");
    print $cgi_query->p("\<TD ALIGN=\"LEFT\"\> LAST MAINTENANCE\<\/TD\>");
    print $cgi_query->p("\<\/TR\>");
}

sub TABLE_END {
    print $cgi_query->p("</TABLE>");
}

sub PAGE_HEADER {
    print $cgi_query->p("Click the <B>Last Maintenance</B> date to update when
the last maintenance was performed");
    print $cgi_query->p("Click the <B>Task</B> to view the long description of the task");
    print $cgi_query->p("<a href=new_task.pl> Click here to add a new item to the Hall D maintenance maintainer </a>");
}

sub do_sql {
    $sth = $dbh -> prepare($sql_cmd)
        or die "Can't prepare $sql_cmd: $dbh->errstr\n";
    $rv = $sth -> execute
        or die "Can't execute the query $sql\n error: $sth->errstr\n";
    return;
}
