#!/usr/bin/perl

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

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

# unless ($useropts{it}{$username}) { print "<<<<<<<<<<<< MAINTENANCE IN PROGRESS >>>>>>>>>" ; exit ; }

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

use CGI::Carp qw(fatalsToBrowser);
use CGI;
use Mail::Sendmail;
use Fcntl qw(:flock);
use File::Copy;
use File::Basename;
use Date::Calc qw(:all);
use Excel::Writer::XLSX;
use Time::Piece ; 
use LWP::UserAgent;
use XML::LibXML;
use LWP::Simple;
use XML::Simple ;
use JSON ;

&today;

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

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

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

$iaction = 'report' unless $iaction ; 

our $debug     = 1 ;
# our $testing = 1 ;
# our $testing = 1 ;
# our $testdcd = 1 ;

# if ($username ne 'handre') {
	# print "<< MAINTENANCE IN PROGRESS >>" ; exit ; 	
# }

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

&page_opts ;

#---------------------------------------------------------------------------------------------------------------------------------------------------------------------
	
our $srchscr = 1 ;

##DO NOT CHANGE THE ORDER OF THERE COLUMNS IT IS THE ORDER IN WHICH THEY ARE STORED IN THE DATABASE

our @sql_col_display = ("system","region","head_operator","system_test_date","status","equipment_checked","venue","comments","dates","special_arrangements") ; 

##DO NOT CHANGE THE ORDER OF THERE COLUMNS IT IS THE ORDER IN WHICH THEY ARE STORED IN THE DATABASE

&db_open_upd ;
	our $db_ignore_open_close = 1 ; # do one open and one close instead of repeating it
		if ($iaction eq 'report') {  	
			&report_screen
		} elsif ($iaction eq 'save') {  	
			&common_min_load_params ;
			&update ;
			&report_ifields;
			$iaction = 'report' ;
			&list_screen ;    
			exit if ($username eq 'handre' || $username eq 'rory') && $testing ;
			$s{no} = 1 ;
			&screen3 ;
		} elsif ($iaction eq 'list') {  
			&common_min_load_params ;
			&report_ifields;			
			&list_screen ;
			&screen3 ;
		}	
		
	$db_ignore_open_close = 0 ; # do one open and one close instead of repeating it
&db_close_conn ;

&common_min_action;

exit;

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

