#!/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 Image::Size;
use Image::Magick;
use File::Temp qw(tempfile);

&today;

# $now_ccyymmdd = "20250321" ;
# $now_dd = "21" ;

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

@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 ;
# our $testing = 1 ;

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

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

&page_opts ;

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


if ($iaction eq 'save') {  	
	&db_open_upd ;
		our $db_ignore_open_close = 1 ; # do one open and one close instead of repeating it
		&common_min_load_params ;
		&report_ifields;
		&update ;
		# &list_screen ;    
		$db_ignore_open_close = 0 ; # do one open and one close instead of repeating it
	&db_close_conn ;
	exit if $testing ;
	# &screen3;
	&redirect_screen;
}		

&db_open_ro ;
    our $db_ignore_open_close = 1 ; # do one open and one close instead of repeating it
	&report_screen if $iaction eq '' ; 
	
	if ($iaction eq 'report') {  	
		&common_min_load_params ;
		&report_ifields;
		&list_screen ;    
		&screen3;
	}
		
	&common_min_action;
	$db_ignore_open_close = 0 ; # do one open and one close instead of repeating it
&db_close_conn ;

exit;

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

sub validate {

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

sub insert {

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

sub update {
	
	my %seen_event_id = () ; 
	
	foreach (keys %i) {
		next if $_ !~ /^update_/ || ($_ =~ /^update_/ && !$i{$_}) ; 
		local @abc = split("_",$_) ;	
		$seen_event_id{$abc[-3]} = 1 ;
	}

	local $e_q_sql = join (" OR ",map { "id='$_'" } keys %seen_event_id) ;
	
	&db_min_ro($table,'id,event_system_id_multiple,date_from,date_to,event_system_calibration_ids,event_system_calibration_status',"$e_q_sql",'','') ; 
	
	$e_q_sql =~ s/id/event_quote_id/g ;
 
	&db_min_ro('calibration_status_report','event_quote_id,id,calibration_done,request_sent',"$e_q_sql",'','') ; 
	
	my %update_calibration_status_report = () ; 
	foreach (keys %{$db{calibration_status_report}}) {
		$update_calibration_status_report{$_} = 1 ;
	}
	
	my %ii = () ; my %iii = %i  ;
	
	my %calibration_done_by_system = () ; my %calibration_ids_hash = () ; my %calibration_status_hash = () ; my %request_sent_hash = () ; 
	
	foreach my $event_id (keys %seen_event_id) {

		my @event_system_ids                = split(/\;/,$db{$table}{$event_id}{event_system_id_multiple}) ; 
		
		my @calibration_ids                 = split(/\;/,$db{$table}{$event_id}{event_system_calibration_ids}) ; 
		my @event_system_calibration_status = split(/\;/,$db{$table}{$event_id}{event_system_calibration_status}) ;
		
		my @calibration_done_arr			= split(/\|/,$db{calibration_status_report}{$event_id}{calibration_done}) ; 
		my @request_sent_arr				= split(/\|/,$db{calibration_status_report}{$event_id}{request_sent}) ; 		
		
		my $system_cnt = 0 ; 
		
		foreach (@event_system_ids) {
			$system_cnt++ ;
			$calibration_ids_hash{cnt}{$event_id}{$_} 				= $system_cnt ;
			$calibration_status_hash{cnt}{$event_id}{$_} 			= $system_cnt ;
			$calibration_done_by_system{cnt}{$event_id}{$_} 		= $system_cnt ;
			$request_sent_hash{cnt}{$event_id}{$_} 					= $system_cnt ;
		}
		
		$system_cnt = 0 ;
		 
		foreach my $system_data (@calibration_ids) {
			$system_cnt++ ;
			next unless $system_data ; 
			if ($system_data =~ /~/) {
				($system_id,$system_data) = split(/\~/,$system_data) ; 
			} else {
				$system_id = $event_system_ids[$system_cnt - 1] ;
			}
			next unless $calibration_ids_hash{cnt}{$event_id}{$system_id} ; 
			my $day_cnt = 0 ; 
			foreach my $daily_data (split(/\-/,$system_data)) {
				$day_cnt++ ;
				$ii{$event_id}{$system_id}{$day_cnt}{calibrator_id} = $daily_data ;
			}
		}
		
		$system_cnt = 0 ; 
		
		foreach my $system_data (@event_system_calibration_status) {
			$system_cnt++ ;
			next unless $system_data ; 
			if ($system_data =~ /~/) {
				($system_id,$system_data) = split(/\~/,$system_data) ; 
			} else {
				$system_id = $event_system_ids[$system_cnt - 1] ;
			}
			
			next unless $calibration_status_hash{cnt}{$event_id}{$system_id} ;
			$calibration_status_hash{data}{$event_id}{$system_id} = $system_data ;
			my $day_cnt = 0 ; 
			foreach my $daily_data (split(/\-/,$system_data)) {
				$day_cnt++ ;
				$ii{$event_id}{$system_id}{$day_cnt}{calibration_status} = $daily_data ;
			}
		}

		$system_cnt = 0 ; 
		
		foreach my $system_data (@calibration_done_arr) {
			$system_cnt++ ;
			next unless $system_data ;
			my $system_id = 0 ; 
			my ($system_id,$system_data) = split(/\~/,$system_data) ; 
			next unless $calibration_done_by_system{cnt}{$event_id}{$system_id} ;
			$calibration_done_by_system{data}{$event_id}{$system_id} = $system_data ;
		}

		$system_cnt = 0 ; 
		
		foreach my $system_data (@request_sent_arr) {
			$system_cnt++ ;
			next unless $system_data ;
			my ($system_id,$system_data) = split(/\~/,$system_data) ; 
			next unless $request_sent_hash{cnt}{$event_id}{$system_id} ;
			my $day_cnt = 0 ; 
			foreach my $daily_data (split(/\;/,$system_data)) {
				$day_cnt++ ;
				$ii{$event_id}{$system_id}{$day_cnt}{request_sent} = $daily_data ;
			}
		}

	}
	
	my @col_name = ("calibration_status","calibrator_id","request_sent") ;

	foreach (keys %i) {
		
		local @a = split(/\_/,$_) ;
		
		my $upd_field = "update_$a[-3]\_$a[-2]\_$a[-1]"  ; 
		next unless $i{$upd_field} ;

		if (substr($_,0,19) eq 'calibration_status_') {
			$ii{$a[-3]}{$a[-2]}{$a[-1]}{calibration_status} = $i{$_} ;
		} elsif (substr($_,0,14) eq 'calibrator_id_') {
			$ii{$a[-3]}{$a[-2]}{$a[-1]}{calibrator_id} = $i{$_} ; 
		} elsif (substr($_,0,13) eq 'request_sent_') {
			$ii{$a[-3]}{$a[-2]}{$a[-1]}{request_sent} = $i{$_} ;
		} elsif (substr($_,0,14) eq 'head_operator_') {
			# $ii{$a[-3]}{$a[-2]}{$a[-1]}{head_operator} = $i{$_} ;
		}
	}

	my %iii = %i  ;
	
	my %iv = () ; 
	
	my $calid_field		= 'event_system_calibration_ids' ;
	my $calstatus_field = 'event_system_calibration_status' ;
	my $request_field	= 'request_sent' ;

	foreach my $event_id (keys %ii) {
		
		%i  = () ; %iv = () ; 

		my $use_system_id_not_count = 0 ; 
		
		my @ed_from = &common_split_sql_time($db{$table}{$event_id}{date_from}) ;
		my @ed_to   = &common_split_sql_time($db{$table}{$event_id}{date_to}) ; 
		
		&common_date_array("$ed_from[0]$ed_from[1]$ed_from[2]","$ed_to[0]$ed_to[1]$ed_to[2]") ;
		
		$sys_cnt = 0;
		foreach my $system_id (sort { $request_sent_hash{cnt}{$event_id}{$a} <=> $request_sent_hash{cnt}{$event_id}{$b} } keys %{$request_sent_hash{cnt}{$event_id}}) {

			my $line = qq~~ ; my $sys_day = 0 ;
			foreach my $_date (@common_date_array) {
				$sys_day++;
				# my $upd_field = "update_$event_id\_$system_id\_$sys_day"  ; 
				# $iv{$request_field} .= ($ii{$event_id}{$system_id}{$sys_day}{request_sent}) ? "$ii{$event_id}{$system_id}{$sys_day}{request_sent}-" : ($request_sent_arr[$sys_day-1] && !$iii{$upd_field}) ? "$request_sent_arr[$sys_day-1]-" : '-' ;
				$line .= "$ii{$event_id}{$system_id}{$sys_day}{request_sent};" ;

			}
			$line =~ s/;+$// ;
			
			$iv{$request_field} .= qq~$system_id\~$line~ if $line ;
			$iv{$request_field} .= qq~|~ ;
			
		}
		$iv{$request_field} =~ s/\|+$// ;

		$sys_cnt = 0 ;
		
		foreach my $system_id (sort { $calibration_ids_hash{cnt}{$event_id}{$a} <=> $calibration_ids_hash{cnt}{$event_id}{$b} } keys %{$calibration_ids_hash{cnt}{$event_id}}) {
			
			$sys_cnt++ ;
			
			my $sys_day = 0 ;

			my $line = qq~~ ;

			foreach my $_date (@common_date_array) {
				$sys_day++;
				# my $upd_field = "update_$event_id\_$system_id\_$sys_day"  ; 
				# $line .= ($ii{$event_id}{$system_id}{$sys_day}{calibrator_id}) ? "$ii{$event_id}{$system_id}{$sys_day}{calibrator_id}-" : ($calibrator_ids_per_event_day[$sys_day-1] && !$iii{$upd_field}) ? "$calibrator_ids_per_event_day[$sys_day-1]-" : '-' ;
				$line .= "$ii{$event_id}{$system_id}{$sys_day}{calibrator_id}-" ;
				# print "\n calibrator_id ($event_id,$system_id,$sys_day) : $ii{$event_id}{$system_id}{$sys_day}{calibrator_id}" ;
			}
			$line =~ s/-+$// ;
			$i{$calid_field} .= qq~$system_id\~$line~ if $line ; 
			$i{$calid_field} .= qq~;~ ;
		}
		$i{$calid_field} =~ s/;+$// ;
		
		$sys_cnt = 0;
		
		foreach my $system_id (sort { $calibration_status_hash{cnt}{$event_id}{$a} <=> $calibration_status_hash{cnt}{$event_id}{$b} } keys %{$calibration_status_hash{cnt}{$event_id}}) {
			
			$sys_cnt++ ;
			
			my @calibrator_statuses_per_event_day = split(/\-/,$calibration_status_hash{data}{$event_id}{$system_id}) ;
			
			my $sys_day = 0 ;
			
			my $calibration_done_str = qq~~ ; 
			
			my @calibration_done_arr = split(/\;/,$calibration_done_by_system{data}{$event_id}{$system_id}) ;
			
			my $line = qq~~ ;
			
			# $i{$calstatus_field} .= qq~$system_id\~~ ;
			
			foreach my $_date (@common_date_array) {
				$sys_day++;
				my $upd_field = "update_$event_id\_$system_id\_$sys_day"  ; 
				# $line .= ($ii{$event_id}{$system_id}{$sys_day}{calibration_status}) ? "$ii{$event_id}{$system_id}{$sys_day}{calibration_status}-" : ($calibrator_statuses_per_event_day[$sys_day-1] && !$iii{$upd_field}) ? "$calibrator_statuses_per_event_day[$sys_day-1]-" : '-'  ;
				$line .= "$ii{$event_id}{$system_id}{$sys_day}{calibration_status}-" ;
				$calibration_done_str .= ($iii{$upd_field} && $ii{$event_id}{$system_id}{$sys_day}{calibration_status} eq '3' && $calibrator_statuses_per_event_day[$sys_day-1] ne '3') ? qq~$now_year-$now_mm-$now_dd $now_hour:$now_min:$now_sec;~ : ($iii{$upd_field} && $ii{$event_id}{$system_id}{$sys_day}{calibration_status} ne '3') ? qq~;~ : qq~$calibration_done_arr[$sys_day - 1];~ ;
			}
			
			$line =~ s/\-+$// ;
			
			$i{$calstatus_field} .= qq~$system_id\~$line~ if $line ;
			$i{$calstatus_field} .= qq~;~ ;
			
			$calibration_done_str =~ s/;+$// ;
			
			$iv{calibration_done} .= qq~$system_id\~$calibration_done_str~ if $calibration_done_str ;
			$iv{calibration_done} .= qq~|~ ;
			
		}
		
		$i{$calstatus_field} =~ s/;+$// ;
		
		$iv{calibration_done} =~ s/\|+$// ;
		
		$ignore{$calid_field} = (!$i{$calid_field} && !$db{$table}{$event_id}{$calid_field} || ($i{$calid_field} eq $db{$table}{$event_id}{$calid_field})) ? 1 : 0 ;
		$ignore{$calstatus_field} = ((!$i{$calstatus_field} && !$db{$table}{$event_id}{$calstatus_field}) || ($i{$calstatus_field} eq $db{$table}{$event_id}{$calstatus_field})) ? 1 : 0 ;

		&db_min_upd($table,"`id`='$event_id'") if !$ignore{$calid_field} || !$ignore{$calstatus_field} ;

		%i = %iv ;

		$ignore{$request_field} = ((!$i{$request_field} && !$db{calibration_status_report}{$event_id}{$request_field}) || ($i{$request_field} eq $db{calibration_status_report}{$event_id}{$request_field})) ? 1 : 0 ;
		$ignore{calibration_done} = ((!$i{calibration_done} && !$db{calibration_status_report}{$event_id}{calibration_done}) || ($i{calibration_done} eq $db{calibration_status_report}{$event_id}{calibration_done})) ? 1 : 0 ;
		
		next if $ignore{$request_field} && $ignore{calibration_done} ;

		$i{user_id} = $userid ;
		$i{last_updated} = "$now_year-$now_mm-$now_dd $now_hour:$now_min:$now_sec" ;	
		
		if ($update_calibration_status_report{$event_id}) {
			&db_min_upd('calibration_status_report',"event_quote_id='$event_id'") ;
		} else {
			$i{id} = &db_min_get_max('calibration_status_report','id') ;
			$i{id} = 1 unless $i{id} ;
			$i{event_quote_id} = $event_id ;
			&db_min_insert('calibration_status_report') ;
		}
	}
			
	%i = %iii ;

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

sub report_ifields {
	
	&load_search_vars ;
		
	if ($i{date_from} and $i{date_to}) { # check dates
		my $date_from_check = $i{date_from} ;
		   $date_from_check =~ s/\-//g ;
		my $date_to_check = $i{date_to} ;
		   $date_to_check =~ s/\-//g ;
		if ($date_from_check > $date_to_check) { 
			$error = qq~'$i{date_from}' > '$i{date_to}'~ ; &report_screen ; 
		}
		
		my @srch_ed_from = &common_split_sql_time($i{date_from}) ;
		my @srch_ed_to   = &common_split_sql_time($i{date_to}) ; 
		
		&common_date_array("$srch_ed_from[0]$srch_ed_from[1]$srch_ed_from[2]","$srch_ed_to[0]$srch_ed_to[1]$srch_ed_to[2]") ;
		
		@report_sql_or = () ;	
		
		foreach my $_srch_date (@common_date_array) {
			my $srch_ccyy_mm_dd = substr($_srch_date,0,4) . '-' . substr($_srch_date,4,2) . '-' . substr($_srch_date,6,2) ;
			push @report_sql_or, "('$srch_ccyy_mm_dd 23:59:59' >= date_from AND '$srch_ccyy_mm_dd 00:00:00' <= date_to)"
		}
		
		my $sql_or = join(' OR ',@report_sql_or) ;
		push @report_sql, "($sql_or)" if $sql_or ; 
		
		# push @report_sql, "((`date_from` >= '$i{date_from} 00:00:00' AND `date_from` <= '$i{date_to} 23:59:59') OR (`date_to` >= '$i{date_from} 00:00:00' AND `date_to` <= '$i{date_to} 23:59:59'))" ;
		push @report_results, "(BETWEEN '$i{date_from}' AND '$i{date_to}')" ;
	} elsif (not $i{date_from}) {
		$error = qq~PLEASE ENTER A START DATE!~ ; 
		&report_screen; 
	} elsif (not $i{date_to}) {
		$error = qq~PLEASE ENTER AN END DATE!~ ; 
		&report_screen; 
	}
	
	if ($i{calibrator_id} and $i{calibrator_id} ne 'all') { 
		push @report_results,"`CALIBRATOR` = '$db{users}{1}{name}'" ;
		push @report_sql, "
			(
				`event_system_calibration_ids` LIKE '%;$i{calibrator_id};%' OR 
				`event_system_calibration_ids` LIKE '%;$i{calibrator_id}' 	OR 
				`event_system_calibration_ids` LIKE '$i{calibrator_id};%' 	OR 
				`event_system_calibration_ids` LIKE '%-$i{calibrator_id};%' OR 
				`event_system_calibration_ids` LIKE '%;$i{calibrator_id}-%' OR
				`event_system_calibration_ids` LIKE '%-$i{calibrator_id}-%' OR	
				`event_system_calibration_ids` LIKE '$i{calibrator_id}-%' 	OR		
				`event_system_calibration_ids` LIKE '%-$i{calibrator_id}' 	OR 	 		
				`event_system_calibration_ids` LIKE '%~$i{calibrator_id}' 	OR 	 		
				`event_system_calibration_ids` LIKE '%~$i{calibrator_id}-%' OR 	 		
				`event_system_calibration_ids` LIKE '%~$i{calibrator_id};%' 	 		
			)
		" ; 
	} elsif ($i{calibrator_id} and $i{calibrator_id} eq 'all') {
		push @report_results,"`CALIBRATORS` = 'ALL'" ;
	} else {
		# push @report_sql, "(`user_id` = '')" ; 
	}
	
	if ($i{calibration_status} and $i{calibration_status} ne 'all') {
		push @report_sql, "
			(
				`event_system_calibration_status` LIKE '%;$i{calibration_status};%' OR 
				`event_system_calibration_status` LIKE '%;$i{calibration_status}' 	OR 
				`event_system_calibration_status` LIKE '$i{calibration_status};%' 	OR 
				`event_system_calibration_status` LIKE '%-$i{calibration_status};%' OR 
				`event_system_calibration_status` LIKE '%;$i{calibration_status}-%' OR
				`event_system_calibration_status` LIKE '%-$i{calibration_status}-%' OR	
				`event_system_calibration_status` LIKE '$i{calibration_status}-%' 	OR		
				`event_system_calibration_status` LIKE '%-$i{calibration_status}' 	OR		
				`event_system_calibration_status` LIKE '%~$i{calibration_status}' 	OR		
				`event_system_calibration_status` LIKE '%~$i{calibration_status}-%' OR		
				`event_system_calibration_status` LIKE '%~$i{calibration_status};%'				
			)
		" ; 
		push @report_results,"`STATUS` = '$db{calibration_status}{$i{calibration_status}}{status}'" ;
	}
		
	$report_results_msg = uc join(', ', @report_results) ;

	if (not $report_results_msg) { 
		$error = qq(ENTER AT LEAST ONE SEARCH PARAMETER); 
		&report_screen; 
	} else { 
		$isaved = qq(SELECT WHERE $report_results_msg) ; 
	}
	
	our $srch_where_sql = join(' AND ',@report_sql) ; 
 
} #------------------------------------------------------------------------------------------

sub load_search_vars {
	
	if ($i{calibrator_id} and $i{calibrator_id} ne 'all') { 
		&db_min_ro('users','1,name',"`id` = '$i{calibrator_id}'",'','') ;
	}
	
	&db_min_ro('calibration_status','id,status','','','') ;
	
	our $calibration_status = 1 ; 
	
} #------------------------------------------------------------------------------------------

sub list_screen {
	
	$print_box_content_rows .= &common_min_forms_start("$lcpage") ;
	
	our $calibration_report = 1 ;
		
	&calibration_load_list_vars("$srch_where_sql") ;
 
	$fcol = 12 ; 
	
	my $report = 1 ; 
	
	our $xlsxdir = "calibration_report" ; 
	
	our $xlsxreportname = "Calibration_Report" ; 
 
	my $date = &common_write_date_interval($i{date_from},$i{date_to}) ;
	
	our $xlsx_title_heading = "Calibration Report $date" ;
	
	my $found_after_date = 0 ; 
	my $today_val = int("$now_year$now_mm$now_dd") ; 
	
	my @date_from_inp = split(/ /,$i{date_from}) ; 
	my ($year_inp1,$month_inp1,$day_inp1) = split(/\-/,$date_from_inp[0]) ; 
	my $date_inp_val1 = int("$year_inp1$month_inp1$day_inp1") ; 
	
	my @date_to_inp = split(/ /,$i{date_to}) ; 
	my ($year_inp2,$month_inp2,$day_inp2) = split(/\-/,$date_to_inp[0]) ; 
	my $date_inp_val2 = int("$year_inp2$month_inp2$day_inp2") ; 
			
	foreach (keys %{$db{$table}}) {
		my @date_from = split(/ /,$db{$table}{$_}{date_from}) ; 
		my ($year_1,$month_1,$day_1) = split(/\-/,$date_from[0]) ; 
		my $date_val1 = int("$year_1$month_1$day_1") ; 
		my @date_to = split(/ /,$db{$table}{$_}{date_to}) ; 
		my ($year_2,$month_2,$day_2) = split(/\-/,$date_to[0]) ; 
		my $date_val2 = int("$year_2$month_2$day_2") ; 
		$found_after_date = 1 if ($today_val <= $date_val1 && $today_val <= $date_inp_val1) || ($today_val <= $date_val2 && $today_val <= $date_inp_val2) ; 
	}
	
	my $table2 = 'calibration_status_report' ;
	
	&db_min_ro($table2,'event_quote_id,request_sent,upload_names','','','') ; 

	foreach my $event_id (keys %{$db{$table2}}) {

		my @request_sent = split(/\|/,$db{$table2}{$event_id}{request_sent}) ;  

		my $max_cnt = 0 ;  
		$max_cnt = scalar @request_sent ;  

		if ($max_cnt > 0) {
		
			foreach my $sys_cnt (0 .. $max_cnt-1) {
				
				my ($system_id1,$data1) = split(/\~/,$request_sent[$sys_cnt]) ; 
				my $event_sys_id = $system_id1 ;
				my @request_sent_day_splt 	= split(/\;/,$data1) ; 
				my $max_nr_days = 0 ; 
				$max_nr_days = scalar @request_sent_day_splt ;

				foreach my $day_cnt (1 .. $max_nr_days) {
					$default_vaules{request_sent}{$event_id}{$event_sys_id}{$day_cnt} = $request_sent_day_splt[$day_cnt-1] ;
				}
			}
		}
	}
	
	&calibration_build_table_and_or_excel(1) ; 
	
	$custom_column_styles{request_sent} = qq~style="width:105px;min-width:105px;max-width:105px;"~ ;
	
	$fnsortorder = 'asc' ;
	
	&common_min_extra_crumb("$lcpage","Search Screen") ;
 
	$trigger_jquery_raw .= qq~
		\$("#savebutt").click(function() {
			\$("#$lcpage-form").submit() ;
		}) ;
	~ ;
	
} #------------------------------------------------------------------------------------------

sub set_tick {
	
	my ($color,$tooltip) = @_ ;
		
	$tick{green}  = qq~<span style="display:none;">2</span><span class="label label-success"><i $tooltip class="glyphicon glyphicon-ok"></i></span>~ ;
	$tick{orange} = qq~<span style="display:none;">1</span><span class="label label-warning"><i $tooltip class="glyphicon glyphicon-ok"></i></span>~ ;
	$tick{red}    = qq~<span style="display:none;">0</span><span class="label label-danger"><i $tooltip class="glyphicon glyphicon-ok"></i></span>~ ;

	return ($tick{$color}) ;
			
} #------------------------------------------------------------------------------------------

sub set_cross {
	
	my ($color,$tooltip) = @_ ;
		
	$tick{green}  = qq~<span style="display:none;">2</span><span class="label label-success"><i $tooltip class="glyphicon glyphicon-remove"></i></span>~ ;
	$tick{orange} = qq~<span style="display:none;">1</span><span class="label label-warning"><i $tooltip class="glyphicon glyphicon-remove"></i></span>~ ;
	$tick{red}    = qq~<span style="display:none;">0</span><span class="label label-danger"><i $tooltip class="glyphicon glyphicon-remove"></i></span>~ ;

	return ($tick{$color}) ;
			
} #------------------------------------------------------------------------------------------

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

sub page_opts {

	our $glyphicon = 'list' ;
	our $lcpage    = 'calibration-report' ;

	&common_page_name ;
	   
	our $table     = 'event_quotes' ;
	$page_title    = 'Calibration Report' ;

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

sub report_screen {

	our $lcol = 3 ;
	our $fcol = 5 ;

	$print_box_content_rows .= &common_min_forms_start('report') ;

	my ($sql_next_day) = &common_add_delta_days(1) ;
	my ($next_year,$next_month,$next_day) = split(/\-/,$sql_next_day) ;
	$print_box_content_rows .= &common_min_form_datepicker('date_from',"$next_year-$next_month-$next_day") ; #unless $i{date_from} ;  
	$print_box_content_rows .= &common_min_form_datepicker('date_to',"$next_year-$next_month-$next_day") ;

	$selected{calibration_status}{all} = 'SELECTED' ;
	if ($i{calibration_status}) {
		$selected{calibration_status}{$i{calibration_status}} = 'SELECTED' ;
		$selected{calibration_status}{all} = '' ;
	} 
	
	&db_min_ro('calibration_status','id,status','','','') ;
	
	# $opts{calibration_status} .= qq~<option value="-1" $selected{calibration_status}{-1}>None</option>~ ; 
	foreach (keys %{$db{calibration_status}}) {
		$opts{calibration_status} .= qq~<option value="$_" $selected{calibration_status}{$_} >$db{calibration_status}{$_}{status}</option>~ ; 
	}
	$opts{calibration_status} .= qq~<option value="all" $selected{calibration_status}{all} >All</option>~ ; 
	
	$preferred_title{calibration_status} = "Status" ;
	
	$preferred_title{calibrator_id} = "Calibrator" ;
	
	&db_min_ro('users',"id,name,username","(user_type = 'support' OR user_type = 'manager' OR user_type = 'casual_calibrator') AND name NOT LIKE 'Rory%'",'','') ;

	$selected{calibrator_id}{all} = 'SELECTED' ;
	
	if ($i{calibrator_id}) {
		$selected{calibrator_id}{$i{calibrator_id}} = 'SELECTED' ;
		$selected{calibrator_id}{all} = '' ;
	} 
	
	foreach (sort {$db{users}{$a}{name} cmp $db{users}{$b}{name}} keys %{$db{users}}) {
		next if $useropts{it}{$db{users}{$_}{username}} ;
		$opts{calibrator_id} .= qq~<option value="$_" $selected{calibrator_id}{$_}>$db{users}{$_}{name}</option>~ ; 
	}
	$opts{calibrator_id} .= qq~<option value="all" $selected{calibrator_id}{all}>All</option>~ ; 

	$print_box_content_rows  .= &common_min_form_select('calibration_status','') ; 
	$print_box_content_rows  .= &common_min_form_select('calibrator_id','') ; 
	$print_box_content_rows  .= &common_min_forms_end('','','report') ;

	&common_min_search_screen ;

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

sub screen3 {

&thead;

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

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

my $page = "event-quotes" ;

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

&common_min_table('id',$page,'list') ;

our $savjqy = 1 ;

$extra_form_fields .= qq~
<input type="hidden" name="date_from" 			value="$i{date_from}">
<input type="hidden" name="date_to"				value="$i{date_to}">
<input type="hidden" name="calibrator_id"		value="$i{calibrator_id}">
<input type="hidden" name="calibration_status" 	value="$i{calibration_status}">~;

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

$useropts{'common'}{'css'} .= qq~tbody tr td { font-size: 10px; } ~ ;

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="date_from"        	value="$i{date_from}">
<input type="hidden" name="date_to"          	value="$i{date_to}">
<input type="hidden" name="calibrator_id"      	value="$i{calibrator_id}">
<input type="hidden" name="calibration_status"	value="$i{calibration_status}">
<input type="hidden" name="isaved"				value="$success">
</form>

</html>
ENDOFTEXT
#

exit ;

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

use common ;
use common_min ;
use report ;
use xlsxcreator ;
use today ;
use calibration ;

1;