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

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 {
	
	&db_min_ro('event_systems','id,name,description','','','') if $param{system_ids} ; 
	# if $param{table} eq 'event_systems' ; 
	
	&db_min_ro('users','id,name',"`user_type` LIKE 'casual%'",'','') if $param{op_ids} ; 
	# if $param{table} eq 'users' ; 
	
	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}'~ ;
	
	&db_min_ro($table,'id,ref,date_from,date_to,event_system_id_multiple,operator_ids',"
		(`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`)
	",'','') ; 
	
	
	# foreach my $id (keys %{$db{$table}}) {
		
	# }
	
	$json = '[' ;
	
	# 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;