#!/usr/bin/perl

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

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

use CGI::Carp qw(fatalsToBrowser);
use Fcntl qw(:flock);
use JSON::Repair ':all' ;
# use Time::Piece ;
use Date::Calc qw(Add_Delta_Days) ; 

require cfg ;

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

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

foreach $ARG (@ARGV) { ($par,$val) = split(/\=/,$ARG) ; $val  =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $param{$par} = $val ; }

# our $debug = 1 ;

# my $json_type = $param{json_type} ;
	
# e.g. https://itvadmin.co.za/cgi-bin/scripts/get/get_event_quote_conflicts.pl?
# our $debug = 1 ;

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

&load_json ;

print $json ;

exit ;

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

sub load_json {

	# if $param{table} eq 'users' ; 
	
	if (!$param{quote_id}) {
		print "[]" ;
		exit ;		
	}
	
	my @param_times_from = ($param{times_from} =~ /(\d+)/g) ? split(";",substr($param{date_from},11) . ';' . $param{times_from}) : (substr($param{date_from},11)) ;
	my @param_times_to   = ($param{times_to} =~ /(\d+)/g) ? split(";",$param{times_to} . ';' . substr($param{date_to},11)) : (substr($param{date_to},11)) ;
	
	my $day_cnt = 0 ;
	
	my ($p_year,$p_month,$p_day) = split("-",substr($param{date_from},0,10)) ;
	
	foreach (@param_times_from) {
		
		$p_month = sprintf("%02d",$p_month) ; 
		$p_day = sprintf("%02d",$p_day) ; 
		$day_cnt++ ;
		my $time_f = $_ ; 
		my $time_e = $param_times_to[$day_cnt-1] ; 
		$time_f =~ s/\://g ;
		$time_e =~ s/\://g ;
		$param_day_start{$day_cnt} = int("$p_year$p_month$p_day$time_f") ;
		$param_day_end{$day_cnt} = int("$p_year$p_month$p_day$time_e") ;
		($p_year,$p_month,$p_day) = Add_Delta_Days($p_year,$p_month,$p_day,1) ;	
	}
	
	our $table = 'event_quotes' ; my @sys_ids = () ; 

	# foreach (split(";",$param{system_ids})) {
		# next unless $_ ;
		# push @sys_ids,"`event_system_id_multiple` LIKE '%;$_;%' OR `event_system_id_multiple` LIKE '$_;%' OR `event_system_id_multiple` LIKE '%;$_'"
	# }
	
	# my $sys_ids_sql = join (" OR ", @sys_ids) ;
	
	# $sys_ids_sql = qq~($sys_ids_sql) AND ~ if $sys_ids_sql ;
	
	# $sys_ids_sql .= qq~`id` != '$param{quote_id}'~ ; 
	 # $sys_ids_sql .= qq~`id` != ''~ ; 
	
	 # my $sys_ids_sql = qq~`date_to` < '$param{date_from}' OR `date_from` > '$param{date_to}'~ ;
	
	my @event_ids_sql = () ; my @event_ids_arr = () ; my @event_ids_sql_2 = () ;
	
	foreach (split(";",$param{system_ids})) {
		 # push @event_ids_sql,"event_system_id_multiple LIKE '%;$_'" ;
		 # push @event_ids_sql,"event_system_id_multiple LIKE '%;$_;%'" ;
		 # push @event_ids_sql,"event_system_id_multiple LIKE '$_;%'" ;
		 push @event_ids_sql,"FIND_IN_SET($_,REPLACE(event_system_id_multiple,';',','))" ;
		 push @event_ids_sql_2,"id='$_'" ;
		 push @event_ids_arr,"$_"
	}
	my $event_ids_sql_str = join(" OR ",@event_ids_sql) ; 
	my $event_ids_sql_str_2 = join(" OR ",@event_ids_sql_2) ; 

	unless ($event_ids_sql_str) {
		print "[]" ;
		exit ;
	}
	 
	&db_min_ro($table,'id,ref,date_from,date_to,event_system_id_multiple,operator_ids,times_from,times_to,days_active,qty',"
		((`date_from` <= '$param{date_to}' 	AND '$param{date_to}' 	<= `date_to`) OR 
		(`date_from` <= '$param{date_from}' AND '$param{date_from}' <= `date_to`) OR 
		(`date_from` >= '$param{date_from}' AND '$param{date_to}' 	>= `date_to`) OR 
		(`date_from` <= '$param{date_from}' AND '$param{date_to}' 	<= `date_to`))
		AND id <> '$param{quote_id}'
		AND quote_rejected <> '1' 
		AND quote_cancelled <> '1'  
		AND ($event_ids_sql_str)
	",'','') ; 
	
	$json = '[' ; my $event_cnt = 0 ; 
	
	foreach my $id (sort keys %{$db{$table}}) {	
		$event_cnt++ ;
		last ;
	}
	
	unless ($event_cnt) {
		print "[]" ;
		exit ;	
	}
	
	&db_min_ro('event_systems','id,name,description',$event_ids_sql_str_2,'','') if $event_ids_sql_str_2 ; 
	
	my $param_start_time = $param{date_from} ;
	my $param_end_time = $param{date_to} ;
	$param_start_time =~ s/[T\-\:\s]//g;
	$param_end_time =~ s/[T\-\:\s]//g;

	foreach my $id (sort keys %{$db{$table}}) {
		
		next unless $id == 7067 ;
		
		next if !$db{$table}{$id}{days_active} && !$db{$table}{$id}{qty} ;
		
		my ($s_year,$s_month,$s_day) = split("-",substr($db{$table}{$id}{date_from},0,10)) ;
		
		my @times_from = () ; my @times_to = () ;
		
		push @times_from , substr($db{$table}{$id}{date_from},11) ;
		
		if ($db{$table}{$id}{times_to} =~ /(\d+)/g && $db{$table}{$id}{times_from} =~ /(\d+)/g && $db{$table}{$id}{qty} > 1) {
			 foreach (split(";",$db{$table}{$id}{times_from})) {
				 push @times_from,$_ if $_ ;
			 }
			 foreach (split(";",$db{$table}{$id}{times_to})) {
				 push @times_to,$_ if $_ ;
			 }
		}
		
		push @times_to , substr($db{$table}{$id}{date_to},11) ;
		
		my $event_to_json = qq~"event":"$db{$table}{$id}{ref} [$id]"~ ; my $system_json = qq~~ ; my $time_json = qq~~ ;
		
		# $json .= qq~{"event":"$db{$table}{$id}{ref} [$id]"~ ; 
		
		my $system_cnt = 0 ;
		
		foreach my $sys_id_in_table (split(";",$db{$table}{$id}{event_system_id_multiple})) {
			
			next unless $sys_id_in_table ;
			
			next unless grep { $_ == $sys_id_in_table } @event_ids_arr ;
			
			$system_cnt++ ;
			
			$system_json .= qq~,"system_$system_cnt":"$db{event_systems}{$sys_id_in_table}{name} [$db{event_systems}{$sys_id_in_table}{description}]"~ ;
			
			next if $system_cnt > 1 ;
			
			my $days_cnt = -1 ; my $day_num =  0 ;
			
			if ($db{$table}{$id}{times_from} && $db{$table}{$id}{times_from} =~ /(\d+)/g) {

				foreach my $day_active (split(";",$db{$table}{$id}{days_active})) {
					
					$days_cnt++ ;
					if ($days_cnt > 0) {
						($s_year,$s_month,$s_day) = Add_Delta_Days($s_year,$s_month,$s_day,1) ;
						$s_day = sprintf("%02d",$s_day) ; 
						$s_month = sprintf("%02d",$s_month) ; 
					}
					next unless $day_active ;
					my $start_time = "$s_year$s_month$s_day$times_from[$days_cnt]" ;
					my $end_time = "$s_year$s_month$s_day$times_to[$days_cnt]" ;
					$start_time =~ s/\://g ; $end_time =~ s/\://g ;
					
					if (($param_start_time <= $end_time && $start_time <= $param_start_time) || ($param_end_time <= $end_time && $start_time <= $param_end_time)) {
						
						$day_num = $days_cnt + 1 ; 
						my $date_interval = &common_write_date_interval("$s_year-$s_month-$s_day","$s_year-$s_month-$s_day") ;
						$date_interval .= " FROM $times_from[$days_cnt] TO $times_to[$days_cnt]" ;
						$time_json .= qq~,"date_interval_day_$day_num":"$date_interval"~ ;
			
					} elsif (($param_start_time < $start_time && $end_time < $param_end_time) || ($param_end_time <= $end_time && $start_time <= $param_start_time)) {
						
						foreach (keys %param_day_start) {

							if (($param_day_start{$_} <= $end_time && $start_time <= $param_day_start{$_}) || ($param_day_end{$_} <= $end_time && $start_time <= $param_day_end{$_}) || ($param_day_end{$_} <= $end_time && $start_time <= $param_day_start{$_}) || ($param_day_start{$_} < $start_time && $end_time < $param_day_end{$_})) {
								
								$day_num = $days_cnt + 1 ; 
								my $date_interval = &common_write_date_interval("$s_year-$s_month-$s_day","$s_year-$s_month-$s_day") ;
								$date_interval .= " FROM $times_from[$days_cnt] TO $times_to[$days_cnt]" ;
								$time_json .= qq~,"date_interval_day_$day_num":"$date_interval"~ ;								
			
							}
						}
					}
				}
				
			} elsif ($db{$table}{$id}{qty}) {

				for my $day_num (1 .. $db{$table}{$id}{qty}) {
					$days_cnt = $day_num - 1 ;
					if ($days_cnt > 0) {
						($s_year,$s_month,$s_day) = Add_Delta_Days($s_year,$s_month,$s_day,1) ;
						$s_day = sprintf("%02d",$s_day) ; 
						$s_month = sprintf("%02d",$s_month) ; 
					}
					my $start_time = "$s_year$s_month$s_day$times_from[0]" ;
					my $end_time = "$s_year$s_month$s_day$times_to[0]" ;
					$start_time =~ s/\://g ; $end_time =~ s/\://g ;
				
					if (($param_start_time <= $end_time && $start_time <= $param_start_time) || ($param_end_time <= $end_time && $start_time <= $param_end_time)) {
						
						my $date_interval = &common_write_date_interval("$s_year-$s_month-$s_day","$s_year-$s_month-$s_day") ;
						$date_interval .= " FROM $times_from[$days_cnt] TO $times_to[$days_cnt]" ;
						$time_json .= qq~,"date_interval_day_$day_num":"$date_interval"~ ;
	
					} elsif (($param_start_time < $start_time && $end_time < $param_end_time) || ($param_end_time <= $end_time && $start_time <= $param_start_time)) {
						
						foreach (keys %param_day_start) {

							if (($param_day_start{$_} <= $end_time && $start_time <= $param_day_start{$_}) || ($param_day_end{$_} <= $end_time && $start_time <= $param_day_end{$_}) || ($param_day_end{$_} <= $end_time && $start_time <= $param_day_start{$_}) || ($param_day_start{$_} < $start_time && $end_time < $param_day_end{$_})) {

								my $date_interval = &common_write_date_interval("$s_year-$s_month-$s_day","$s_year-$s_month-$s_day") ;
								$date_interval .= " FROM $times_from[$days_cnt] TO $times_to[$days_cnt]" ;
								$time_json .= qq~,"date_interval_day_$day_num":"$date_interval"~ ;								
								
							}
						}
					}
					
				}
			}
			$json .= qq~{$event_to_json$system_json$time_json},~ if $event_to_json && $system_json && $time_json ;
		} 
		# $json .= qq~{"user":"","system":"$system_name","event":"$db{$table}{$id}{ref}","interval":"$db{$table}{$id}{date_from} to $db{$table}{$id}{date_to}"},~ ; 
	}
	
	# our %overlap_dates = () ; 
	# # our %seen_this = () ; my %json_by_id = () ; 
	
	# # if ($param{system_ids}) {
		
		# # foreach my $id (sort keys %{$db{$table}}) {
			
			# # next if $id eq $param{quote_id} ;
			
			# # foreach my $sys_id_input (split(/;/,$param{system_ids})) {
				
				# # next unless $sys_id_input ;
				
				# # foreach my $sys_id_in_table (split(/;/,$db{$table}{$id}{event_system_id_multiple})) {
					
					# # next unless $sys_id_in_table ;
					
					# # if ($sys_id_input eq $sys_id_in_table) {
						
						# # next if $seen_this{$sys_id_input} ;
						# # $seen_this{$sys_id_input} = 1 ;
						# # my $system_name = qq~$db{event_systems}{$sys_id_input}{name}~ ;
						# # $system_name .= qq~ ($db{event_systems}{$sys_id_input}{description})~ if $db{event_systems}{$sys_id_input}{description} ;
						# # next unless $system_name ;
						# # # my $date_interval = &common_write_date_interval($db{$table}{$id}{date_from},$db{$table}{$id}{date_to}) ;
						
						# # $json .= qq~{"user":"","system":"$system_name","event":"$db{$table}{$id}{ref}","interval":"$db{$table}{$id}{date_from} to $db{$table}{$id}{date_to}"},~ ; 
						# # # $json_by_id{$id} .= qq~$json~ ; 
					# # }
				# # }		
			# # }
		# # }		
	# # }
	

	# # %seen_this = () ;
	
	# # if ($param{op_ids}) {
	
		# # foreach my $id (sort keys %{$db{$table}}) {
			
			# # next if $id eq $param{quote_id} ;
			
			# # foreach my $op_id_input (split(/,/,$param{op_ids})) {
				
				# # next unless $op_id_input ;
				
				# # foreach my $op_id_in_table (split(/,/,$db{$table}{$id}{operator_ids})) {
					
					# # next unless $op_id_in_table ;
					
					# # if ($op_id_in_table eq $op_id_input) {
						
						# # next if $seen_this{$op_id_input} ;
						# # $seen_this{$op_id_input} = 1 ;
						# # my $op_name = qq~$db{users}{$op_id_in_table}{name}~ ;
						# # next unless $op_name ;
						# # # my $date_interval = &common_write_date_interval($db{$table}{$id}{date_from},$db{$table}{$id}{date_to}) ;
						
						# # $json .= qq~{"user":"$op_name","system":"","event":"$db{$table}{$id}{ref}","interval":"$db{$table}{$id}{date_from} to $db{$table}{$id}{date_to}"},~ ; 
						
					# # }
				# # }
			# # }
		# # }
	# # }
	
	# &calibration_check_for_overlapping_dates('event_systems',"event_system_id_multiple",$param{date_from},$param{date_to},$param{system_ids},1) ;
	
	# $json = '[' ;
	
	# foreach my $sys_id (sort keys %overlap_dates) {
		
		# my $system_name = $db{event_systems}{$sys_id}{name} ; 
		# $system_name .= qq~ ($db{event_systems}{$sys_id}{description})~ if $db{event_systems}{$sys_id}{description} ; 
		# foreach my $event_id (keys %{$overlap_dates{$sys_id}}) {
			# $json .= qq~{"system":"$system_name","event":"$db{$table}{$event_id}{ref}","interval":"$overlap_dates{$sys_id}{$event_id}"},~ ; 
		# }
	# }
	
	if ($json =~ /\,/) { $json  = substr($json,0,-1) ; }

	$json  =~ s/\\//g ;
	$json .= ']' ;
	 
} #------------------------------------------------------------------------------------------       

use db ;
use common ;
# use calibration ; 

1;