#!/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'});
$action       		= $ARGV[0] ;
$selectfield  		= $ARGV[1] ;

# our $debug  = 1 ;
our $table	= 'all_fixtures_analysts' ;
our $dialog = 1 ;

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

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

&db_open_ro ;
    our $db_ignore_open_close = 1 ; # do one open and one close instead of repeating it
	
		if ($iaction eq 'add'){
			&common_min_add_screen ;
			&screen1 ;
		}
			
		if ($iaction eq 'save'){
			&common_min_load_params ;
			&validate ;
			&insert ;
			&screen2 ;
		}
			
		&common_min_action;
		  
	$db_ignore_open_close = 0 ; 
&db_close_conn ; 

  
exit ;    

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

sub insert {

	&add_db_fields ; # $i{last_edited_by} = $userid ; $i{last_updated} = "$now_ccyy_mm_dd $now_hour:$now_min:$now_sec" ;
	
	&validate ;
	
	$ignore{sel_field} = 1 ;
	
	$i{id} = &db_min_get_max($table,'id') ;

	&db_min_insert($table) ;

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

sub validate {
	
	# called from common_min_action

	my $exists   = '' ;

	# $i{name} = &common_fix_str($i{name}) ;

	&db_min_ro($table,'id,name',"coding_provider_id='$i{coding_provider_id}'",'','') ;

	foreach my $id (keys %{$db{$table}}) {
		if (lc $db{$table}{$id}{name} eq lc $i{name}) {
			$exists = 1 ;
		}
	}
	 
	if ($exists) {
		$alert = &common_min_alert('warning',"'$i{name}' AS A FIXTURES ANALYST NAME FOR THIS CODING PROVIDER ALREADY EXISTS!",'warning-sign') ;
		%col_name = () ;
		&common_min_add_screen;
		&screen1;
    }

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

sub add_screen {

	$box_header_title = "Add Fixtures Analyst" ;
	$hidden_action    = 'save' ;
	$head_col_width   = 2 ;
	
} #------------------------------------------------------------------------------------------

sub add_db_fields {
	
	$ignore{iaction}    				= 1 ;
	$hidden{id}         				= 2 ; 
	$hidden{sel_field}  				= 2 ;
	$required{name}     				= 1 ;
	$required{coding_provider_id}		= 1 ;
	$sort_field{1} 						= "name" ;
	$sort_field{2} 						= "coding_provider_id" ;
	$select{coding_provider_id}			= 1 ;
	
	if ($i{name}) {
		$trigger_jquery_raw .= qq~
			\$("input[name='name']").val("$i{name}") ;
		~ ;
	}
	
	if ($i{coding_provider_id}) {
		$trigger_jquery_raw .= qq~
			\$("#selectCoding_provider_id").val("$i{coding_provider_id}").trigger("chosen:updated") ;
		~ ;
	}
	
	$trigger_jquery_raw .= qq~\$("#selectCoding_provider_id").chosen({ allow_single_deselect:false,search_contains:true });~ ;
	
	if ($usertype eq 'coding_provider') {
		&db_min_ro('users','id,name,username',"id='$userid'",'','') ; 
	} else {
		&db_min_ro('users','id,name,username',"user_type='coding_provider'",'','') ; 
	}
	
	foreach (keys %{$db{users}}) {
		my $selected = ($_ eq $userid) ? "SELECTED" : "" ;
		$opts{coding_provider_id} .= qq~<option value='$_' $selected>$db{users}{$_}{name} [$db{users}{$_}{username}]</option>~ ; 
	}
	
} #------------------------------------------------------------------------------------------

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 {
	
	$i{name} =~ s/\'/\\'/g ;
	$i{name} =~ s/\"/\\"/g ;
	
	$trigger_jquery_raw = qq~

		function get_row_nr (id,nr_of_characters_in_id_to_determine_row) {

			let lastIndex = id.lastIndexOf('_');
			lastIndex = parseInt(lastIndex) ;
			let row_nr_1 = id.substring(1+lastIndex); 
			
			if (!nr_of_characters_in_id_to_determine_row || nr_of_characters_in_id_to_determine_row == 1) {
				return row_nr_1 ; 
			}
			
			let id_2 = id.substring(0,lastIndex); 
			lastIndex = id_2.lastIndexOf('_');
			lastIndex = parseInt(lastIndex) ;
			let row_nr_2 = id_2.substring(1+lastIndex); 
			
			if (nr_of_characters_in_id_to_determine_row == 2) {
				return row_nr_2+"_"+row_nr_1 ; 
			}
			
		}	
			
		let input_coding_provider = "$i{coding_provider_id}" ;
		
		const table_ = parent.\$('#$useropts{table_id}').DataTable()._('tr') ;
		
		console.log("table_.length : "+table_.length) ; 
		
		for (let i = 1 ; i <= table_.length ; i++) {
			
			let \$td = parent.\$("#$useropts{table_id} tr:eq("+i+") td:nth-last-child(3)") ;
			let \$selects = \$td.find("select") ;
			
			console.log("i : "+i) ; 
			console.log("1 selects.length : "+\$selects.length) ; 
			
			if (!\$selects.length) {
				continue ;
			}
			
			console.log("1 selects.length : "+\$selects.length) ; 
			
			\$selects.each(function () {
				let row_nr = get_row_nr (this.id,2) ; 

				if (input_coding_provider == parent.\$("#selectCoding_provider_"+row_nr).val()) {
					parent.\$("#"+this.id).append("<option value='$i{id}'>$i{name}</option>").trigger("chosen:updated") ;
				}
			});
		}
			
		parent.BootstrapDialog.closeAll() ;
	
	~ ;
	
	&common_min_dialog_save_screen ;

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

use today ;
use dialog ;
use common ;

1;