#!/usr/bin/perl

use CGI; #For CGI processing
use DBI; #For Database access

#Define the CGI Query and print header
$query = CGI::new();
print $query->header;

#Set the database parameters
$user = "nouser";
$password = "nopassword";
$dsn = "DBI:mysql:database=flying;host=deadhost";

$dbh = DBI->connect($dsn, $user, $password);
$table = "flight";

$statement="select * from flight";
$sth=$dbh->prepare($statement) or die "Can't execute $statement: $dbh->errstr\n";
$rv = $sth->execute or die "can't execute query: $sth->errstr\n";

$total_day_landings=0;
$total_night_landings=0;
$total_night_time=0;
$total_training_time=0;
$total_solo_time =0;
$total_xc_time =0;
$total_hood_time =0;
$total_time=0;

#Display flight Summary
print "<html><head><title>AC's Flight Log</title>";
print <<"EOF";
<script language="javascript">

if (document.images) {
pixon = new Image();           // Active images
pixon.src = "/images/pictures152x31-on.gif"; 

pixoff = new Image();          // Inactive images
pixoff.src = "/images/pictures152x31-off.gif";  

homeon = new Image();           // Active images
homeon.src = "/images/home150x31-on.gif"; 

homeoff = new Image();          // Inactive images
homeoff.src = "/images/home150x31-off.gif";  

linkson = new Image();
linkson.src = "/images/links154x31-on.gif";

linksoff = new Image();
linksoff.src = "/images/links154x31-off.gif";

flightlogon = new Image();
flightlogon.src = "/images/flightlog148x31-on.gif";

flightlogoff = new Image();
flightlogoff.src ="/images/flightlog148x31-off.gif";
}


function imgOn(imgName) {
 if (document.images) {
  document[imgName].src = eval(imgName + "on.src");
  }
}

function imgOff(imgName) {
 if (document.images) {
  document[imgName].src = eval(imgName + "off.src");
 }
}

// -->
</script>

</head>
<body background="./images/cloudbg.gif">
<center>
<table border="0" cellspacing="0" cellpadding="0" WIDTH="768">
  <tr>
    <td>
      <img src="./images/title.gif" HEIGHT="69" WIDTH="768" ALT="[Title] AC's Flight Line"></td>
  </tr>
  <tr>
    <td align="center">
    <table border="0" cellspacing="0" cellpadding="0" WIDTH="604">
<tr>
<td><A HREF="/" onMouseOver="imgOn('home')" onMouseOut="imgOff('home')"><img src="./images/home150x31-off.gif" HEIGHT="31" WIDTH="150" BORDER="0" ALT="[Link] Home" name="home"></A></td>

<td><img src="./images/flightlog148x31-gray.gif" HEIGHT="31" WIDTH="148" name="flightlog" BORDER="0" ALT="[Link]Flight Log"></td>

<td><A HREF="./pictures/" onMouseOver="imgOn('pix')" onMouseOut="imgOff('pix')"><img src="./images/pictures152x31-off.gif" HEIGHT="31" WIDTH="152" name="pix" BORDER="0" ALT="[Link]Pictures"></A></td>

<td><A HREF="./links/" onMouseOver="imgOn('links')"onMouseOut="imgOff('links')"><img src="./images/links154x31-off.gif" HEIGHT="31" WIDTH="154" name="links" BORDER="0" ALT="[Link]Links"></A></td></tr>
</table>
    </td>
  </tr>
<tr><td colspan="4">
<P><br>
EOF
print "<H1>Flight Summary</H2>";
print "<table>";
while($hash_ref = $sth->fetchrow_hashref){
  print "<TR>";
  print "<TD><b>Flight:</b></TD><TD COLSPAN=\"11\">$hash_ref->{ID}</TD>";
  print "</TR>\n";
  print "<TR>";
  print "<TD><b>Remarks/<br>Procedures:</b></TD><TD COLSPAN=\"11\">$hash_ref->{remarks}</TD>";
  print "</TR>\n";
  print "<TR>";
  print "<TH>Date</TH><TH>A/C Type</TH><TH>A/C ID</TH><TH>From</TH><TH>To</TH><TH># Day Lndgs</TH><TH># Night Lndgs</TH><TH>Training Time</TH><TH>Solo Time</TH><TH>Cross-country Time</TH><TH>Total Time</TH>";
  print "</TR>";
  print "<TR>";
  print "<TD>$hash_ref->{date}</TD><TD>$hash_ref->{make}</TD><TD>$hash_ref->{ident}</TD><TD>$hash_ref->{apt_from}</TD><TD>$hash_ref->{apt_to}</TD><TD>$hash_ref->{dayland}</TD><TD>$hash_ref->{nightland}</TD><TD>$hash_ref->{hrs_training}</TD><TD>$hash_ref->{hrs_pic}</TD><TD>$hash_ref->{hrs_xc}</TD><TD>$hash_ref->{hrs_total}</TD>";
  print "<TR>";
  print "<TR><TD COLSPAN=\"12\"><hr></TD></TR>";
  $total_day_landings = $total_day_landings + $hash_ref->{dayland};
  $total_night_landings = $total_night_landings + $hash_ref->{nightland};
  $total_night_time = $total_night_time + $hash_ref->{hrs_night};
  $total_training_time = $total_training_time + $hash_ref->{hrs_training};
  $total_solo_time = $total_solo_time + $hash_ref->{hrs_pic};
  $total_xc_time = $total_xc_time + $hash_ref->{hrs_xc};
  $total_hood_time = $total_hood_time + $hash_ref->{hrs_hood};
  $total_time = $total_time + $hash_ref->{hrs_total};
}
print "</table>";
$total_landings = $total_day_landings + $total_night_landings;
print "Total Day Landings: $total_day_landings <br>";
print "Total Night Landings: $total_night_landings <br>";
print "Total Landings: $total_landings <br>";
print "Total Training Time: $total_training_time <br>";
print "Total Night Time: $total_night_time <br>";
print "Total Solo/PIC Time: $total_solo_time <br>";
print "Total Cross-country Time: $total_xc_time <br>";
print "Total Hood Time: $total_hood_time <br>";
print "<hr>";
print "<strong>Total Time:</strong> $total_time <br>";
print "</table>";
print "</body></html>";
