#!/usr/bin/perl

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

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

use CGI::Carp qw(fatalsToBrowser);
use CGI;
use Crypt::PasswdMD5;

&today;

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

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

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

# our $debug = 1 ;

#-------- page opts --------------------------------------------------------------------------------------------------------------------------------------------------

&page_opts ;

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

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

# unless (($iaction eq 'view') or ($iaction eq 'list')) { unless ($useropts{super}{$username}) { print "UNAUTHORISED" ; exit ; }  }

&common_min_action;

exit;

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

sub validate {

my $exists   = '' ;

# $i{SupplierName} = ucfirst $i{SupplierName} ;
# $i{name} =~ s/ //gi ;

&db_min_ro($table,'*',"`SupplierName`='$i{SupplierName}'",'SupplierName','') ;

foreach my $id (keys %{$db{$table}}) { if ($db{$table}{$id}{SupplierName} eq $i{SupplierName}) { $exists = 1 ; } }

if ($exists) {
    $alert = &common_min_alert('warning',"SUPPLIERNAME ALREADY EXISTS!",'ok') ;
	%col_name = ();
    &common_min_add_screen;
    &common_min_screen1;
    }

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

sub insert {

&add_db_fields ;

$i{SupplierID} = &db_get_max_supplier_id ;
$i{AccNo}      = &db_get_max_supplier_acc_no ;

&db_min_insert($table) ;

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

sub update {

unless ($i{SupplierID}) { $error = qq(NO ID) ; return ; }

&edit_db_fields ;

unless ($i{Active}) { $i{Active} = '0' ; }
	
# $i{lastupdate} = "$now_ccyy_mm_dd $now_hour:$now_min:$now_sec" ;

&db_min_upd($table,"`SupplierID`='$i{SupplierID}'") ;

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

sub list_screen {

&db_min_ro('waybills','WaybillNo,MainAccNo,Client','','','') ;
foreach my $id (keys %{$db{waybills}}) { 
	# $client_accno_on_waybill{$db{waybills}{$id}{MainAccNo}} = $db{waybills}{$id}{Client} ;
	$client_accno_on_waybill{$db{waybills}{$id}{MainAccNo}} .= $db{waybills}{$id}{WaybillNo} . ', ' ;
	}

&db_min_ro('manifests','ManifestDetailID,Supplier,ManifestID','','','') ;
foreach my $id (keys %{$db{manifests}}) { 
	$supplier_on_manifest{$db{manifests}{$id}{Supplier}} = $db{manifests}{$id}{ManifestID} . ', ' ;
	}

&db_min_ro($table,'*','','SupplierName','') ;

foreach my $id (keys %{$db{$table}}) {
    
	my $del_butt  = qq(&nbsp;<a class="btn btn-danger btn-xs" href="javascript:deleteMinItem('$db{$table}{$id}{SupplierName}','$id')"><i class="glyphicon glyphicon-trash icon-white"></i></a>) ; 
    my $edit_butt = qq(&nbsp;<a class="btn btn-info btn-xs" href="javascript:editMinItem('$id');"><i class="glyphicon glyphicon-edit icon-white"></i></a>) ; 
    my $view_butt = qq(&nbsp;<a class="btn btn-success btn-xs" href="javascript:viewMinItem('$id');"><i class="glyphicon glyphicon-zoom-in icon-white"></i></a>) ; 
	
	# if ((lc $username eq 'vivian') or (lc $username eq 'rory')) { 
		# $del_butt = qq(&nbsp;<a class="btn btn-danger btn-xs" href="javascript:deleteMinItem('$db{$table}{$id}{SupplierName}','$id')"><i class="glyphicon glyphicon-trash icon-white"></i></a>) ; 
		# }
		
	if ($iaction eq 'display'){
	# unless ($useropts{super}{$username}) {
		$del_butt = '' ; $edit_butt = '' ; 
		if ($db{$table}{$id}{Active}) { next ; }
		}		
	elsif ($client_accno_on_waybill{$db{$table}{$id}{AccNo}}){
		my $ttw = $client_accno_on_waybill{$db{$table}{$id}{AccNo}} ; $ttw = substr($ttw,0,-2);
		$del_butt = qq(&nbsp;<a class="btn btn-default btn-xs" href="#" title="Supplier used on waybills $ttw!" data-toggle="tooltip"><i class="glyphicon glyphicon-trash icon-white"></i></a>) ; 
		}	
	elsif ($supplier_on_manifest{$db{$table}{$id}{SupplierName}}){
		my $ttw = $supplier_on_manifest{$db{$table}{$id}{SupplierName}} ; $ttw = substr($ttw,0,-2);
		$del_butt = qq(&nbsp;<a class="btn btn-default btn-xs" href="#" title="Supplier used on manifests $ttw!" data-toggle="tooltip"><i class="glyphicon glyphicon-trash icon-white"></i></a>) ; 
		}
		
	my $disp_active = qq(<span class="label label-success" title="Active!" data-toggle="tooltip"><i class="glyphicon glyphicon-ok"></i></span>); if ($db{$table}{$id}{Active}) { $disp_active = qq(<span class="label label-danger" title="Inactive!" data-toggle="tooltip"><i class="glyphicon glyphicon-ok"></i></span>); }
	my $disp_email  = '' ; if ($db{$table}{$id}{Email}) { $disp_email = qq(<a href="mailto:$db{$table}{$id}{Email}">$db{$table}{$id}{Email}</a>); }
	
    $print_tbody .= qq(<tr id="$id">
        <td>$id</td>
        <td>$db{$table}{$id}{SupplierName}</td>
        <td>$db{$table}{$id}{Country}</td>
        <td>$db{$table}{$id}{City}</td>
        <td class="dt-center">$db{$table}{$id}{AirportCode}</td>
        <td>$db{$table}{$id}{ContactName}</td>
        <td>$disp_email</td>
        <td>$db{$table}{$id}{PhoneNo}</td>
        <td style="padding-top:12px;">$disp_active</td>
        <td nowrap>
            $edit_butt
            $view_butt
            $del_butt
        </td>
    </tr>) ;
    }
	
} #------------------------------------------------------------------------------------------

sub add_db_fields {

&hidden_db_fields;

$hidden{SupplierID} = 2 ; 
$hidden{AccNo} = 2 ; 
# $required{name} = 1 ;

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

sub edit_db_fields {

&hidden_db_fields;

$hidden{SupplierID} = 1 ; # 1 = bypass for db update

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

sub view_db_fields {

&hidden_db_fields;

$ignore{SupplierID} = 1 ;

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

sub hidden_db_fields {

$ignore{iaction} = 1 ;
$ignore{FaxNo} = 1 ;
$ignore{Website} = 1 ;
$ignore{PostalAddr1} = 1 ;
$ignore{PostalAddr2} = 1 ;
$ignore{PostalAddr3} = 1 ;
$ignore{PostalAddr4} = 1 ;
$ignore{DocCharge} = 1 ;
$ignore{InsPerc} = 1 ;
$ignore{InsMin} = 1 ;
$ignore{FuelPerc} = 1 ;
$ignore{FuelFlatRate} = 1 ;
$ignore{FuelMin} = 1 ;
$ignore{FuelMax} = 1 ;
$ignore{Terms} = 1 ;
# $ignore{AccNo} = 1 ;
$ignore{RegNo} = 1 ;
$ignore{VatNo} = 1 ;
$ignore{PhyPC} = 1 ;
$ignore{PostalPC} = 1 ;
# $ignore{PhoneNo} = 1 ;

$checkbox{Active} = 1 ;
$textarea{Notes} = 1 ;
$textarea_height{Notes} = 80 ;

$required{SupplierName} = 1 ;
$required{ContactName} = 1 ;
$required{CellNo} = 1 ;
$required{Country} = 1 ;
$required{Email} = 6 ; # only email format allowed
$required{City} = 1 ;

&sort_fields;

# SupplierID 	SupplierName 	ContactName  CellNo 	Email 	 	PhyAddr1 	PhyAddr2 	PhyAddr3 	PhyAddr4  	Country 	City 	AirportCode

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

sub sort_fields {

$sort_field{1}   = 'SupplierName' ;  $preferred_title{SupplierName} = 'Supplier Name' ;
$sort_field{2}   = 'Country' ;  
$sort_field{3}   = 'City' ;  
$sort_field{4}   = 'AirportCode' ;   $preferred_title{AirportCode} = 'Airport' ;
$sort_field{5}   = 'PhoneNo' ;       $preferred_title{PhoneNo} = 'Tel No' ;
$sort_field{6}   = 'ContactName' ;   $preferred_title{ContactName} = 'Contact Name 1' ; 
$sort_field{7}   = 'Email' ;         $preferred_title{Email} = 'Email 1' ;
$sort_field{8}   = 'CellNo' ;        $preferred_title{CellNo} = 'CellNo 1' ;
$sort_field{9}   = 'ContactName2' ;  $preferred_title{ContactName2} = 'Contact Name 2' ; 
$sort_field{10}  = 'Email2' ;        $preferred_title{Email2} = 'Email 2' ;
$sort_field{11}  = 'CellNo2' ;       $preferred_title{CellNo2} = 'CellNo 2' ;
$sort_field{12}  = 'ContactName3' ;  $preferred_title{ContactName3} = 'Contact Name 3' ; 
$sort_field{13}  = 'Email3' ;        $preferred_title{Email3} = 'Email 3' ;
$sort_field{14}  = 'CellNo3' ;       $preferred_title{CellNo3} = 'CellNo 3' ;
$sort_field{15}  = 'PhyAddr1' ;      $preferred_title{PhyAddr1} = 'Addr 1' ;
$sort_field{16}  = 'PhyAddr2' ;      $preferred_title{PhyAddr2} = 'Addr 2' ;
$sort_field{17}  = 'PhyAddr3' ;      $preferred_title{PhyAddr3} = 'Addr 3' ;
$sort_field{18}  = 'PhyAddr4' ;      $preferred_title{PhyAddr4} = 'Addr 4' ;
$sort_field{19}  = 'Active' ;        $preferred_title{Active} = 'Inactive' ;
$sort_field{20}  = 'Notes' ;

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

sub add_screen {

# called from common_add_screen

&select_opts_airport('AirportCode','airport_codes','name','',1,'iata_code','');

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

sub select_opts_airport {

my ($field,$table,$valfield,$fid,$required,$addfield,$where) = @_ ;

$select{$field} = 1 ;

if ($required) { $required{$field} = 1 ; }

&db_min_ro($table,'`iata_code`,`name`',$where,'','') ;

foreach my $id (keys %{$db{$table}}) {
    if ($fid eq $id) { $selected = 'selected="selected"'; } else { $selected = ''; }		
    if ($db{$table}{$id}{$addfield})    { $disp_addfield = " [$db{$table}{$id}{$addfield}]"  ; } else { $disp_addfield = '' ; } 
    $opts{$field} .= qq(<option value="$id" $selected>$db{$table}{$id}{$valfield}$disp_addfield</option>) ; 
    }

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

sub edit_screen {

our ($lcol,$fcol) = &common_min_columns($label_col,$field_col) ;

# &common_debug("edit_screen: id[$i{SupplierID}] AirportCode[$db{$table}{$i{SupplierID}}{AirportCode}]");

&select_opts_airport('AirportCode','airport_codes','name',$db{$table}{$i{SupplierID}}{AirportCode},1,'iata_code','');

# $extra_form_fields .= &common_min_form_input('new_password','','password') ; 

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

sub view_screen {

my ($iid) = @_ ;

our ($lcol,$fcol) = &common_min_columns($label_col,$field_col) ;

foreach my $cnt (sort {$a <=> $b} keys %sort_field) {
	my $col   = $sort_field{$cnt} ;
	if ($ignore{$col}) { next ; }
	my $val = $db{$table}{$iid}{$col} ;
	$print_box_content_rows .= &common_min_form_view($col,$val) ;    
	}

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

sub thead {

$print_thead = qq(<THEAD><TR><TH>SupplierID</TH><TH>CompanyName</TH><TH>Country</TH><TH>City</TH><TH>AirportCode</TH><TH>Contact</TH><TH>Email</TH><TH>TelNo</TH><TH>Active</TH><TH>&nbsp;</TH></TR></THEAD>) ;
    
} #-------------------------------------------------------------------------------

sub page_opts {

our $glyphicon = 'plane' ;
our $lcpage    = 'supplier-contact' ; # $ucpage = uc $lcpage ; # $ucfirstpage = ucfirst $lcpage ;

my @splitlcpage = split(/\-/,$lcpage) ;   
my @ucfirstpage = map(ucfirst, map(lc,@splitlcpage)); 
$ucfirstpage    = join(' ', @ucfirstpage);  
$ucpage         = uc $ucfirstpage ;
   
our $table     = 'agents' ;
our $uniq_id   = 'SupplierID' ;

$page_title  = 'Supplier Contacts' ;

our $glob_sort_col = 1 ;
our $glob_sort_asc_desc = 'asc' ;

&common_min_add_extras ;

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

use common ;
use today ;

1;