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

#  This file is part of Open Admin for Schools.


my %lex = ('Main' => 'Main',
	   'Continue' => 'Continue',
	   'Homeroom' => 'Homeroom',
	   'Grade' => 'Grade',
	   'Select by' => 'Select by',
	   'Error' => 'Error',
	   'Sort by' => 'Sort by',
	   'Name' => 'Name',
	   'Common Math Assessment' => 'Common Math Assessment',
	   'Report' => 'Report',
	   'OR' => 'OR',
	   'No Selection' => 'No Selection',
	   'School Year' => 'School Year',
	   'Show Withdrawn' => 'Show Withdrawn',


	   );

my $self = 'viewHidden.pl';

use DBI;
use CGI;

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;


my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $iddst) = localtime(time);
$year = $year + 1900;
$wday++; $mon++;
my $currsdate = "$year-$mon-$mday";
my $currdate = "$dow[$wday], $month[$mon] $mday, $year";
#my $currjd = julian_day( split('-', $currsdate) );

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


my $title = "View Hidden Students";
print qq{$doctype\n<html><head><title>$title</title>\n};
print qq{<link rel="stylesheet" href="$css" type="text/css">\n};


# Some Javascript here for resizing...
print "<script type=\"text/javascript\">
 <!-- \n";

print "function showrecord(studnum) { \n";
print " winName=window.open(\"viewHidden.pl?studnum=\" + studnum + \"&page=\" + 1,\n";
print "'Student_Record','height=1200,width=1600,screenX=100,screenY=100,scrollbars=yes');";
print " winName.focus();
}
 -->
 </script>\n";



print qq{$chartype\n</head><body style="padding:1em 5em;">\n};

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


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

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



#----------------
sub showStartPage { # Display Students in the student_temp table.
#----------------

    my $sth = $dbh->prepare("select * from student_temp order by lastname, firstname"); 

    my $first = 1;
    
    $sth->execute();
    if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; }
    while ( my $ref = $sth->fetchrow_hashref ) {
	%r = %$ref;

	if ( $first ) {
	    print qq{<table cellpadding="3" cellspacing="0" border="1">\n};
	    print qq{<caption>Temporary Student Table</caption>\n};
	    print qq{<tr><th>Student</th><th>Grade</th><th>Birthdate</th><th>Tab</th><th>Pop</th></tr>\n};
	    $first = 0;
	}

	print qq{<tr><td><b>$r{lastname}</b>, $r{firstname} ($r{studnum})</td><td>$r{grade}</td>};
	print qq{<td>$r{birthdate}</td>};
	print qq{<td>\n};

	print qq{<form action="$self" method="post" target="_blank">\n};
	print qq{<input type="hidden" name="page" value="1">\n};
	print qq{<input type="hidden" name="studnum" value="$r{studnum}">\n};
	print qq{<input type="submit" value="Tab View">\n};
	print qq{</form></td>};

	print qq{<td><a href="javascript:showrecord($r{studnum})">};
	print qq{Popup</a></td>};
	print qq{</tr>\n};

	$first = 0;
    }

    print qq{</table>\n};

    
    if ( $first ) { # no records
	print qq{<h3>No Records Found</h3>\n};
    }
    
    print qq{</body></html>\n};

    exit;

}



#--------------------
sub showStudentRecord {
#--------------------

    
    # foreach my $key ( sort keys %arr ) { print "K:$key V:$arr{$key}<br>\n"; }
    # Passed: studnum
    

    # Read in Template
    eval open (FH,"<", "../../template/student.tpl");
    if ( $@ ) {
	print $lex{Error}. " $@<br>\n";
	die $lex{Error}. " $@\n";
    }

    # Slurp data
    my $formtext;
    { local $/; $formtext = <FH>; close FH;}

    # Create hash for fieldnames from meta.
    my $sth = $dbh->prepare("select fieldid, fieldname, required from meta where tableid = ?");
    $sth->execute( student );
    if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; }
    my %fieldnames;
    while ( my ( $fieldid, $fieldname, $required ) = $sth->fetchrow ) {
	if ( $required ) { # has any value
	    $fieldname = qq{<span style="font-weight:bold;">$fieldname</span>};
	}
	$fieldnames{$fieldid} = $fieldname;
    }

    # Now put replacement text back in.
    $formtext =~ s{\<\*(.*?)\*\>}
    { exists( $fieldnames{$1} ) 
	? $fieldnames{$1} 
	: $1
    }gsex;


    my $table = 'student_temp';


    $sth = $dbh->prepare("select * from $table where studnum = ?");
    $sth->execute( $arr{studnum} );
    if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; }

    my $ref = $sth->fetchrow_hashref;
    my %rec = %$ref;
    
    my $text = $formtext; # make a copy to use.

    
    # Check for a picture and picture mode (ShowStudentPicture)
#    if ( $r_ShowStudentPicture ) { # put in a table value with a picture link.
#	if ( $rec{pic} ) {
#	    my $pictureform = qq{<div><img style="width:250px;" src="/pic-big/}. $rec{studnum}.
#		qq{.jpg"></div>\n};
#	    $text =~ s/\<a name=\"picture\"\>\<\/a\>/$pictureform/;
#	}
#    }
	
    # now put field values back into $text variable...
    $text =~ s{ \<\@(.*?)\@\> }
    { exists($rec{$1}) 
       ? $rec{$1} 
	: "$rec{$1}-$1"	 
          }gsex;

    print qq{<hr><div style="background-color:blue;border 1px solid black;padding:1em;width:150ch;">};
    print $text, qq{\n};
    print qq{</div>\n};

    
    if ( $first ){ # no records found
	print qq{<div style="font-size:150%;font-weight:bold;">$lex{'No Records Found'}</div>\n};
    
    } else {
	print qq{</table>\n};
    }

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

    exit;

}
