#!/usr/bin/perl

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

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

&today ;

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

use DBI;
use CGI::Carp qw(fatalsToBrowser);
use CGI;

@ARGV         				= split(/\\*\&/, $ENV{'QUERY_STRING'});
our $action       			= $ARGV[0] ;
our $selectfield  			= $ARGV[1] ;
our $analytics_id			= $ARGV[2] ;
our $row_cnt  				= $ARGV[3] ;
our $default_reason			= $ARGV[4] ;
our $parent_all_fixtures	= $ARGV[5] ;

exit if $parent_all_fixtures && !$analytics_id ;

$is_from_analytics_event = "" unless $is_from_analytics_event ;

# our $debug  = 1 ;
our $table    = 'analytics_event_bookings' ;

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

our $dialog = 1 ;

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

if ($iaction eq 'add'){
    &common_min_add_screen ;
    &screen1 ;
}
    
if ($iaction eq 'save'){
    &common_min_load_params ;
    &update ;
    &screen2 ;
}
	
&common_min_action;
   
exit ;    

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

sub update {

	&add_db_fields ; # $i{last_edited_by} = $userid ; $i{last_updated} = "$now_ccyy_mm_dd $now_hour:$now_min:$now_sec" ;

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

	# &db_min_upd($table,"id='$analytics_id'") if $analytics_id ;

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


sub add_screen {

	$box_header_title = 'Add Reason' ;
	$hidden_action    = 'save' ;
	$head_col_width   = 2 ;

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

sub add_db_fields {

	$ignore{iaction}   			= 1 ;
	$hidden{id}        			= 2 ; 
	$hidden{sel_field} 			= 2 ;
	$required{reason} 			= 1 ;
	$textarea{reason} 			= 1 ; 
	
	$db{$table}{''}{sel_field} 	= $selectfield if $selectfield ;
	$default_reason 			=~ s/\_new_line_character_/\n/g ;
	$default_reason 			=~ s/\\&nbsp;/ /g ;
	$default_reason 			=~ s/\%A0/ /g ;
	$default_reason 			=~ s/\%C2/ /g ;
	$default_reason 			=~ s/_blank_space_/ /g ;
	$db{$table}{''}{reason} 	= "$default_reason" ;
	
	$sort_field{1} 				= "reason" ;
	
} #------------------------------------------------------------------------------------------

sub thead {

	&common_min_thead ;
    
} #-------------------------------------------------------------------------------
 
sub screen1 {
	
	$trigger_jquery_raw .= qq~
		\$('#btn-close-customer').click(function (e) {
			parent.BootstrapDialog.closeAll() ;
		});
	~ ;

	&common_min_dialog_load_screen ;

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

sub screen2 {
	
	my $reason_tooltip = "$i{reason}" ; 
	
	$reason_tooltip =~ s/\R/<br>/g;
	$reason_tooltip =~ s/ /\&nbsp;/g;

	my $reason_default = "$i{reason}" ; 
	
	$reason_default =~ s/\R/_new_line_character_/g;
	$reason_default =~ s/ /_blank_space_/g ;
	
	# my $data_base_value = "$i{cant_charge_client_reasons}" ;
	
	# $data_base_value =~ s/\R/<br>/g;
	
	my $cant_charge_client_id = ($parent_all_fixtures) ? qq~cant_charge_client_$row_cnt\_$analytics_id~ : qq~cant_charge_client_fixtures_$row_cnt~ ;

	my $ucfirst_cant_charge_client_id = ucfirst $cant_charge_client_id ;

	my $reasons_field_name = ($parent_all_fixtures) ? "cant_charge_client_reasons_$analytics_id" : "cant_charge_client_reasons" ;
	
	$trigger_jquery_raw .= qq~
	
		let reasons = parent.\$("input[name='$reasons_field_name']").val() ;
		
		let reasons_arr = reasons ? reasons.split(":|;") : [undefined] ;
		
		let row_cnt = $row_cnt ;
		
		if (reasons_arr.length < row_cnt) {
			reasons_arr.length = row_cnt ;
			reasons_arr = reasons_arr.map(v => v !== undefined ? v : "") ;
		}
		
		reasons_arr[row_cnt - 1] = "$reason_default" ;
		let joined_reasons = reasons_arr.join(":|;") ;
	
	~ ;
	
	$trigger_jquery_raw .= qq~
	
		let analytics_id = "$analytics_id" ;
		
		let update_url = "$useropts{scripts}/get/get_analytics_charge_client.pl?analytics_id="+analytics_id+"&row_cnt="+row_cnt+"&reason=$reason_default" ;
		
		// console.log("update_url : "+update_url) ;
		
		\$.get(update_url) ; 
		
	~ if $analytics_id  ;
	
	$trigger_jquery_raw .= qq~
	
		parent.\$("input[name='$reasons_field_name']").val(joined_reasons) ;

		// if (parent.\$("#reason_icon_$row_cnt").html()) {
		// 	parent.\$("#reason_icon_$row_cnt").remove() ;
		// }
		
		if (parent.\$("#reason_icon_$row_cnt\_$analytics_id").html()) {
			parent.\$("#reason_icon_$row_cnt\_$analytics_id").remove() ;
		}
		
		let html_cant_charge = "&nbsp;&nbsp;<a href=javascript:dlgMdl('$useropts{scripts}/dialog/add_fixtures_charge_free_table_reason.pl?add&&$analytics_id&$row_cnt&$reason_default&$parent_all_fixtures','Add&nbsp;Reason','','medium-dialog'); class='btn btn-primary btn-xs' style='position:relative;padding:20px 20px 0px 0px;' title data-toggle='tooltip' data-placement='left' data-original-title='$reason_tooltip' id='reason_icon_$row_cnt\_$analytics_id'><i class='glyphicon glyphicon-question-sign' style='position:absolute;margin-top:2.5px;right:3.5px;'></i></a>" ;

		let checkbox_html = "<input name='$cant_charge_client_id' data-no-uniform='true' type='checkbox' id='checkbox$ucfirst_cant_charge_client_id' value='1'>" ;

		parent.\$("#c_$cant_charge_client_id").html(checkbox_html+""+html_cant_charge).trigger("chosen:updated") ;
		// parent.\$("#checkbox$ucfirst_cant_charge_client_id").after(html_cant_charge).trigger("chosen:updated") ;

		parent.\$("#checkbox$ucfirst_cant_charge_client_id").prop("checked",true) ;

		parent.\$('[data-toggle="tooltip"]').tooltip({html:true}) ;
		parent.BootstrapDialog.closeAll() ;
	~;
	
	&common_min_dialog_save_screen ;

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

use today ;
use dialog ;
use common ;

1;