#!/usr/bin/perl

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

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

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

&today;

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

@ARGV   = split(/\\*\&/, $ENV{'QUERY_STRING'});
$action = $ARGV[0] ;

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

our $debug = 1 ;
our $testing = 1 ;

our $open_new_tab = qq~target="_blank"~ ; # opens booking page in a new tab

#-------- page opts --------------------------------------------------------------------------------------------------------------------------------------------------

&page_opts ;

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

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

&db_switch_conn('sss') ;
 
# &db_open_ro ;
	# $db_ignore_open_close = 1 ;
		&common_min_action;
	$db_ignore_open_close = 0 ; 
&db_close_conn ;


exit;

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

sub validate {

	my $exists   = '' ;

	$i{name} = &common_fix_str($i{name}) ;
	
	if (length($i{name}) > 75) { $alert = &common_min_alert('danger',"YOUR TEAM NAME CONTAINS TOO MANY CHARACTERS!",'warning-sign') ; %col_name = () ; &load_vars ; &common_min_add_screen ; &common_min_screen1 ; exit ; }
	
	&db_min_ro($table,'*',"name = '$i{name}'",'name','') ;

	foreach my $id (keys %{$db{$table}}) { 
		if (lc $db{$table}{$id}{name} eq lc $i{name}) { 
			$exists = 1 ; 
			last ;
		} 
	}

	if ($exists) { $alert = &common_min_alert('danger',"'$i{name}' AS A TEAM NAME ALREADY EXISTS!",'warning-sign') ; %col_name = () ; &load_vars ; &common_min_add_screen ; &common_min_screen1 ; exit ; }

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

sub insert {

	&add_db_fields ;
	
	$i{id} = &db_min_get_max($table,'id') ;

	$ignore{name}		= 1 ; 
	$ignore{region_id}	= 1 ;
	$ignore{country_id}	= 1 ;
	
	&db_min_insert($table) ;
	
	$ignore{name}		= "" ; 
	$ignore{region_id}	= "" ;
	$ignore{country_id}	= "" ;
	
} #------------------------------------------------------------------------------------------

sub update {

	unless ($i{id}) { $error = qq(NO ID) ; return ; }

	&edit_db_fields ;

	# &db_min_ro('teams','1,region_id,aisa_client_id',"id='$i{id}'",'','') ; 

	$ignore{name}		= 1 ; 
	$ignore{region_id}	= 1 ;
	$ignore{country_id}	= 1 ;
	
	&db_min_upd($table,"id='$i{id}'") ;

	$ignore{name}		= '' ; 
	$ignore{region_id}	= '' ;
	$ignore{country_id}	= '' ;

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

sub load_vars {
	
} #------------------------------------------------------------------------------------------

sub list_screen {
	
	&db_switch_conn('aisa');
	
	&db_min_ro('customers','id,name','','','') ;
	
	&db_min_ro('regions','*','','','') ;
	
	&db_min_ro('countries','id,name','','','') ;
	
	&db_min_ro('analytics_event_bookings',"id,fixtures_saved_values,client_id","fixtures_saved_values IS NOT NULL",'','') ;
	
	foreach my $analytics_id (keys %{$db{analytics_event_bookings}}) {
		
		# 1;2025-06-03 08:00:00;5;open;a;3404;1;3404;s;r;s;r|2;2025-06-03 08:00:00;6;u19;a;6046;1;6046;p;s;p;s
		
		my @row_split_1 = split(/\|/,$db{analytics_event_bookings}{$analytics_id}{fixtures_saved_values}) ; 
		
		foreach (@row_split_1) { 
			my @row_split_2 = split(/\;/,$_) ; 
			$venue_id_used{$row_split_2[5]} = 1 if $row_split_2[5] ;
			$venue_id_used{$row_split_2[7]} = 1 if $row_split_2[7] ; 
			$client_team_combo_used{$db{analytics_event_bookings}{$analytics_id}{client_id}}{$row_split_2[5]} = 1 if $db{analytics_event_bookings}{$analytics_id}{client_id} && $row_split_2[5]  ;
		}
	}
	
	&db_switch_conn('sss');
	
	&db_min_ro('event_quotes',"DISTINCT team_ids AS 'team_ids'","team_ids <> 0",'','') ;

	foreach my $_id (keys %{$db{event_quotes}}) {
		if ($_id =~ /;/) { ## 13,32
			foreach (split(/\;/,$_id)) {
				$venue_id_used{$_} = 1 ;
			}
		} else {
			$venue_id_used{$_id} = 1 ;
		}
	}
 
	&db_min_ro($table,'*','','','') ;

	foreach my $id (sort {$db{$table}{$a}{name} cmp $db{$table}{$b}{name}} keys %{$db{$table}}) {
		
		my $edit_butt = qq~&nbsp;<a class="btn btn-default btn-xs" href="#" title="Request user rights in order to edit." data-toggle="tooltip"><i class="glyphicon glyphicon-edit icon-white"></i></a>~ ;   
		my $del_butt  = qq~&nbsp;<a class="btn btn-default btn-xs" href="#" title="Request user rights in order to delete." data-toggle="tooltip"><i class="glyphicon glyphicon-trash icon-white"></i></a>~ ;  
		
		if ($glod_user_level >= 3) { 
			$edit_butt = qq~&nbsp;<a class="btn btn-info btn-xs" href="javascript:editMinItem('$db{$table}{$id}{id}');"><i class="glyphicon glyphicon-edit icon-white"></i></a>~ ; 
			if ($client_team_combo_used{$db{$table}{$id}{aisa_client_id}}{$id}) {
				$edit_butt = qq~&nbsp;<a class="btn btn-default btn-xs" href="#" title="This Team Name as a Home Team Client Combination is Used in an Analytics Event" data-toggle="tooltip"><i class="glyphicon glyphicon-edit icon-white"></i></a>~ ;   
			}
		}	
		
		if ($glod_user_level >= 3) { 
			$del_butt  = qq~&nbsp;<a class="btn btn-danger btn-xs" href="javascript:deleteMinItem('$db{$table}{$id}{city}','$db{$table}{$id}{id}')"><i class="glyphicon glyphicon-trash icon-white"></i></a>~ ; 
		}
		
		if ($venue_id_used{$id}) { 
			# $edit_butt = qq~&nbsp;<a class="btn btn-default btn-xs" href="#" title="In system use!" data-toggle="tooltip"><i class="glyphicon glyphicon-edit icon-white"></i></a>~ ;   
			$del_butt  = qq~&nbsp;<a class="btn btn-default btn-xs" href="#" title="In system use!" data-toggle="tooltip"><i class="glyphicon glyphicon-trash icon-white"></i></a>~ ;  
		}
		
		my $region_name = ($db{$table}{$id}{region_id}) ? "$db{regions}{$db{$table}{$id}{region_id}}{name} [$db{regions}{$db{$table}{$id}{region_id}}{code}]" : "" ; 
		my $matched_region_name = ($db{$table}{$id}{matched_region_id}) ? "$db{regions}{$db{$table}{$id}{matched_region_id}}{name} [$db{regions}{$db{$table}{$id}{matched_region_id}}{code}]" : "" ; 
		my $customer_name = $db{customers}{$db{$table}{$id}{aisa_client_id}}{name} ;
		
		my $country_name = ($db{$table}{$id}{region_id}) ? $db{countries}{$db{regions}{$db{$table}{$id}{region_id}}{country_id}}{name} : ($db{$table}{$id}{matched_region_id}) ? $db{countries}{$db{regions}{$db{$table}{$id}{matched_region_id}}{country_id}}{name} : "" ;
		
		$print_tbody .= qq~
			<tr id="$id">
				<td>$id</td>
				<td>$db{$table}{$id}{name}</td>
				<td>$customer_name</td>
				<td>$region_name</td>
				<td>$matched_region_name</td>
				<td>$country_name</td>
				<td nowrap>
					$edit_butt
				</td>
			</tr>
		~ ;
	}
	&db_switch_conn('aisa');
	# $extra_edit_hidden_inputs .= qq~<input name="" value=>~ ; 

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

sub add_db_fields {
	
	&sort_fields;
		
	&hidden_db_fields;

	# $ignore{id} = 1 ;

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

sub edit_db_fields {
	
	&hidden_db_fields;
	
	&sort_fields;

	$hidden{id} = 1 ; # 1 = bypass for db update

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

sub hidden_db_fields {
	
	$ignore{iaction}	= 1 ;
	# $required{region_code}  = 1 ;
	# $required{address_line_1}  = 1 ;
	# $required{address_line_2}  = 1 ;
	# $required{address_line_3}  = 1 ;
	# $required{address_line_4}  = 1 ;
	# $required{suburb}  = 1 ;
	# $required{postal_code}  = 1 ;
	
} #------------------------------------------------------------------------------------------

sub sort_fields {
	
	%sort_field = () ;

	# $sort_field{1}  = 'region_code' ; $preferred_title{region_code} = 'Region Code' ;
	
	$sort_field{2}  = 'name' ; $preferred_title{name} = 'Name' ;
	$sort_field{3}  = 'aisa_client_id' ; 
	$sort_field{4}  = 'region_id' ; # $preferred_title{region_id} = 'Region' ; 
	$sort_field{5}  = 'country_id' ; # $preferred_title{region_id} = 'Region' ;
	
	# $sort_field{4}  = 'address_line_2' ; 
	# $sort_field{5}  = 'address_line_3' ; 
	# $sort_field{6}  = 'address_line_4' ; 
	# $sort_field{7}  = 'suburb' ; 
	# $sort_field{8}  = 'postal_code' ; 
	
} #-------------------------------------------------------------------------------

sub add_screen {

	# called from common_add_screen

	&select_opts ;

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

sub edit_screen {

	&select_opts ;
 
} #------------------------------------------------------------------------------------------

sub select_opts {

	our %venue_id_used = () ;
	
	if ($i{id}) {
		
		&db_min_ro('event_quotes',"DISTINCT team_ids AS 'team_ids'","`team_ids` <> 0 AND `team_ids` LIKE '%$i{id}%'",'','') ;

		foreach my $_id (keys %{$db{event_quotes}}) {
			
			next if $venue_id_used{$db{event_quotes}{$_id}{team_ids}} ; 
			my @split = split(/\;/,$db{event_quotes}{$_id}{team_ids}) ;  
			foreach (@split) {
				$venue_id_used{$_} = 1 ;
			}
		}
		
		&db_switch_conn('aisa');
		
		&db_min_ro('analytics_event_bookings',"id,fixtures_saved_values","fixtures_saved_values IS NOT NULL AND fixtures_saved_values LIKE '%;$i{id};%'",'','') ;
		
		# &db_min_ro('regions',"id,name,code","",'','') ;
		&common_min_select_opts('region_id','regions','name',$db{$table}{$i{id}}{region_id},'','','','','','','name') ;
		
		$default_country = ($db{$table}{$i{id}}{region_id}) ? $db{regions}{$db{$table}{$i{id}}{region_id}}{country_id} : ($db{$table}{$i{id}}{matched_region_id}) ? $db{regions}{$db{$table}{$i{id}}{matched_region_id}}{country_id} : "" ; 
		
		&common_min_select_opts('country_id','countries','name',$default_country,'','','','','','','name') ;
		
		&db_switch_conn('sss');
		
		foreach my $analytics_id (keys %{$db{analytics_event_bookings}}) {
			
			my @row_split_1 = split(/\|/,$db{analytics_event_bookings}{$analytics_id}{fixtures_saved_values}) ; 
			
			foreach (@row_split_1) { 
				my @row_split_2 = split(/\;/,$_) ; 
				$venue_id_used{$row_split_2[5]} = 1 ;
				$venue_id_used{$row_split_2[7]} = 1 ; 
			}
		}
	}

	if ($venue_id_used{$i{id}}) {
		
		if ($db{$table}{$i{id}}{aisa_client_id}) { 	
			&db_switch_conn('aisa');
			&db_min_ro("customers","1,name","id='$db{$table}{$i{id}}{aisa_client_id}'",'','') ;
			&db_switch_conn('sss');
			$db{$table}{$i{id}}{aisa_client_id} = $db{customers}{1}{name} ;
		} else {
			$db{$table}{$i{id}}{aisa_client_id} = "N/A" ;
		}
		
		if ($db{$table}{$i{id}}{region_id}) {
			
			$db{$table}{$i{id}}{country_id}	= "$db{countries}{$db{regions}{$db{$table}{$i{id}}{region_id}}{country_id}}{name}" ;
			$db{$table}{$i{id}}{region_id} 	= "$db{regions}{$db{$table}{$i{id}}{region_id}}{name} [$db{regions}{$db{$table}{$i{id}}{region_id}}{code}]" ;
			
			$required{region_id}  			= 0 ;
			$select{region_id} 				= '' ; $select{country_id} = '' ;			
			$input{region_id} 				= 1 ; $input{country_id} = 1 ;			
			$readonly{region_id} 			= "READONLY" ;			
			$readonly{country_id} 			= "READONLY" ;	
			
		} elsif ($db{$table}{$i{id}}{matched_region_id} && !$db{$table}{$i{id}}{region_id}) {
			
			$db{$table}{$i{id}}{country_id}	= "$db{countries}{$db{regions}{$db{$table}{$i{id}}{matched_region_id}}{country_id}}{name}" ;
			$db{$table}{$i{id}}{region_id} 	= "$db{regions}{$db{$table}{$i{id}}{region_id}}{name} [$db{regions}{$db{$table}{$i{id}}{matched_region_id}}{code}]" ;
			
			$required{region_id}  			= 0 ;
			$select{region_id} 				= '' ; $select{country_id} = '' ;			
			$input{region_id} 				= 1 ; $input{country_id} = 1 ;			
			$readonly{region_id} 			= "READONLY" ;			
			$readonly{country_id} 			= "READONLY" ;	
 
			# # $opts{region_id} =~ s/value="$db{$table}{$i{id}}{matched_region_id}"/value="$db{$table}{$i{id}}{matched_region_id}" SELECTED/g  ;

			# # if ($is_schools_manager || $is_operator) {
				# # @report_sql_or = () ;
				# # foreach $_reg_id (keys %{$glob_regids{$userid}}) {
					# # push @report_sql_or, "$_reg_id"
				# # }
				# # if (grep { $_ eq "$db{$table}{$i{id}}{matched_region_id}" } @report_sql_or) {
					
				# # } else {
					# # $select{region_id} = '' ; 
					# # $select{country_id} = '' ; 
					# # $required{region_id} = 0 ; 
					# # $readonly{region_id} = "READONLY" ; 
					# # $readonly{country_id} = "READONLY" ; 
					# # $input{region_id} = 1 ; 
					# # $input{country_id} = 1 ;
					# # $db{$table}{$i{id}}{region_id} = "N/A" ;
					# # $db{$table}{$i{id}}{country_id} = "N/A" ;
				# # }
			# # }
			
			# # if (!$readonly{region_id}) {
				
				# # my $country_id = 0 ; 
				# # foreach (keys %{$db{regions}}) {
					# # if ($_ eq $db{$table}{$i{id}}{matched_region_id}) {
						# # $country_id = $db{regions}{$_}{country_id} ; 
					# # }
				# # }
				# # $opts{region_id} = qq~<option value="$db{$table}{$i{id}}{matched_region_id}" SELECTED>$db{regions}{$db{$table}{$i{id}}{matched_region_id}}{name} [$db{regions}{$db{$table}{$i{id}}{matched_region_id}}{code}]</option>~ ; 
				# # $opts{country_id} = qq~<option value="$country_id" SELECTED>$db{countries}{$country_id}{name}</option>~ ;
				# # $select{region_id} = 1 ; $select{country_id} = 1 ;	$required{region_id} = 1 ;					
			# # }
			
		} else {
			$select{region_id} 		= '' ;	
			$select{country_id} 	= '' ;					
			$input{region_id} 		= 1 ;		
			$input{country_id} 		= 1 ;		
			$readonly{region_id} 	= "READONLY" ; 
			$readonly{country_id} 	= "READONLY" ; 
			$required{region_id} 	= "" ; 		
			$db{$table}{$i{id}}{region_id} = "N/A" ;
			$db{$table}{$i{id}}{country_id} = "N/A" ;
		}
		
		$required{name}         		= 0 ; 			
		$readonly{name} 				= "READONLY" ;	
		
		$select{aisa_client_id} = '' ;		
		$input{aisa_client_id} = 1 ;	
		$readonly{aisa_client_id} = "READONLY" ; 		
		
	} else {
		
		# # my $regions_limit = '' ;

		# # if ($is_schools_manager || $is_operator) {
			# # @report_sql_or = () ;
			# # foreach $_reg_id (keys %{$glob_regids{$userid}}) {
				# # push @report_sql_or, "id = '$_reg_id'"
			# # }
			# # $regions_limit = join(' OR ',@report_sql_or) ;
		# # }
		
		# # my $default_region_id = ($db{$table}{$i{id}}{region_id}) ? $db{$table}{$i{id}}{region_id} : $db{$table}{$i{id}}{matched_region_id} ;
		
		# # if ($default_region_id) {
			# # $opts{region_id} =~ s/value="$default_region_id"/value="$default_region_id" SELECTED/g  ;
			# # $opts{country_id} =~ s/value="$db{regions}{$default_region_id}{country_id}"/value="$db{regions}{$default_region_id}{country_id}" SELECTED/g  ;
		# # } 
		
		# # $select{region_id} = 1 ;		
		# # $select{country_id} = 1 ;		
		# # $required{region_id} = 1 ;
		# # $required{name} = 1 ;
		
		# # $trigger_jquery_raw .= qq~
			# # \$("#selectCountry_id").chosen({ allow_single_deselect:true });
		# # ~ ;
		
		$required{name}         		= 0 ; 			
		$readonly{name} 				= "READONLY" ;	
		
		my $default_region_id = ($db{$table}{$i{id}}{region_id}) ? $db{$table}{$i{id}}{region_id} : $db{$table}{$i{id}}{matched_region_id} ;
		
		$select{region_id} 		= '' ;	
		$select{country_id} 	= '' ;					
		$input{region_id} 		= 1 ;		
		$input{country_id} 		= 1 ;		
		$readonly{region_id} 	= "READONLY" ; 
		$readonly{country_id} 	= "READONLY" ; 
		$required{region_id} 	= "" ; 		
		$db{$table}{$i{id}}{region_id} = ($default_region_id) ? "$db{regions}{$default_region_id}{name}" : "N/A" ;		
		$db{$table}{$i{id}}{country_id} = ($default_region_id) ? "$db{countries}{$db{regions}{$default_region_id}{country_id}}{name}" : "N/A" ;		
		
		$select{aisa_client_id} = 1 ;
		
		&db_switch_conn('aisa');
		&common_min_select_opts('aisa_client_id','customers','name','','','','','','','','name') ;
		&db_switch_conn('sss');
		
		if ($db{$table}{$i{id}}{aisa_client_id}) {
			$trigger_jquery_raw .= qq~
				\$("#selectAisa_client_id").val($db{$table}{$i{id}}{aisa_client_id}).trigger("chosen:updated") ;
			~ ;
		}

		$trigger_jquery_raw .= qq~
			\$("#selectAisa_client_id").chosen({ allow_single_deselect:true });
		~ ;
		

	}
	
	# $trigger_jquery_raw .= qq~
		# \$("#selectCountry_id").val($db{$table}{$i{id}}{aisa_client_id}).trigger("chosen:updated") ;
	# ~ ;

	# $select{region_id} = '' ;			
	# $select{aisa_client_id} = 1 ;			
	# $input{region_id} = 1 ;		
	# $input{country_id} = 1 ;		
	# $select{country_id} = '' ;		
	
	# $readonly{region_id} 	= "READONLY" ; 
	# $readonly{country_id} 	= "READONLY" ; 
	# $readonly{name}			= "READONLY" ;
	# $required{region_id} 	= "" ; 
	# $required{name}			= "" ;
	
	# $i{region_id} = "123123123" ;
	
	# if ($db{$table}{$i{id}}{region_id}) {
		# $db{$table}{$i{id}}{region_id} = ($db{regions}{$db{$table}{$i{id}}{region_id}}{code}) ? "$db{regions}{$db{$table}{$i{id}}{region_id}}{name} [$db{regions}{$db{$table}{$i{id}}{region_id}}{code}]" : $db{regions}{$db{$table}{$i{id}}{region_id}}{name} ;
	# } elsif ($db{$table}{$i{id}}{matched_region_id}) {
		# $db{$table}{$i{id}}{region_id} = ($db{regions}{$db{$table}{$i{id}}{matched_region_id}}{code}) ? "$db{regions}{$db{$table}{$i{id}}{matched_region_id}}{name} [$db{regions}{$db{$table}{$i{id}}{matched_region_id}}{code}]" : $db{regions}{$db{$table}{$i{id}}{matched_region_id}}{name} ;
	# } else {
		# $db{$table}{$i{id}}{region_id} = "N/A" ;
	# }
	
	# if ($default_country) {
		# $db{$table}{$i{id}}{country_id} = $db{countries}{$default_country}{name} ;
	# } else {
		# $db{$table}{$i{id}}{country_id} = "N/A" ;
	# }
	
	if (!$readonly{region_id}) {
		
		$trigger_jquery_raw .= qq~
					
			let map_region_to_country = {} ; 
			
			const \$countrySelect 	= \$("#selectCountry_id") ;
			const \$regionSelect  	= \$("#selectRegion_id") ;

			const updateChosen = \$el => \$el.trigger("chosen:updated");
			
			const fetchJSON = async url => {
				try {
					const res = await fetch(url);
					if (!res.ok) throw new Error(`Request failed: \${res.status}`);
					return await res.json();
				} catch (err) {
					console.error("Fetch error:", err);
					return [];
				}
			};	
			
			const populateSelect = (\$field, json_list, getLabel, defaultSelected = () => "") => {
				
				let isSelected ;

				// Determine how to handle selected logic
				if (typeof defaultSelected === "function") {
					isSelected = defaultSelected ;
				} else if (Array.isArray(defaultSelected)) {
					isSelected = data => defaultSelected.includes(data.id) ? "selected" : "" ;
				} else {
					isSelected = data => data.id == defaultSelected ? "selected" : "" ;
				}

				// Auto-select the only item if none explicitly selected
				const autoSelectOnly = json_list.length === 1 && typeof defaultSelected === "string" && defaultSelected === "" ;

				json_list.forEach(data => {
					const selected = autoSelectOnly || isSelected(data) ? "selected" : "" ;
					\$field.append(`<option value="\${data.id}" \${selected}>\${getLabel(data)}</option>`) ;
				});

				updateChosen(\$field) ;
				return json_list.length ;
			};
			
			const getInputValue = name => \$(`input[name='\${name}']`).val();

			const clearSelect = (\$el) => {
				\$el.empty();
				\$el.append(`<option value=""></option>`).trigger("chosen:updated");
			};
			
			// _______________________________________________
			
			\$countrySelect.change( async function() { 
				\$("input[name='custom_selected_country']").val("1") ;
				await update_region_drop_down_from_country(\$countrySelect.val()) ; 
			}) ;
			 
			async function update_region_drop_down_from_country (countryId) {
			 
				clearSelect(\$regionSelect) ;
				
				const regionUrl = `$useropts{scripts}/get/get_regions_from_country.pl?country_id=\${countryId}` ;
				const regions = await fetchJSON(regionUrl);
			
				for (const data of regions) {
					map_region_to_country[data.id] = countryId ;
				}
				 
				const regionCount = populateSelect(\$regionSelect, regions, r => r.code ? `\${r.name} [\${r.code}]` : r.name , "") ;			

				if (regionCount === 0 && \$countrySelect.val()) {
					\$regionSelect.append(`<option value="13" selected>Other [OT]</option>`).trigger("chosen:updated") ;
				}  
				
			}	
			
			// _______________________________________________
			
			\$regionSelect.change( async function() {
				\$("input[name='custom_selected_region']").val("1") ;
				\$("input[name='custom_selected_country']").val("1") ; 
				const regionId = \$regionSelect.val() ;   
				await update_country_from_region(regionId,1) ; 
			});
			
			async function update_country_from_region (regionId,update_country=0) {
				if (map_region_to_country[regionId] || regionId == '13') {
					\$countrySelect.val(map_region_to_country[regionId]).trigger("chosen:updated") ; 
				} else if (!\$("input[name='custom_selected_country']").val() || update_country) { 
					const lookupUrl = `$useropts{scripts}/get/get_country_from_region.pl?country_id=&region_id=\${regionId}`;
					const regionMeta = await fetchJSON(lookupUrl);
					const { country_id } = regionMeta[0] ?? {} ; 
					if (country_id) {
						map_region_to_country[regionId] = country_id;
						\$countrySelect.val(country_id).trigger("chosen:updated");
					} 					
				}		
			}
			
			// _______________________________________________
			
		~ ;		
		
	}

	# $required{based_in} = 1 ;
	# $select{based_in} = 1 ;
	# $opts{based_in} = qq~<option value="JNB" $selected{based_in}{'JNB'}>Johannesburg</option><option value="CPT" $selected{based_in}{'CPT'}>Cape Town</option>~ ;
	
	# return if $venue_id_used{$i{id}} ;
	
	# my $regions_limit = '' ;
	
	# if ($is_schools_manager || $is_operator) {
		# @report_sql_or = () ;
		# foreach $_reg_id (keys %{$glob_regids{$userid}}) {
			# push @report_sql_or, "id = '$_reg_id'"
		# }
		# $regions_limit = join(' OR ',@report_sql_or) ;
	# }
	
	# &common_min_select_opts('region_code','regions','name',$db{$table}{$i{id}}{region_code},'','code','','code','','','name') ;
	
	# &db_switch_conn('aisa');
	
	# &common_min_select_opts('aisa_client_id','customers','name','','','','','','','','name') ;
	
	# &db_min_ro('regions','id,name,code','','','') ; 
	
	# if ($db{$table}{$i{id}}{aisa_client_id}) {
		# # $opts{aisa_client_id} =~ s/\"/\'/g ;
		# # $opts{aisa_client_id} =~ s/value='$db{$table}{$i{id}}{aisa_client_id}'/value='$db{$table}{$i{id}}{aisa_client_id}' SELECTED/g ;
		# $trigger_jquery_raw .= qq~
			# \$("#selectAisa_client_id").val($db{$table}{$i{id}}{aisa_client_id}).trigger("chosen:updated") ;			
		# ~ ;
	# }
	
	# $trigger_jquery_raw .= qq~
		# \$("#selectAisa_client_id").chosen({ allow_single_deselect:true });		
	# ~ ;
	
	&db_switch_conn('sss');
	
} #------------------------------------------------------------------------------------------

sub thead {
	
	if ($venue_id_used{$i{id}}) {
		# $ignore{region_code} = 1 ;
		$ignore{region_id} = 1 ;
		$ignore{name} = 1 ;
	}
	
	$print_thead = qq~<THEAD><TR><TH>ID</TH><TH>Name</TH><TH>Client Name</TH><TH>Region</TH><TH>Region (est.)</TH><TH>Country</TH><TH>&nbsp;</TH></TR></THEAD>~ ;
    
} #-------------------------------------------------------------------------------

sub page_opts {

	our $glyphicon = 'map-marker' ;
	our $lcpage    = 'teams' ; $ucpage = uc $lcpage ; $ucfirstpage = ucfirst $lcpage ;
	our $table     = 'teams' ;
	
	$s{no}         = 1 ;

	&common_min_add_extras ;
	
	$box_icon = '' ;
	
} #-------------------------------------------------------------------------------

use common ;
use today ;

1;