#!/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://itvadmin.co.za/cgi-bin/scripts/get/get_typeahead_json.pl?json_type=additional_services

# our $debug = 1 ;

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

&update_ ;

exit ;

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

sub update_ {

	exit if !$param{analytics_id} || !$param{row_cnt} || !$param{reason} ;
	
	my $table = 'analytics_event_bookings' ;
	
	&db_min_ro($table,'1,cant_charge_client_reasons',"id='$param{analytics_id}'",'','') if $param{analytics_id} ; 
	
	my @reasons = ($db{$table}{1}{cant_charge_client_reasons}) ? split(/\:|;/,$db{$table}{1}{cant_charge_client_reasons}) : (undef) ; 
	
	%i = () ; 
	
	$i{reason} = $param{reason} ;
	
	$i{reason} =~ s/\:|;//g ;
	
	$i{reason} =~ s/\_new_line_character_//g ;
	
	print "\n reason : $i{reason}" ;
	
	$#reasons = $param{row_cnt}-1 if $#reasons < $param{row_cnt}-1 ;   # Pads with undef automatically
	
	@reasons = map { defined($_) ? $_ : "" } @reasons ;
	
	$reasons[$param{row_cnt}-1] = $i{reason} ;
	
	$i{cant_charge_client_reasons} = join(":|;",@reasons) ;
	
	$i{cant_charge_client_reasons} =~ s/\:\|;+$//g ;
	
	print "\n cant_charge_client_reasons : $i{cant_charge_client_reasons}" ;
	
	# &db_min_upd($table,"id='$param{analytics_id}'") if $param{analytics_id} ;

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

use db ;
use common ;

1;