#!/usr/bin/perl
#  Copyright 2001-2026 Leslie Richardson

#  This file is part of Open Admin for Schools.

#  Open Admin for Schools is free software; you can redistribute it 
#  and/or modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2 of 
#  the License, or (at your option) any later version.

my %lex = ('View' => 'View',
	   'Staff' => 'Staff',
	   'Absences' => 'Absences',
	   'Main' => 'Main',
	   'Eoy' => 'Eoy',
	   'No Records Found' => 'No Records Found',
	   'Error' => 'Error',
	   'Edit' => 'Edit',
	   'Delete' => 'Delete',

	   );

my $self = 'staffabsview.pl';

use DBI;
use CGI;
use Cwd;
use Time::JulianDay;

my $self = 'staffabsview.pl';
my $editscript = 'staffabsEdit.pl';
my $deletescript = 'staffabsDelete.pl';


eval require "../../etc/admin.conf";
if ( $@ ) {
    print $lex{Error}. ": $@<br>\n";
    die $lex{Error}. ": $@\n";
}

my $q = new CGI;
print $q->header( -charset, $charset );
my %arr = $q->Vars;

if ($arr{showall} ) {
    $showall = $arr{showall};
}


my $dsn = "DBI:$dbtype:dbname=$dbase";
my $dbh = DBI->connect($dsn,$user,$password);

my @tim = localtime(time);
my $year = $tim[5] + 1900;
my $month = $tim[4] + 1;
my $day = $tim[3];
if (length($month) == 1){ $month = "0".$month;}
if (length($day) == 1){ $day = "0".$day;}
my $currdate = "$year-$month-$day";

my $currjd = julian_day( split('-', $currdate));
my $prevjd = $currjd - 30; # 30 days previous
my $prevdate = join('-',inverse_julian_day($prevjd));

#my $startjd = julian_day( split('-', $schoolstart)); # global var
#my $datejd = julian_day( split('-', $date));



# Print Page Header
my $title = "$lex{View} $lex{Staff} $lex{Absences}";
print qq{$doctype\n<html><head><title>$title</title>
 <link rel="stylesheet" href="$css" type="text/css">
 </head>\n};

print qq{<body>[ <a href="$homepage">$lex{Main}</a> ]\n};

print qq{<h1>$title</h1>\n};

if ( not $arr{page} ) {
    showStartPage();

} elsif ( $arr{page} == 1 ) {
    delete $arr{page};
    showReport();
}

