#!/usr/bin/perl

BEGIN { use lib '/usr/home/cfg' ; require push_inc ; }
    
require cfg ;

#---------------------------------------------------------------------------------------------------------------------------------------------------------------------

use CGI::Carp qw(fatalsToBrowser);
use DBI;
use CGI;

#---------------------------------------------------------------------------------------------------------------------------------------------------------------------

@ARGV   = split(/\\*\&/, $ENV{'QUERY_STRING'});
$action = $ARGV[0] ;

our ($q)                 = CGI -> new() ;
$iaction                 = $q -> param('iaction')                || $action ;
$itownid                 = $q -> param('itownid')                || '' ;
$isaved                  = $q -> param('isaved')                 || '' ;
$itype                   = $q -> param("itype")                  || '' ;

#---------------------------------------------------------------------------------------------------------------------------------------------------------------------

print "Content-type: text/html\n\n";

&today;

if ($iaction eq ''){
    &db_get_town_opts ;
    &screen4;
    }

if ($iaction eq 'add'){
    &add_screen;
    &screen1;
    }

if ($iaction eq 'list'){
    &list_screen ;    
    &screen3;
    } 

if ($iaction eq 'edit'){
    &load_screen ;
    &edit_screen;
    &screen1;
    }

if ($iaction eq 'save'){
    &common_load_params ;
    &check_towns ;
    &insert_town ;
    &screen2;
    }
    
if ($iaction eq 'update'){    
    &common_load_params ;
    &update_town ;
    &load_screen ;
    &edit_screen;
    &screen2;
    }

exit;

#------------------------------------------------------------------------------------------

sub check_towns {

my $exists = &db_check_town($i{town}) ;

if ($exists) {
    $alert = qq(<div class="alert alert-danger" role="alert"><i class="glyphicon glyphicon-exclamation-sign"></i> A TOWN WITH THIS NAME ALREADY EXISTS !</div>) ;
    &edit_screen;
    &screen1;
    }

} #------------------------------------------------------------------------------------------

sub insert_town {

&db_insert_town ;    
    
$itownid = $new_town_id ;

} #------------------------------------------------------------------------------------------

sub update_town {

unless ($itownid) { return ; }

&db_open_upd ;

# $sql = qq(UPDATE towns SET 
# town         = "$i{town}",
# hub          = "$i{hub}",
# acchubcode   = "$i{acchubcode}",
# opshubcode   = "$i{opshubcode}",
# airportcode  = "$i{airportcode}"
# WHERE townid = "$itownid") ;
          
$sql = qq(UPDATE towns SET 
town         = "$i{town}",
airportcode  = "$i{airportcode}"
WHERE townid = "$itownid") ;
          
&common_debug($sql) ;

$sth = $dbh -> do ($sql) or die "could not execute :<BR>$sql<BR>$!" ;

&db_close_conn ;

$success = qq(SUCCESSFULLY SAVED) ;

} #------------------------------------------------------------------------------------------

sub get_opts {

&db_get_airport_opts ;
# &db_load_town_hubs ;
# &db_load_town_acchubcode ;
# &db_load_town_opshubcode ;

} #------------------------------------------------------------------------------------------

sub list_screen {

&db_get_airport_opts ;
&db_town_list ;
&extra_crumb ;

$trigger_jquery = qq(var oTable = \$('#tnlist').dataTable(); oTable.fnSort( [ [1,'asc'] ] );) ;

} #------------------------------------------------------------------------------------------

sub add_screen {

&get_opts ;
$box_header_title = 'Add Town' ;
$hidden_action    = 'save' ;
$head_col_width   = 1 ;
&extra_crumb ;

} #------------------------------------------------------------------------------------------

sub edit_screen {

$box_header_title = 'Edit Town : ' . $tn_town ;
$hidden_action    = 'update' ;
$head_col_width   = 1 ;
&extra_crumb ;

} #------------------------------------------------------------------------------------------

sub extra_crumb {

$insert_crumbs = qq(<li><a href="$useropts{'folder'}/towns">Manage Towns</a></li>) ;

&config_set_breadcrumbs ;

} #------------------------------------------------------------------------------------------

sub load_screen {

unless ($itownid) { $warning = qq(NO TOWN ID) ; return ; }

&db_load_town($itownid) ;
&get_opts ;

$trigger_jquery_raw .= qq(\$("#selectAirPort").val("$tn_airportcode").trigger('change');) ;
# $trigger_jquery_raw .= qq(\$("#selectHUB").val("$tn_hub").trigger('change');) ;
# $trigger_jquery_raw .= qq(\$("#selectAccHubCode").val("$tn_acchubcode").trigger('change');) ;
# $trigger_jquery_raw .= qq(\$("#selectOpsHubCode").val("$tn_opshubcode").trigger('change');) ;

} #------------------------------------------------------------------------------------------

sub screen1 {

$screen_buttons = qq(<div class="row"><div class="col-md-12"><button type="submit" class="btn btn-primary">Save</button>&nbsp;<button type="button" class="btn btn-default" onclick="history.go(-1);">Back</button></div></div><div class="row"><div class="col-md-12">&nbsp;</div></div>) ;

&common_town_html ;

if ($error) {
    $alert = qq(<div class="alert alert-danger" role="alert"><i class='glyphicon glyphicon-exclamation-sign'></i> $error !</div>) ;
    }
elsif ($warning) {
    $alert = qq(<div class="alert alert-warning" role="alert"><i class="glyphicon glyphicon-exclamation-sign"></i> $warning !</div>) ;
    }
elsif ($success) {
    $alert = qq(<div class="alert alert-success" role="alert"><i class="glyphicon glyphicon-ok"></i> $success !</div>) ;
    }

unless ($print_town) { $trigger_jquery .= qq(\$("#towntable").hide();) ; }

require tn_edit ;

exit ;

} #------------------------------------------------------------------------------------------

sub screen2 {

print <<ENDOFTEXT;
<!DOCTYPE html>
<html lang="en">
<head>    
    <meta charset="utf-8">
    <title>Film Freight Admin</title>
    <script src="$useropts{'bower_components'}/jquery/jquery.min.js"></script>   
</head>
<body>
<script>

\$(document).ready(function(){ \$('#town-form').submit(); })

</script>
</body>

<form role="form" name="townform" id="town-form" method="post" action="/town-list">
<input type="hidden" name="iaction"          value="list">
<input type="hidden" name="itownid"          value="$itownid">
<input type="hidden" name="isaved"           value="1">
</form>

</html>
ENDOFTEXT
#

exit ;

} #------------------------------------------------------------------------------------------

sub screen3 {

if ($isaved) { $alert = qq(<div class="alert alert-success" role="alert"><i class="glyphicon glyphicon-ok"></i> TOWN <strong><a href="javascript:editTown('$itownid');">$itownid</a></strong> SUCCESSFULLY SAVED !</div>) ; }

require tn_list ;
exit ;

} #------------------------------------------------------------------------------------------

sub screen4 {

require tn_main ;
exit ;

} #------------------------------------------------------------------------------------------

use db ;
use today ;
use common ;

1;