#!/usr/bin/perl
#
#
use CGI qw/:standard/;

use HTML::Template;


do 'const.pl';
do 'trackLib.pl';
do 'showInfoLib.pl';
do 'db.pl';
use DateTime;


sub getMenuInfo{
	local($flog,$e)=@_;
	#my $sql = "SELECT distinct nome FROM dati_gps WHERE tipo_elemento<>'y' ";
	my  $dt = DateTime->now; # same as ( epoch => time() )
	$dt->set_time_zone( 'GMT' );
 	$ymd   = $dt->ymd('');      # 06/12/2002
	$hms    = $dt->hms('');           # 14:02:29

     	$now = $ymd . $hms;

	my $sql = "SELECT nome,stato FROM percorsi";
	if ($e ne "home"){
		$sql = $sql . " WHERE codice='$e'";
	}
	print $flog "definisco: $sql . \n";
	my $sqldatetpl = "SELECT to_char(a.inizio,'YYYYMMDD') as giorno,to_char(a.inizio,'HH24:MI') as inizio,to_char(a.fine,'HH24:MI') as fine, a.istanza,a.descrizione,a.stato   FROM percorsi_attivi a, percorsi b WHERE to_char(a.fine,'YYYYMMDDHH24MISS') < '$now' and a.percorso_oid = b.oid and b.nome='<NOME>' order by giorno, inizio";
	#my $sqldatetpl = "SELECT to_char(a.inizio,'YYYYMMDD') as giorno,to_char(a.inizio,'HH24:MI') as inizio,to_char(a.fine,'HH24:MI') as fine, a.istanza   FROM percorsi_attivi a, percorsi b WHERE a.percorso_oid = b.oid and b.nome=";
	my $dbh = dbconn($db,$dbname,$dbhost,$dbuser,$dbpsw);
	print $flog "eseguo $sql\n";
	my @List=getInfoByQuery($sql,$dbh,$flog);
	print $flog "eseguito $sql\n";
	my $num = $#List;
	my $out = "";
	my $attivo="";
	my $live_perc="";
	for (my $i=0;$i<=$num;$i++){
		my $rec = @List[$i];
		my $nome = $rec->[0];
		my $stato = $rec->[1];
		if ($stato == 1){
			$attivo = $nome;
		}
		$out = "$out<p>\n$nome\n";
		#my $sqldate = $sqldatetpl . "'$nome' ORDER BY a.descrizione";
		my $sqldate = $sqldatetpl ;
		$sqldate=~s/<NOME>/$nome/g;
		print $flog "eseguo $sqldate\n";
		my @ListDates=getInfoByQuery($sqldate,$dbh,$flog);
		print $flog "eseguito $sqldate\n";
		my $numdates = $#ListDates;
		$out = "$out<ul id=\"navlist\">";
		my $url;
		for (my $j=0;$j<=$numdates;$j++){
			my $t = @ListDates[$j]->[0];
			#my $dt = DateTime->from_epoch(epoch=>$t);
			#my $giorno = $dt->ymd(''); 
			my $inizio = @ListDates[$j]->[1];
			my $finetipo = @ListDates[$j]->[2];
			my $istanza = @ListDates[$j]->[3];
			my $desc = @ListDates[$j]->[4];
			my $perc_stato = @ListDates[$j]->[5];
			$url = "<a href=\"/tracking/cgi-bin/earthMyRegata.pl?nome=$istanza\&data=$t\&eventi=$e\&desc=$desc\">TRACES - $desc - $t</a>";
			#$out="$out<li>$url</li>";
			$url = "<a href=\"/tracking/cgi-bin/showInfo.pl?race=$istanza\&eventi=$e\&info=speed\">General Speed Info</a>";
			#$out="$out<li>$url</li>\n";
			#my @CatList =  getListOfCategory($istanza, $t, FLOG);
			#my $hid = "<li><ul>";
			#for my $x (@CatList){
				#my $cat = $x->[0];
				#$url = "<a href=\"/tracking/cgi-bin/staticRegataBack.pl?name=$istanza\&data=$t\&eventi=$e\&cat=$cat\">PLAYBACK - $desc - $t-$cat</a>";
				#$hid="$hid<li>$url</li>\n";
			#}
			#$hid = "$hid\n</ul><a href=\"javascript:close('pblist','$hid')\">close</a></li>";
			$out="$out<li>PLAYBACK BY CLASS:";
			#$out="$out<div id=\"pblist-$istanza\"><a href=\"javascript:showCat('pblist-$istanza','$e','$istanza','$t')\">Show Classes</a></div></li>\n";
			$out="$out<li>---------</li>";
			if ($perc_stato == 1){
				$live_perc = $istanza;
			}
		}
		$out = "$out</ul></li></ul>\n</p>\n";
		
	}
	#$attivo="1";
	$sql= "SELECT to_char(a.inizio,'YYYYMMDD') as giorno,to_char(a.inizio,'HH24:MI') as inizio,to_char(a.fine,'HH24:MI') as fine, a.istanza,a.descrizione,a.stato   FROM percorsi_attivi a, percorsi b WHERE  a.percorso_oid = b.oid and b.codice='$e' and a.stato=1 order by giorno, inizio";
	my @ListDates=getInfoByQuery($sql,$dbh,$flog);

	if ($#ListDates >= 0){
			my $t = @ListDates[$j]->[0];
			#my $dt = DateTime->from_epoch(epoch=>$t);
			#my $giorno = $dt->ymd(''); 
			my $inizio = @ListDates[$j]->[1];
			my $finetipo = @ListDates[$j]->[2];
			my $istanza = @ListDates[$j]->[3];
			my $desc = @ListDates[$j]->[4];
		#$out = "$out<p><a href=\"/tracking/cgi-bin/showRegataLive.pl\">Live</a></p>";
		$out = "$out<p><a href=\"/tracking/cgi-bin/earthMyRegataLive.pl?eventi=$e\&nome=$istanza\">Live (Plugin Google Earth)</a></p>";
		#$out = "$out<p><a href=\"/tracking/cgi-bin/mapRegataLive.pl?eventi=$e\&nome=$istanza\">Live (No Plugin is needed)</a></p>";
	}	
		dbdisconn($dbh);
	return $out;


}

$ENV{'HTML_TEMPLATE_ROOT'}=$rootArea;
my $query = new CGI;
open (FLOG,">>$logfile");
my $evento = "home";
my $tipo = "";
my @Area = $query->param('script');
my $area = @Area[0];
my @Eventi = $query->param('eventi');
if ($#Eventi>=0){
$evento = @Eventi[0];
}
my @Tipo = $query->param('tipo');
if ($#Tipo>=0){
$tipo = @Tipo[0];
}
$area=$evento . ".html";
my $bodyinfo=showInfo($area,$areaweb,FLOG);
my $menuinfo = getMainMenuInfo(FLOG,$evento,$tipo);
$menuinfo = "<div id=\"innerleft\">" .$menuinfo . "</div>";
my $tpl = "/" . $evento . ".tpl";
my $template = HTML::Template->new(filename => $tpl);
#$template->param(MENU => $menuinfo);
$template->param(LEFT => $menuinfo);
$template->param(BODY => $bodyinfo);
close (FLOG);
print "Content-Type: text/html\n\n", $template->output;


  # fill in some parameters
  #   $template->param(HOME => $ENV{HOME});
  #    



