#!/usr/bin/perl
use CGI qw(:standard);
use Pg;
$cgi_object = new CGI;
print $cgi_object->header;
my $bgn = param("bgn");
my $userseq = param("userseq");
my $username = param("username");
my $starttime = param("starttime");
my $stoptime = param("stoptime");
my $userphone = param("userphone");
my $custref = param("custref");
my $mfgref = param("mfgref");
my $company = param("company");
my $contact = param("contact");
my $addr1 = param("addr1");
my $addr2 = param("addr2");
my $city = param("city");
my $state = param("state");
my $zip = param("zip");
my $phone = param("phone");
my $equip = param("equip");
my $serial = param("serial");
my $svcneeded = param("svcneeded");
my $technotes = param("technotes");
my $svcperf = param("svcperf");
my $svcdate = param("svcdate");
my $partsin = param("partsin");
my $timein = param("timein");
my $timeout = param("timeout");
my $rtmins = param("rtmins");
my $rtmiles = param("rtmiles");
my $about = param("about");
my $abrtrn = param("abrtrn");
my $etadate = param("etadate");
my $actcode = param("actcode");
my $status = param("status");
my $prob = param("prob");
my $cause = param("cause");
my $action = param("action");
my $conf = param("conf");
my $dispnotes = param("dispnotes");
$conn = Pg::connectdb("host=fixed port=5432 dbname=test user=hugginsd");
print start_html("MPS Data Entry Evaluation Form"), h1("MPS Data Entry Evaluation Form");
if (param()) {
	print "<table border=2 text=+2>"; 
	print "<tr>\n"; 
	print "<td>", $userseq, "</td>";
	print "</tr>\n"; print "</table>\n";
	$query = "update dataentry set username='$username', userphone='$userphone', custref='$custref', mfgref='$mfgref', company='$company', contact='$contact', addr1='$addr1', addr2='$addr2', city='$city', state='$state', zip='$zip', phone='$phone', equip='$equip', serial='$serial', svcneeded='$svcneeded', technotes='$technotes', svcperf='$svcperf', svcdate='$svcdate', partsin='$partsin', timein='$timein', timeout='$timeout', rtmins='$rtmins', rtmiles='$rtmiles', about='$about', abrtrn='$abrtrn', etadate='$etadate', actcode='$actcode', status='$status', prob='$prob', cause='$cause', action='$action', conf='$conf', dispnotes='$dispnotes', stoptime='now' where text(userseq)='$userseq';";
	#$query = "update dataentry set username='$username', userphone='$userphone', stoptime='now' where text(userseq)='$userseq';";
	$result=$conn->exec("$query"); 
	$query="select userseq, username, userphone, starttime, stoptime, stoptime-starttime from dataentry where text(userseq)='$userseq';";
	$result=$conn->exec("$query");
	print "<table border=2 text=+2>";
	print
"<tr><th>userseq</th><th>username</th><th>userphone</th><th>starttime</th><th>stoptime</th><th>finished in</th></tr>\n";
	while (@row=$result->fetchrow) {

		print "<TR>\n";
		print "<TD>", $row[0], "</TD>";
		print "<TD>", $row[1], "</TD>";
		print "<TD>", $row[2], "</TD>";
		print "<TD>", $row[3], "</TD>";
		print "<TD>", $row[4], "</TD>";
		print "<TD>", $row[5], "</TD>";
		print "<TD>", $row[6], "</TD>";
		print "<TD>", $row[7], "</TD>";
		print "<TD>", $row[8], "</TD>";
		print "<TD>", $row[9], "</TD>";
		print "</TR>\n";
	}
	print "</table>\n";
} else {
	$query="insert into dataentry (bgn, starttime) values ('y','now');";
	$result=$conn->exec("$query");
	$query="select max(userseq) from dataentry;";
	$result=$conn->exec("$query");
	print hr, start_form;
	while (@row=$result->fetchrow) {
	$userseq=$row[0];
	print "<table border=2 text=+2>";
	print "<TR>\n";
	print "<TD>", $row[0], "</TD>";
	print "<TD>", $userseq, "</TD>";
	print "</TR>\n";
	}
	print "</table>\n";
	#print p("userseq: ", $userseq);
	print p("PLEASE READ!!!  DO NOT USE THE ENTER KEY UNTIL YOU ARE DONE!  USE TAB OR
THE MOUSE INSTEAD!  DO NOT ENTER ANY SINGLE OR DOUBLE QUOTES!", hidden("userseq",$userseq));
	print p("What is your name: ", textfield("username",""));
	print p("What is your phone number: ", textfield("userphone",""));
	print p("Customer Reference: ", textfield("custref",""));
	print p("Manufacturer Reference: ", textfield("mfgref",""));
	print p("Company: ", textfield("company",""));
	print p("Contact: ", textfield("contact",""));
	print p("Address line 1: ", textfield("addr1",""));
	print p("Address line 2: ", textfield("addr2",""));
	print p("City: ", textfield("city",""));
	print p("State: ", textfield("state",""));
	print p("Zip: ", textfield("zip",""));
	print p("Phone: ", textfield("phone",""));
	print p("Equipment: ", textfield("equip",""));
	print p("Serial #: ", textfield("serial",""));
	print p("Service Needed: ", textfield("svcneeded",""));
	print p("Tech Notes: ", textfield("technotes",""));
	print p("Service Performed: ", textfield("svcperf",""));
	print p("Service Date: ", textfield("svcdate",""));
	print p("Parts in Date: ", textfield("partsin",""));
	print p("Time in: ", textfield("timein",""));
	print p("Time out: ", textfield("timeout",""));
	print p("Round Trip Minutes: ", textfield("rtmins",""));
	print p("Round Trip Miles: ", textfield("rtmiles",""));
	print p("Airbill Out: ", textfield("about",""));
	print p("Return Airbill: ", textfield("abrtrn",""));
	print p("ETA Date: ", textfield("etadate",""));
	print p("Action Code: ", textfield("actcode",""));
	print p("Status Code: ", textfield("status",""));
	print p("Problem Code: ", textfield("prob",""));
	print p("Cause Code: ", textfield("cause",""));
	print p("Action Code: ", textfield("action",""));
	print p("Confirmation: ", textfield("conf",""));
	print p("Dispatch Notes: ", textfield("dispnotes",""));
	print p(submit("send"), reset("clear"));
	print end_form, hr;
}