sub update {

	&db_min_ro('mobile_tracking_system','event_system_id,id,mobile_tracking_system_data','','','') ; 
	
	my %system_exists = () ; 
	
	foreach (keys %{$db{mobile_tracking_system}}) {
		$system_exists{$_} = 1 ;
	}
		
	my %ii = %i ; my %seen_event_system_id = () ; 
	
	foreach my $field (keys %ii) {

		next if ($field =~ /^update_/ && !$ii{$field}) || $field !~ /^update_/ ;
		
		local @a = split(/\_/,$field) ;
		my $event_system_id = $a[-1] ;
		$event_system_id = int($event_system_id) ;
		
		next if $seen_event_system_id{$event_system_id} ;
		$seen_event_system_id{$event_system_id} = 1 ;
		
		%i = () ; 
		foreach my $field_2 (@sql_col_display) {
			next if $field_2 eq 'system' ;
			my $field_name = qq~$field_2\_$event_system_id~ ; 
			$i{mobile_tracking_system_data} .= qq~$ii{$field_name}:;:~ ;
		}
		
		$i{mobile_tracking_system_data} =~ s/(?:\:;\:)+$// if $i{mobile_tracking_system_data} ;
		
		$i{mobile_tracking_system_data} = qq~~ if $i{mobile_tracking_system_data} !~ /[a-zA-Z0-9]/ ;
		
		$ignore{mobile_tracking_system_data} = ((!$i{mobile_tracking_system_data} && !$db{mobile_tracking_system}{$event_system_id}{mobile_tracking_system_data}) || ($i{mobile_tracking_system_data} eq $db{mobile_tracking_system}{$event_system_id}{mobile_tracking_system_data})) ? 1 : 0 ;

		# if ($db{mobile_tracking_system}{$event_system_id}{id}) {
		if ($system_exists{$event_system_id}) {
			&db_min_upd('mobile_tracking_system',"`event_system_id`='$event_system_id'") unless $ignore{mobile_tracking_system_data} ;
		} elsif (!$ignore{mobile_tracking_system_data}) {
			$i{event_system_id} = $event_system_id ;
			$i{id} = &db_min_get_max('mobile_tracking_system','id') ;
			&db_min_insert('mobile_tracking_system') ; 
		}
	}
	
	%i = %ii ;

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

sub report_screen {

	our $lcol = 3 ;
	our $fcol = 5 ;
	
	$print_box_content_rows .= &common_min_forms_start('report') ;
	
	# &common_min_select_opts('system_id',$table,'name',$i{client_id},'') ;
	
	my $def_val = ($i{system_type}) ? $i{system_type} : "all" ;
	
	my %def_selected = () ; 
	$def_selected{$def_val} = "SELECTED" ; 
	
	$opts{system_type} = qq~
		<option value='all' $def_selected{all}>All</option>
		<option value='solo' $def_selected{solo}>Solo</option>
		<option value='mobile' $def_selected{mobile}>Mobile</option>
	~ ; 
	
	$print_box_content_rows .= &common_min_form_select('system_type',$i{system_type}) ; 
	
	$i{region} = ($i{region}) ? $i{region} : "all" ;
 
	my %select_hashes = () ; 
	
	$select_hashes{region} = {1 => 'JHB',2 => 'PTA',3 => 'CPT',4 => 'KZN',5 => 'BLOEM',6 => 'EL',7 => 'PE',8 => 'GARDEN ROUTE',9 => 'LP',10 => 'MP',11 => 'NC',12 => 'NW',13 => 'INTERNATIONAL','all' => 'All'} ;  	
	
	$opts{region} = "<option " . join ("</option><option ", map { "value='$_'>$select_hashes{region}{$_}" } sort { $a <=> $b} keys %{$select_hashes{region}}) . "</option>" ;
	
	$opts{region} =~ s/value='\Q$i{region}\E'/value='$i{region}' SELECTED/g if $i{region} ;
		 
	$print_box_content_rows .= &common_min_form_select('region',$i{region}) ; 

	$print_box_content_rows .= &common_min_forms_end('','','list') ;

	&common_min_search_screen ;

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

sub report_ifields {
	
	if ($i{system_type} && $i{system_type} ne 'all') {
		push @report_sql, "(es.system_type = '$i{system_type}')" ; 
		my $system_type = ucfirst $i{system_type} ;
		push @report_results, "SYSTEM TYPE = '$system_type'" ;		
	} elsif ($i{system_type} eq 'all') {
		push @report_sql, "(es.system_type = 'mobile' OR es.system_type = 'solo')" ; 
		push @report_results, "SYSTEM TYPE = ('Mobile' OR 'Solo')" ;		
	}

	my %select_hashes = () ; 
	
	$select_hashes{region} = {1 => 'JHB',2 => 'PTA',3 => 'CPT',4 => 'KZN',5 => 'BLOEM',6 => 'EL',7 => 'PE',8 => 'GARDEN ROUTE',9 => 'LP',10 => 'MP',11 => 'NC',12 => 'NW',13 => 'INTERNATIONAL','all' => 'All'} ;  	
	
	if ($i{region} && $i{region} ne 'all') {
		push @report_sql, "(mts.mobile_tracking_system_data REGEXP '^$i{region}(:;:|\$)')" ; 
		push @report_results, "REGION = '$select_hashes{region}{$i{region}}'" ;		
	}
 
	$report_results_msg = uc join(', ', @report_results) ;

	if (not $report_results_msg) { 
		$error = qq~ENTER AT LEAST ONE SEARCH PARAMETER~ ; 
		&report_screen; 
	} elsif ($iaction eq 'save') {
		$isaved = qq~SUCCESSFULLY SAVED!~ ; 
	} else { 
		$isaved = qq~SELECT WHERE $report_results_msg~ ; 
	}
	
	our $srch_where_sql = join(' AND ',@report_sql) ; 
	
	# $srch_where_sql = qq~es.id=1~ ;
	
} #------------------------------------------------------------------------------------------

sub load_list_vars {
	
	my ($where) = @_ ; 
	
	&common_min_select_opts('head_operator','users','name','','','',"`user_type` = 'casual_c'") ;
	
	# &db_min_ro($table,"*","$where",'','') ; 
	
	$where = "WHERE $where" if $where  ;
	
	&db_min_raw("
		SELECT 
			es.*,
			mts.mobile_tracking_system_data
		FROM $table es
		LEFT JOIN mobile_tracking_system mts ON es.id = mts.event_system_id
		$where;
	");
	
	foreach $row (@$rows_array_ref) {        
		for (0 .. $col_cnt){
			next if $col_name{$_} eq 'lastupdate' ; 
			$db{$table}{@$row[0]}{$col_name{$_}} = @$row[$_] ;
		}    
	}	
	
} #------------------------------------------------------------------------------------------

sub list_screen {
	
	$print_box_content_rows .= &common_min_forms_start("$lcpage") ;

	&load_list_vars($srch_where_sql) ; 
	
	$fcol = 12 ; 
	
	our $xlsx_title_heading = "Mobile System Tracking Report" ;
	our $xlsxdir            = "mobile_system_tracking" ; 
	our $xlsxreportname     = "Mobile_System_Tracking_Report" ; 

	&report_xlsx_export_header("$xlsxreportname",$xlsxdir,'',$xlsx_title_heading) ;
	
	push @sql_col_display,"update" ;
	# push @sql_col_display,"" ;
	
	my @rev_sql_col_display = reverse @sql_col_display ;
	
	foreach (@rev_sql_col_display) { $lastchild++ ; $last_child{$_} = $lastchild ; } # &common_debug("last-child : $_ [$last_child{$_}]");
	
	$xlsxrow-- ;
	
	our $current_date = int("$now_year$now_mm$now_dd") ; our $found_editable = 0 ; 
	
	our %seen_field_id = () ; our %all_location_ids_per_system = () ; our $all_ids = qq~~ ;
	 
	local @month_names = ("","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec") ;
	
	our $quote_and_ops_ids = qq~~ ; 
	
	our @selects_with_default_ids = () ; our @table_with_default_ids = () ; our @dropdown_chosen_ids = () ; our @update_ids = () ;
	
	my $table_row = 0 ; my $placeholder_row_cnt = 0 ; 
	
	our @date_value_ids = () ; our @default_javascript_by_id = () ; our @all_update_field_ids = () ;
	
	my %select_hashes = () ; 
	# our @sql_col_display = ("system","region","head_operator","system_test_date","status","equipment_checked","venue","comments","dates","special_arrangements") ;

	$select_hashes{region} 				= {1 => 'JHB',2 => 'PTA',3 => 'CPT',4 => 'KZN',5 => 'BLOEM',6 => 'EL',7 => 'PE',8 => 'GARDEN ROUTE',9 => 'LP',10 => 'MP',11 => 'NC',12 => 'NW',13 => 'INTERNATIONAL'} ; 
	$select_hashes{status} 				= {1 => 'All Clear',2 => 'Support Needed',3 => 'Part Pending'} ; 
	$select_hashes{equipment_checked} 	= {1 => 'Yes',2 => 'No'} ; 
	$select_hashes{venue} 				= {1 => 'JHB HUB',2 => 'BLOEM HUB',3 => 'KZN HUB',4 => 'CPT HUB',5 => 'RMA HUB',6 => 'SCHOOL',7 => 'OPERATOR'} ; 
	
	foreach (keys %{$db{users}}) {
		$select_hashes{head_operator}{$_} = $db{users}{$_}{name} ;
	}
	
	$opts{region} = "<option " . join ("</option><option ", map { "value='$_'>$select_hashes{region}{$_}" } sort { $a <=> $b} keys %{$select_hashes{region}}) . "</option>" ;
	$opts{status} = "<option " . join ("</option><option ", map { "value='$_'>$select_hashes{status}{$_}" } sort { $a <=> $b} keys %{$select_hashes{status}}) . "</option>" ;
	$opts{equipment_checked} = "<option " . join ("</option><option ", map { "value='$_'>$select_hashes{equipment_checked}{$_}" } sort { $a <=> $b} keys %{$select_hashes{equipment_checked}}) . "</option>" ;
	$opts{venue} = "<option " . join ("</option><option ", map { "value='$_'>$select_hashes{venue}{$_}" } sort { $a <=> $b} keys %{$select_hashes{venue}}) . "</option>" ;
	
	# $opts{region}	= qq~<option value='1'>JHB</option><option value='2'>PTA</option><option value='3'>CPT</option><option value='4'>KZN</option><option value='5'>BLOEM</option><option value='6'>EL</option><option value='7'>PE</option><option value='8'>GARDEN ROUTE</option><option value='9'>LP</option><option value='10'>MP</option><option value='11'>NC</option><option value='12'>NW</option>~ ;
	# $opts{status}	= qq~<option value='1'>All Clear</option><option value='2'>Support Needed</option>~ ;
	# $opts{equipment_checked}	= qq~<option value='1'>Yes</option><option value='2'>No</option>~ ;
	# $opts{venue}	= qq~<option value='1'>JHB HUB</option><option value='2'>BLOEM HUB</option><option value='3'>KZN HUB</option><option value='4'>CPT HUB</option<option value='5'>RMA HUB</option<option value='6'>SCHOOL</option<option value='7'>OPERATOR</option>~ ;

	# rgb(255,165,0) ## orange
	# rgb(255,255,0) ## yellow
	# rgb(81,203,242) ## Blue
	# rgb(50,50,50) ## Black/Grey
	# rgb(249,13,13) ## Red
	# rgb(128,64,0) ## Brown
	
	# Bloem + NC - Orange
	# JHB + PTA - Yellow
	# CPT + Garden Route - Blue
	# KZN - Black/Grey
	# EL + PE - Red
	# LP + MP + NW - Brown
	
	my %region_background_color = () ; 
	
	$region_background_color{1}  = "rgb(255,255,0)" ;
	$region_background_color{2}  = $region_background_color{1} ;
	$region_background_color{3}  = "rgb(81,203,242)" ;
	$region_background_color{4}  = "rgb(115,117,117)" ;
	$region_background_color{5}  = "rgb(255,153,0)" ;
	$region_background_color{6}  = "rgb(249,13,13)" ;
	$region_background_color{7}  = $region_background_color{6} ;
	$region_background_color{8}  = "rgb(81,203,242)" ;
	$region_background_color{9}  = "rgb(172,115,57)" ;
	$region_background_color{10} = $region_background_color{9} ;
	$region_background_color{11} = $region_background_color{5} ;
	$region_background_color{12} = $region_background_color{9} ;
	# 153, 102, 51
	foreach my $id (sort {$a <=> $b} keys %{$db{$table}}) {
	
		$table_row++ ;

		$print_tbody .= qq~<tr id="$id">~  ;
		
		our %field = () ;

		our %ucfirstfield = () ;
		
		our $default_javascript = qq~~ ;
		$xlsxcol = 0 ; 
		
		$placeholder_row_cnt++ ;		
		
		my $col_cnt = -2 ; 
		
		my @default_vals = split(/\:;:/,$db{$table}{$id}{mobile_tracking_system_data}) ; 
		
		foreach (@sql_col_display) { 

			next unless $_ ; # blank for the buttons column
			
			$col_cnt++ ;
			
			$field{$_} = qq~$_\_$id~ if $_ ne 'system' ; 
			$ucfirstfield{$field{$_}} = ucfirst $field{$_} ;
			if ($_ eq 'system') {
				# DO nothing
			} elsif ($_ eq 'update') {
				$ucfirstfield{$field{$_}} = qq~checkbox~ . $ucfirstfield{$field{$_}}  ;
			} elsif ($_ eq 'system_test_date' || $_ eq 'comments' || $_ eq 'dates' || $_ eq 'special_arrangements') {
				$ucfirstfield{$field{$_}} = qq~textarea~ . $ucfirstfield{$field{$_}}  ;
			# } elsif ($_ eq 'date_due') {
				# $ucfirstfield{$field{$_}} = qq~input~ . $ucfirstfield{$field{$_}} ;
			} else {
				$ucfirstfield{$field{$_}} = qq~select~ . $ucfirstfield{$field{$_}} ;
				$opts{$field{$_}} = $opts{$_} ;
			} 
			
			# our $formatting = $format84 ;
			our $formatting = $format84 ;
			my $val        = $db{$table}{$id}{$_} ;
			our $val_min    = 'MUSTBEBLANK' ;
			our $align      = qq~ class="dt-center"~ ; 
			our $nowrap     = '' ;
			
			my $due_date = 0 ; 
			
			my $default_val_is_saved = ($default_vals[$col_cnt]) ? 1 : 0 ;
			
			my $result = $_ ; 
			
			$result =~ s/\_id//g ; 
			
			$result = join(' ', map { ucfirst $_ } split /\_/, $result);
			
			$preferred_placeholder{$field{$_}} = $result . ' ' . $id  ;
			$background_color = (($_ eq 'status' || $_ eq 'equipment_checked') && $default_vals[$col_cnt] eq '1') ? "rgb(46,204,113)" : (($_ eq 'status' || $_ eq 'equipment_checked') && $default_vals[$col_cnt] eq '2') ? "rgb(249,13,13)" : ($_ eq 'status' && $default_vals[$col_cnt] eq '3') ? "rgb(255,153,0)" : '' ;
			
			$background_color = ($_ eq 'region' && $default_vals[$col_cnt] && $region_background_color{$default_vals[$col_cnt]}) ? $region_background_color{$default_vals[$col_cnt]} : $background_color ;
			
			if ($_ eq 'system') {
				my $id_val = sprintf("%09d",$id) ;
				$val_min  = qq~$db{$table}{$id}{name} [$db{$table}{$id}{description}]~ ; 
				$val = qq~<span style="display:none;">$id_val</span>$val_min~ ; 
			} elsif ($_ eq 'region' || $_ eq 'head_operator' || $_ eq 'status' || $_ eq 'equipment_checked' || $_ eq 'venue') {
				# $opts{$field{$_}} =~ s/value='$default_vals[$col_cnt]'/value='$default_vals[$col_cnt]' SELECTED/g if $default_vals[$col_cnt] ;
				$val = &common_min_table_select($field{$_},$default_vals[$col_cnt],'',$table_row,$last_child{$_},$background_color,$default_val_is_saved,$_) ; 
				$val_min = $select_hashes{$_}{$default_vals[$col_cnt]} ;
			}  elsif ($_ eq 'system_test_date' || $_ eq 'comments' || $_ eq 'dates' || $_ eq 'special_arrangements') {
				$val = &common_min_table_textarea($field{$_},$default_vals[$col_cnt],'',$table_row,$last_child{$_},'',$default_val_is_saved) ; 
				$val_min = $default_vals[$col_cnt] ;
			} elsif ($_ eq 'update') {
				$val = &common_min_table_checkbox($field{update}) ; 
				chop $default_javascript if $default_javascript ;
				if ($default_javascript) {
					push @default_javascript_by_id, qq~"$id":{$default_javascript}~ ;
					push @all_update_field_ids,"#$ucfirstfield{$field{region}},#$ucfirstfield{$field{head_operator}},#$ucfirstfield{$field{system_test_date}},#$ucfirstfield{$field{status}},#$ucfirstfield{$field{equipment_checked}},#$ucfirstfield{$field{venue}},#$ucfirstfield{$field{comments}},#$ucfirstfield{$field{dates}},#$ucfirstfield{$field{special_arrangements}}" ;
				} 				
			}
			 
			if ($val_min eq 'MUSTBEBLANK') {
				$val_min = $val ;
			}
			$val_min =~ s/\MUSTBEBLANK//g ;
  
			$print_tbody .= qq~<td $align $nowrap>$val</td>~ ; 
			next if $_ eq 'update' ;
			 
			&xlsxcreator_write_xlsx($ws,$xlsxrow,$xlsxcol,$val_min,$formatting) ;
			$xlsxcol++ ;

		}
		
		$print_tbody .= qq~</tr>~ ;
		$xlsxrow++ if $id ne 'add_row' ; 	
		
		chop $system_location_ids if $system_location_ids ; chop $event_venue_ids if $event_venue_ids ;
	
	}
	
	&common_min_table_textarea_jquery ; 
	
	&common_min_table_validate_date_inputs_jquery ;

	&common_min_table_update_checkbox_col ;
	
	# &common_min_table_select_jquery("#$useropts{table_id} td:nth-last-child($last_child{region}),#$useropts{table_id} td:nth-last-child($last_child{head_operator}),#$useropts{table_id} td:nth-last-child($last_child{status}),#$useropts{table_id} td:nth-last-child($last_child{equipment_checked}),#$useropts{table_id} td:nth-last-child($last_child{venue})") if $iaction ne 'save' and $iaction ne 'delete' and $iaction ne 'report' ;
	
			# \$("#$useropts{table_id} tr th").slice(-11, -8).css("width","12.25%");
		# \$("#$useropts{table_id} tr th").slice(-7, -2).css("width","12.25%");
		# \$("#$useropts{table_id} tr th:nth-last-child($last_child{date_allocated})").css("width","8.25%"); 
		# \$("#$useropts{table_id} tr th:nth-last-child($last_child{update})").css("width","4%"); 
	
	# $trigger_jquery  .= qq~
		# \$("#selectRegion_1").val("13").trigger("chosen:updated") ;
	# ~ ;
	
	$trigger_jquery .= qq~
		\$("#$useropts{table_id} tr th:nth-last-child(1)").css("max-width","10px;"); 
	~ ;	
	
	$worksheet{$ws}->set_column("A:A",40);
	$worksheet{$ws}->set_column("B:G",20);
	$worksheet{$ws}->set_column("H:H",40);	
	$worksheet{$ws}->set_column("I:I",20);
	$worksheet{$ws}->set_column("J:J",100);
	
	&report_xlsx_export_footer('L',15,$xlsxdir) ;

	$fnsortorder = 'desc' ;
	
	&common_min_extra_crumb("$lcpage","Search Screen") ;
	
	$trigger_jquery_raw .= qq~
		\$("#savebutt").click(function() {
			let item_val ;
			$jquery_chosen_sel
			\$("#$lcpage-form").submit() ;
		}) ;
	~ ;
	
	$extra_form_footer = qq~
		<form role="form" id="logisticsform" method="post">
			<input type="hidden" id="logisticsaction" name="iaction" value="">
			<input type="hidden" name="date_from" value="$i{date_from}">
		</form>
	~ ;	
	#&nbsp;From&nbsp;Venue &nbsp;To&nbsp;Venue
	
	$trigger_jquery_raw .= qq~
		function toggleToFrom (ft) { 
			\$('#logisticsaction').val(ft);
			\$('#logisticsform').submit();
		}
	~; 
	

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

sub thead {
	
	&common_min_thead ;
	
} #-------------------------------------------------------------------------------

sub page_opts {

	our $glyphicon = 'list' ;
	our $lcpage    = 'mobile-tracking-systems' ;

	&common_page_name ;
	   
	# our $table     = 'mobile_tracking_system' ;
	our $table     = 'event_systems' ;
	$page_title    = 'Mobile Tracking Systems Report'  ;
	
	our $blank_action = 1 ; 

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

# sub screen2 {

# my $page = "mobile-tracking-system-report" ;
	
# # # if ($i{id} && $redirsave eq 'edit') {
# # if ($redirsave eq 'edit') {
	# # $input_hidden_id = qq~<input type="hidden" name="id" value="$i{id}">~ ; 
# # }

# # my $doc_ready_jquery = qq~\$(document).ready(function(){ \$('#$redirsave-form').submit(); })~ unless $username eq 'rory' ;

# # &common_min_table_select_jquery("#itv-table td:nth-last-child(4),#itv-table td:nth-last-child(4),#itv-table td:nth-last-child(5),#itv-table td:nth-last-child(8)") ;
# &common_min_table_select_jquery("#$useropts{table_id} td:nth-last-child($last_child{region}),#$useropts{table_id} td:nth-last-child($last_child{head_operator}),#$useropts{table_id} td:nth-last-child($last_child{status}),#$useropts{table_id} td:nth-last-child($last_child{equipment_checked}),#$useropts{table_id} td:nth-last-child($last_child{venue})") ;

# my $form_name = $redirsave . 'form' ;

# our $success = "SUCCESSFULLY SAVED" ; 

# &common_min_alert_type ;

# 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>
# <script>
	# \$(document).ready(function(){ \$('#$redirsave-form').submit(); })
# </script>
# </body>

# <form role="form" name="$form_name" id="$redirsave-form" method="post" action="$page">
# <input type="hidden" name="iaction"          value="$redirsave">
# <input type="hidden" name="isaved"           value="$success">
# $input_hidden_id
# </form>

# </html>
# ENDOFTEXT
# #

# exit ;

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

sub screen3 {
	
	foreach (@sql_col_display) {
		$custom_column_styles{$_} = qq~style="min-width:150px;"~ if $_ ne 'update' ;
	}
	
	&thead;

	if ($isaved) { $alert = &common_min_alert('success',"$ucpage $isaved!",'ok') ; } else { &common_min_alert_type ; }

	# my $page = "$lcpage\s" ;

	my $page = "mobile-tracking-system-report" ;

	if ($s{no}) { $page = $lcpage ; }

	&common_min_table('id',$page,'list') ;
	
	# &common_min_table_select_jquery("#itv-table td:nth-last-child(4),#itv-table td:nth-last-child(5),#itv-table td:nth-last-child(8)") ;
	&common_min_table_select_jquery("#$useropts{table_id} td:nth-last-child($last_child{region}),#$useropts{table_id} td:nth-last-child($last_child{head_operator}),#$useropts{table_id} td:nth-last-child($last_child{status}),#$useropts{table_id} td:nth-last-child($last_child{equipment_checked}),#$useropts{table_id} td:nth-last-child($last_child{venue})") ;
	
	our $savjqy = 1 ;

	# &common_min_extra_crumb("manage-$lcpage\s","Manage $ucfirstpage\s") ;

	$extra_form_fields .= qq~
		<input type="hidden" name="oaction"		value="$iaction">
		<input type="hidden" name="system_type"	value="$i{system_type}">
		<input type="hidden" name="region"		value="$i{region}">
	~;

	$print_box_content_rows .= &common_min_forms_end('','','save') ;

	require _blank ;

	exit ;

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

sub redirect_screen {
	
&common_min_alert_type ;

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>
<script>

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

</script>
</body>

<form role="form" id="$lcpage-form" method="post" action="$lcpage">
<input type="hidden" name="iaction"				value="report">
<input type="hidden" name="oaction"				value="report">

</form>

</html>
ENDOFTEXT
#

exit ;

# <input type="hidden" name="oaction"				value="$iaction">

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

use db ;
use today ;
use common ;
use report ; 
use xlsxcreator ;

1;