#!/usr/bin/perl

BEGIN { use lib '/usr/home/cfg' ; require push_inc ; }
    
use CGI qw( :standard );
use CGI::Carp qw(fatalsToBrowser);
use Fcntl qw(:flock) ;
use LWP::UserAgent;
use HTTP::Request;
use XML::Simple ;
use Date::Calc qw(Month_to_Text);

require cfg ;

print header;   # CGI.pm method

# unless ($useropts{admin}{$userid} eq $username) { require '401.pm' ; exit ; }

# https://itvadmin.co.za/cgi-bin/scripts/dialog/dcb_track_trace.pl?debug=1&waybillnr=

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

&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               = $q -> param('debug')                  || '' ;
our $waybillnr           = $q -> param('waybillnr')              || '' ;

# $debug  = 0 ;

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

our %trackntrace = () ;

&dcb_trackntrace($waybillnr) ;
&view_form ; 
&screen2 ;

# &start_html ;

# &reload_parent ;

exit;

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

sub view_form {
	
	$sort_field{1}	= 'booking_date' ; 
	$sort_field{2}	= 'comments' ; 
	$sort_field{3}	= 'consignee_name' ; 
	$sort_field{4}	= 'consignor_name' ; 
	$sort_field{5}	= 'current_status' ; 
	$sort_field{6}	= 'delivery_date' ; 
	$sort_field{7}	= 'insured_value' ; 	
	$sort_field{8}	= 'number_of_parcels' ; 
	$sort_field{9}	= 'receiver_address_line_1' ; 
	$sort_field{10}	= 'receiver_address_line_2' ; 
	$sort_field{11}	= 'receiver_city' ; 
	$sort_field{12}	= 'receiver_contact_name' ; 	
	$sort_field{13}	= 'receiver_contact_tel_1' ; 
	$sort_field{14}	= 'receiver_name' ; 
	$sort_field{15}	= 'receiver_postal_code' ; 
	$sort_field{16}	= 'receiver_suburb' ; 
	$sort_field{17}	= 'sender_address_line_1' ; 	
	$sort_field{18}	= 'sender_address_line_2' ; 
	$sort_field{19}	= 'sender_city' ; 
	$sort_field{20}	= 'sender_contact_name' ; 
	$sort_field{21}	= 'sender_contact_tel' ; 
	$sort_field{22}	= 'sender_postal_code' ; 	
	$sort_field{23}	= 'sender_suburb' ; 
	$sort_field{24}	= 'service_type_code' ; 
	$sort_field{25}	= 'shipment_is_insured' ; 
	$sort_field{26}	= 'shipper_reference' ; 
	$sort_field{27}	= 'account_nr' ; 
	$sort_field{28}	= 'additional_information' ; 
	$sort_field{29}	= 'pod' ; 
	$sort_field{30}	= 'tracking_detail_1' ; 
	$sort_field{31}	= 'tracking_detail_2' ; 
	$sort_field{32}	= 'waybill_date' ; 
	$sort_field{33}	= 'waybill_number' ; 
	
	$sort_field_additional_information{1}	= 'additional_info_code' ; 
	$sort_field_additional_information{2}	= 'additional_info_value' ; 
	$sort_field_additional_information{3}	= 'waybill_number' ; 
	
	$sort_field_tracking_detail{1}	= 'event_code' ; 
	$sort_field_tracking_detail{2}	= 'event_date' ; 
	$sort_field_tracking_detail{3}	= 'event_description' ; 
	$sort_field_tracking_detail{4}	= 'event_hub_code' ; 
	$sort_field_tracking_detail{5}	= 'event_time' ; 

	
	$lcol = 2 ; $fcol = 8 ;
	
	foreach my $row_cnt (sort keys %sort_field) {
 
		my $row = $sort_field{$row_cnt} ;
		my $val = $trackntrace{$row} ;
			
		if ($row eq 'booking_date'){
			# $val = '' ;
		} elsif ($row_ eq 'comments') {
			# $val = '' ;
		} elsif ($row_ eq 'consignee_name') {
			# $val = 'DEV' ;
		} elsif ($row eq 'consignor_name') {
			# $val = 'DARREN' ;
		} elsif ($row eq 'current_status') {
			# $val = 'Proof of delivery' ;
		} elsif ($row eq 'delivery_date') {
			# $val = '08052023' ;
			if ($val) {
				my ($day,$month,$year) = (substr($val,0,2),substr($val,2,2),substr($val,4,4)) ; 
				$day = sprintf("%02d",$day) ; $month = sprintf("%02d",$month) ;
				$month = Month_to_Text($month) ;
				$val = "$day $month $year" ;
			}
		} elsif ($row eq 'insured_value') {
			# $val = '0.00' ;
		} elsif ($row eq 'number_of_parcels') {
			# $val = '1' ;
		} elsif ($row eq 'receiver_address_line_1') {
			# $val = '15 SIM ROAD' ;
		} elsif ($row eq 'receiver_address_line_2') {
			# $val = '' ;
		} elsif ($row eq 'receiver_city') {
			# $val = 'KEMPTON PARK' ;
		} elsif ($row eq 'receiver_contact_name') {
			# $val = 'DARREN' ;
		} elsif ($row eq 'receiver_contact_tel_1') {
			# $val = '0834752596' ;
		} elsif ($row eq 'receiver_name') {
			# $val = '' ;
		} elsif ($row eq 'receiver_postal_code') {
			# $val = '1619' ;		
		} elsif ($row eq 'receiver_suburb') {
			# $val = 'POMONA' ;	
			$val = join(' ', map { ucfirst(lc($_)) } split(' ', $val));
		} elsif ($row eq 'sender_address_line_1') {
			# $val = '' ;		
		} elsif ($row eq 'sender_address_line_2') {
			# $val = '' ;		
		} elsif ($row eq 'sender_city') {
			# $val = 'KEMPTON PARK' ;	
			$val = join(' ', map { ucfirst(lc($_)) } split(' ', $val));
		} elsif ($row eq 'sender_contact_name') {
			# $val = 'DEV' ;	
			$val = join(' ', map { ucfirst(lc($_)) } split(' ', $val));
		} elsif ($row eq 'sender_contact_tel') {
			# $val = '0112302085' ;		
		} elsif ($row eq 'sender_postal_code') {	
			# $val = '1619' ;
		} elsif ($row eq 'sender_suburb') {
			# $val = 'POMONA' ;
			$val = join(' ', map { ucfirst(lc($_)) } split(' ', $val));
		} elsif ($row eq 'service_type_code') {
			# $val = 'EC' ;		
		} elsif ($row eq 'shipment_is_insured') {
			# $val = 'YES' ;		
			$val = ucfirst lc $val ;		
		} elsif ($row eq 'shipper_reference') {
			# $val = '' ;		
		} elsif ($row eq 'account_nr') {
			# $val = 'DCBDEVLP' ;			
		} elsif ($row eq 'additional_information') {
			$print_box_content_rows .= &common_min_form_label($row,'') ; 
			$print_box_content_rows .= qq~</div>~ ;
			foreach (sort keys %sort_field_additional_information) {
				my $row2 = $sort_field_additional_information{$_} ; 
				my $val2 = $trackntrace{additional_information}{$row2} ; 
				if ($row2 eq 'additional_info_code') {
					# $val2 = 'overrideDefaultSubcontractor' ;
				} elsif ($row2 eq 'additional_info_value') {
					# $val2 = 'no' ;
				} elsif ($row2 eq 'waybill_number') {
					# $val2 = 'Dn230505' ;
				}
				$row2 = ucfirst $row2 ;
				$print_box_content_rows .= &common_min_form_view("&nbsp;-&nbsp$row2",$val2,'') ;
			}
			next ; 
		}
		elsif ($row eq 'pod') {
			# $val = '' ;
		} elsif ($row =~ /tracking_detail/) { 
			my $tracking_detail_cnt = substr($row,-1) ; 
			$print_box_content_rows .= &common_min_form_label($row,'') ; 
			$print_box_content_rows .= qq~</div>~ ;
			foreach (sort keys %sort_field_tracking_detail) {
				my $row2 = $sort_field_tracking_detail{$_} ;
				my $val2 = $trackntrace{tracking_detail}{$row2}{$tracking_detail_cnt} ; 
				if ($row2 eq 'event_code') {
					# $val2 = ($tracking_detail_cnt eq '1') ? 'PODCRE' : 'WBLCRE' ;
				} elsif ($row2 eq 'event_date') {
					# $val2 = ($tracking_detail_cnt eq '1') ? '09052023' : '05052023' ;
					if ($val2) {
						my ($day,$month,$year) = (substr($val2,0,2),substr($val2,2,2),substr($val2,4,4)) ; 
						$day = sprintf("%02d",$day) ; $month = sprintf("%02d",$month) ;
						$month = Month_to_Text($month) ;
						$val2 = "$day $month $year" ;						
					}

				} elsif ($row2 eq 'event_description') {
					# $val2 = ($tracking_detail_cnt eq '1') ? 'Waybill has been PODed' : 'Waybill Dn230505 created in import' ;
				} elsif ($row2 eq 'event_hub_code') {
					# $val2 = ($tracking_detail_cnt eq '1') ? 'JNB' : 'JNB' ;
				} elsif ($row2 eq 'event_time') {
					# $val2 = ($tracking_detail_cnt eq '1') ? '0719' : '0718' ;
				}
				$row2 = ucfirst $row2 ;
				# $sort_field_tracking_detail{$_} = ucfirst $sort_field_tracking_detail{$_} ; 
				
				print "\n sort_field_tracking_detail : $sort_field_tracking_detail{$_}" ;
				
				$print_box_content_rows .= &common_min_form_view("&nbsp;-&nbsp$sort_field_tracking_detail{$_}",$val2,'') ;
			}
			next ; 
		}  elsif ($row eq 'waybill_date') {
			# $val = '05052023' ;
		} elsif ($row eq 'waybill_number') {
			# $val = 'Dn230505' ;
		} 
		
		$print_box_content_rows .= &common_min_form_view($row,$val,'') ;
	}
	
	
	
} #------------------------------------------------------------------------------------------