#----------------
sub showStartPage {
#----------------

    # Get userids and dates of absences
    # my $sth1 = $dbh->prepare("select * from staff_absent where userid = ?"); # in case we need months
    my $sth1 = $dbh->prepare("select count(*) from staff_absent where userid = ?");

    my $sth2;
    if ( $showall ) {
	$sth2 = $dbh->prepare("select * from staff_absent order by adate desc");
    } else {
	$sth2 = $dbh->prepare("select * from staff_absent where to_days(adate) > to_days('$prevdate')
       	      order by adate desc");
    }

    # Check if withdrawn.
    my $sth3 = $dbh->prepare("select count(*) from staffwd where userid = ?");

    
    print qq{<form action="$self" method="post">\n};
    print qq{<input type="hidden" name="showall" value="showall">\n};
    print qq{<input type="submit" value="Show All Records">\n};
    print qq{</form>\n};


    my %status; # staff who are withdrawn will have a 1 value; current will be 0;
    
    # Display the current months records.
    $sth2->execute;
    if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }
    my $first = 1;
    
    while ( my $ref = $sth2->fetchrow_hashref ) {
	my %r = %$ref;

	my $userid = $r{userid};
	# cache list of staff status
	if ( not $status{ $userid } ) { # add them to the list
	    $sth3->execute( $userid );
	    if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }
	    my $wdcount = $sth3->fetchrow;
	    $status{ $userid } = $wdcount;
	    
	} else { # already in the list
	    if ( $status{ $userid }  and not $showall ) { # a 1 value means withdrawn.
		next;
	    }
	}

	
	my $date = $r{adate};
	my $datejd = julian_day(split('-',$date));
	my $weekday = day_of_week($datejd) + 1;
	
	if ( $first ) { # start table
	    print qq{<table border="1" cellpadding="3" cellspacing="0" style="margin:1em;float:left;">\n};
	    print qq{<tr><th>Date</th><th>Staff Member</th><th>Reason</th><th>Day Part</th><th>Other</th>};
	    print qq{<th>Late</th><th>Comment</th></tr>\n};
	    $first = 0;
	}

	my $wd;
	if ( $status{$userid} ) { # withdrawn
	    $wd = qq{<span style="color:red;font-weight:bold;">WD</span>};
	}
	
	print qq{<tr><td>$wd <b>$dow[$weekday]</b> $r{adate}</td><td><b>$r{lastname}</b>, $r{firstname} </td>};
	print qq{<td>$r{reason}</td>};
	print qq{<td>$r{daypart}</td><td>$r{other}</td><td>$r{late}</td><td>$r{comment}</td></tr>\n};

    }
    print qq{</table>\n};

    # Test
    #print qq{<div>Status</div>\n};
    #foreach my $key (sort keys %status ) {
    # print qq{<div>$key - VAL:$status{$key}</div>\n};
    #}

    
    my $sth = $dbh->prepare("select distinct userid, lastname, firstname from staff_absent
    	 order by lastname, firstname");
    $sth->execute;
    if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }

    my $first = 1;
    while ( my ($userid,$ln,$fn) = $sth->fetchrow ) {

	if ( $first ) { # start table
	    print qq{<table border="1" cellpadding="3" cellspacing="0" style="margin:1em;float:left;">\n};
	    print qq{<tr><th>Staff Member</th><th>Absent<br>Count</th><th></th></tr>\n};
	    $first = 0;
	}

	my $wd;
	if ( $status{$userid} ) { # withdrawn
	    $wd = qq{<span style="color:red;font-weight:bold;">WD</span>};
	}

	
	# Get a record count
	$sth1->execute($userid);
	if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }
	my $rcount = $sth1->fetchrow;

	print qq{<tr><td>$wd <b>$ln</b>, $fn ($userid)</td><td>$rcount</td>};
	print qq{<td><form action="$self" method="post" };
	print qq{style="display:inline;">\n};
	print qq{<input type="hidden" name="userid" value="$userid">\n};
	print qq{<input type="hidden" name="page" value="1">\n};
	print qq{<input type="submit" value="View">\n};
	print qq{</form></td></tr>\n};
    }

    print qq{</table>\n};
    print qq{</body></html>\n};

    exit;
}


#--------------
sub showReport {
#--------------

    #    print qq{<div>Show Report</div>\n};
    #    foreach my $key ( sort keys %arr ) { print qq{K:$key VAL:$arr{$key}<br>\n}; }
    #    Only passed userid
    
    
    my $sth = $dbh->prepare("show columns from staff_absent");
    $sth->execute;
    if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }
    while (my @cols = $sth->fetchrow ) {
	if ( $cols[0] eq 'id' or $cols[0] eq 'comment' ) { next; }
	push @fields, $cols[0];
    }

    $sth = $dbh->prepare("select * from staff_absent where userid = ?
    	 order by adate desc, lastname, firstname");
    $sth->execute( $arr{userid});
    if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }
    $rows = $sth->rows;


    if ($rows < 1 ) {
	print qq{<p>$lex{'No Records Found'}</p>\n};
	print qq{</body></html>\n};
	exit;
    }

    print qq{<p>[ <a href="$self">Back to Start Page</a> ]</p>\n};
    
    print qq{<table border="1" cellpadding="3" cellspacing="0">\n};

    print qq{<tr>};
    foreach my $fld (@fields ) {
	print qq{<th>$fld</th>\n};
    }
    print qq{<th colspan="2"></th></tr>\n};

    while ( my $ref = $sth->fetchrow_hashref ) {
	my %r = %$ref;

	print qq{<tr>\n};
	foreach my $field ( @fields ) {
	    print qq{<td>$r{$field}</td>};
	}

	# Edit Button
	print qq{<td><form action="$editscript" method="post" };
	print qq{style="display:inline;">\n};
	print qq{<input type="hidden" name="id" value="$r{id}">\n};
	print qq{<input type="submit" value="$lex{Edit}">\n};
	print qq{</form></td>};

	# Delete Button
	print qq{<td><form action="$deletescript" method="post" };
	print qq{style="display:inline;">\n};
	print qq{<input type="hidden" name="id" value="$r{id}">\n};
	print qq{<input type="submit" value="$lex{Delete}">\n};
	print qq{</form></td>};

	print qq{</tr>\n};
    }

    print qq{</table>\n};
    
    print qq{<p>[ <a href="$homepage">$lex{Main}</a> | \n};
    print qq{<a href="$self">Start Page</a> ]</div>\n};
    
    print qq{</body></html>\n};

    exit;
}
