#!/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;

&today;

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

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

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

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

&page_opts ;

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

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

&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;
	}
	
	if ($iaction eq 'save') {  	
		&common_min_load_params ;
		&update ; 
		&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 update {
	
	&schedule_payments_update("temp_calibrator") ; 

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

sub report_ifields {

	&schedule_payments_report_ifields("temp_calibrator") ; 
 
} #------------------------------------------------------------------------------------------

sub list_screen {
	
	our $count_outstanding = 0 ;
	our $temp_calibrator_payments_report = 1 ; 

	my $field3 = 'temp_calibrator' ; 
	
	$field3 =~ s/\_/ /g ;
	
	&db_min_ro('event_cost_items','id,name,rates,full_half_day',"`name` LIKE '$field3%' OR `name` LIKE 'temp support%'",'','') ;
	
	foreach my $id (keys %{$db{event_cost_items}}) {
		my @full_half_day = split(/\;/,$db{event_cost_items}{$id}{full_half_day}) ; 
		my @rates = split(/\;/,$db{event_cost_items}{$id}{rates}) ; 
		my $cnt = 0 ;
		foreach (@full_half_day) {
			$temp_support_rates{full_day} = $rates[$cnt] if $full_half_day[$cnt] eq '1' ;
			$temp_support_rates{half_day} = $rates[$cnt] if $full_half_day[$cnt] eq '2' ;
			$cnt++ ;
		}
		if ($db{event_cost_items}{$id}{name} eq 'Temp Calibrator Allocations') {
			$db{event_cost_items}{1}{rates} = $db{event_cost_items}{$id}{rates} ;
		}
	}
	
	&schedule_payments_list_screen("temp_calibrator") ; 
	
} #------------------------------------------------------------------------------------------

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

sub page_opts {

	our $glyphicon = 'list' ;
	our $lcpage    = 'temp-calibrator-payment' ;

	&common_page_name ;
	   
	our $table     = 'shift_operator_ids' ;
	$page_title    = 'Temp Operator Payments' ;

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

sub report_screen {
	
	&schedule_payments_report_screen("temp_calibrator") ; 

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

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 ;

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

$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="temp_calibrator_id"	value="$i{temp_calibrator_id}">
<input type="hidden" name="paid_status"			value="$i{paid_status}">~;

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

require _blank ;

exit ;

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

# sub screen4 {
	
# &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(){ \$('#temp-calibrator-payment-form').submit(); })

# </script>
# </body>

# <form role="form" id="tempcalibrator-payments-report-form" method="post" action="tempcalibrator-payments-report">
# <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="temp_calibrator_id"      value="$i{temp_calibrator_id}">
# <input type="hidden" name="paid_status"      value="$i{paid_status}">
# <input type="hidden" name="isaved"           value="$success">
# </form>

# </html>
# ENDOFTEXT
# #

# exit ;

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

use common ;
use report ;
use xlsxcreator ;
use today ;
use schedule ;

1;