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

&common_min_action;

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 ;
	
	&db_min_insert($table) ;

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

sub update {

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

	&edit_db_fields ;

	$ignore{name}		= 1 ; 
	$ignore{region_id}	= 1 ;

	&db_min_upd($table,"id='$i{id}'") ;

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

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

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

sub list_screen {
	
	&db_switch_conn('aisa');
	
	&db_min_ro('customers','id,name','','','') ;
	
	&db_min_ro('regions','*','','','') ;
	
	&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} ;
		
		$print_tbody .= qq~
			<tr id="$id">
				<td>$id</td>
				<td>$db{$table}{$id}{name}</td>
				<td>$region_name</td>
				<td>$matched_region_name</td>
				<td>$customer_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 {
	
	if ($i{id}) {
		
		&db_min_ro('event_quotes',"DISTINCT team_ids AS 'team_ids'","`team_ids` <> 0 AND `team_ids` LIKE '%$i{id}%'",'','') ;

		our %venue_id_used = () ;
		
		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') ;
		
		&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 ; 
			}
		}
	}
	
	%sort_field = () ;

	# $sort_field{1}  = 'region_code' ; $preferred_title{region_code} = 'Region Code' ;
	$sort_field{1}  = 'region_id' ; # $preferred_title{region_id} = 'Region' ;
	$sort_field{2}  = 'name' ; $preferred_title{name} = 'Name' ;
	$sort_field{3}  = 'aisa_client_id' ; 
	# $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' ; 
 
	if ($venue_id_used{$i{id}}) {	 
		# &db_min_ro('regions',"code,name","",'','') ;
		
		if ($db{$table}{$i{id}}{region_id}) {
			$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 ;
			$input{region_id} 				= 1 ;			
			$readonly{region_id} 			= "READONLY" ;			
		} elsif ($db{$table}{$i{id}}{matched_region_id}) {
			$opts{region_id} =~ s/value="$db{$table}{$i{id}}{matched_region_id}"/value="$db{$table}{$i{id}}{matched_region_id}" SELECTED/g  ;
			$select{region_id} = 1 ;		
		}
		
		$required{name}         		= 0 ; 			
		$readonly{name} 				= "READONLY" ;	
	} else { 
		if ($db{$table}{$i{id}}{matched_region_id}) {
			$opts{region_id} =~ s/value="$db{$table}{$i{id}}{matched_region_id}"/value="$db{$table}{$i{id}}{matched_region_id}" SELECTED/g  ;
		} 
		$select{region_id} = 1 ;		
	}
	
	$select{region_id} = '' ;			
	$select{aisa_client_id} = 1 ;			
	$input{region_id} = 1 ;		
	
	$readonly{region_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" ;
	}
	
} #-------------------------------------------------------------------------------

sub add_screen {

	# called from common_add_screen

	&select_opts ;

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

sub edit_screen {

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

sub select_opts {

	# $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 $sql_or = '' ;
	
	if ($is_schools_manager || $is_operator) {
		@report_sql_or = () ;
		foreach $_reg_id (keys %{$glob_regids{$userid}}) {
			push @report_sql_or, "id = '$_reg_id'"
		}
		$sql_or = 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>Region</TH><TH>Region (est.)</TH><TH>Client Name</TH></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;