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

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 ; }
	
# my $json_type = $param{json_type} ;
	
# e.g. https://sssadmin.co.za/cgi-bin/sss/scripts/get/get_typeahead_json.pl?json_type=additional_services

# our $debug = 1 ;

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

&load_json ;

print $json ;

exit ;

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

sub load_json {
	
	# return unless $param{supplier_type_id} ;
	
	# &db_min_ro('cities',"id,name","`region_id`='$param{region_id}'",'','') if $param{region_id} ;
	# &db_min_ro('cities',"id,name","",'','') if !$param{region_id} ;
	
	exit if !$param{poc_name} && !$param{poc_id} ;
	
	my $sql_where = ($param{poc_id}) ? "`id`='$param{poc_id}'" : ""  ;
	$sql_where .= ($param{poc_id} && $param{poc_name}) ? " OR " : "" ;
	$sql_where .= ($param{poc_name}) ? "`name`='$param{poc_name}'" : "" ;
	
	$json = '' ;
	
	&db_open_ro('aisa') ;
		$db_ignore_open_close = 1 ;
			&db_min_ro('event_quotes_poc',"id,name,contact_nr","$sql_where",'','') ; 
			foreach (keys %{$db{event_quotes_poc}}) {
				$json .= qq~{"id":"$_","contact_nr":"$db{event_quotes_poc}{$_}{contact_nr}"},~ if $db{event_quotes_poc}{$_}{contact_nr} ; 
			}
		$db_ignore_open_close = 0 ;
	&db_close_conn ;
	
	chop $json if $json ;
	$json = qq~[$json]~ ;
	
} #------------------------------------------------------------------------------------------       

use db ;
use common ;

1;