sub screen2 {

$trigger_jquery_raw .= qq(\$("#savebutt").click(function() {    
    var sc_val = \$("#selectAssigned_users").chosen().val() ;
    \$('#assigned-users').val(sc_val);    
    \$('#events-form').submit();
});) ;

&common_min_alert_type ;

&common_min_footer('id','') ;

&common_min_dialog ;
      
print <<ENDOFTEXT;
$dialog{'common'}{'head'}
    
<div id="content" class="col-lg-12 col-sm-12">
    <!-- content starts -->   
                       
    $print_top
    
    <div class="row">
        <div class="box col-md-12">
            $events_content
        </div>
        <!--/span-->
    </div><!--/row-->
    
    <div class="row">
        <div class="box col-md-12">
            <div class="box-inner">
                <div class="box-header well" data-original-title="">
                    <h2><i class="glyphicon glyphicon-tasks"></i> $boxtitle DCB Track & Trace</h2>                    
                </div>
                <div class="box-content">
                    $print_box_content_rows
                </div>
            </div>
        </div>
    </div><!--/row--> 
</div><!--/row-->
    
</div><!--/fluid-row-->
    
</div><!--/.fluid-container-->

<!-- external javascript -->

<style>body { font-size: 14px; }.control-label { font-size: 14px; }</style>
<link href="$useropts{'css'}/bootstrap-dialog.css" rel='stylesheet'>
$dialog{'common'}{'js'}
<script src="$useropts{'js'}/bootstrap-datetimepicker.min.js"></script>
<script src="$useropts{'js'}/jquery.form-validator.min.js"></script>
<script src="$useropts{'js'}/bootstrap-dialog.js"></script>

<script>

\$.validate(); 

\$(document).ready(function () { 
    $trigger_jquery
});

$trigger_jquery_raw

</script>

$print_footer_jscript
$print_footer_forms
</body>
</html>
ENDOFTEXT
#

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

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

ENDOFTEXT
#

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

sub reload_parent {
	
# my $jquery_raw = qq~parent.location.reload();
    # parent.\$("#alertbar").html("<div class='alert alert-success' id='alertmsg' role='alert'><i class='glyphicon glyphicon-ok'></i> <b>$nummsgs</b> new tickets!</div>");
    # parent.BootstrapDialog.closeAll() ;~;
	
if ($debug) { $jquery_raw = '' ; }
     
print <<ENDOFTEXT;
<script>

\$(function () {
    $jquery_raw
});

</script>
</body>
</html>
ENDOFTEXT
#

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

use db ;
use dcb ;
use common ;
use today ;

1;