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

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

&page_opts ;

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

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

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

&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}) ;

	&db_min_ro($table,'*',"name='$i{name}' AND `country_id` = '$i{country_id}'",'name','') ;

	foreach my $id (keys %{$db{$table}}) { 
		if (lc $db{$table}{$id}{name} eq lc $i{name}) { 
			$exists = 1 ;
		}
	}
	
	if ($exists) {
		&db_min_ro('countries','1,name',"id='$i{country_id}'") ; 
		$alert = &common_min_alert('warning',"`$i{name}` AS A REGION NAME IN THE COUNTRY `$db{countries}{1}{name}` ALREADY EXISTS!",'ok') ;
		%col_name = ();
		&common_min_add_screen;
		&common_min_screen1;
    }

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

sub insert {

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

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

sub update {

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

	&edit_db_fields ;

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

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

sub list_screen {
	
	&db_switch_conn('sss') ;
	
	&db_min_ro('event_quotes',"DISTINCT region_id AS 'region_id'","region_id <> '0'",'','') ;

	foreach my $_id (keys %{$db{event_quotes}}) {
		$region_id_used{$_id} = 1 ;
	}
	
	&db_min_ro('users','id,region_ids',"region_ids <> '0'",'','') ;
	
	foreach my $user_id (keys %{$db{users}}) {
		next if $region_id_used{$db{users}{$user_id}{region_ids}} ;
		my @region_ids = split(/\,/,$db{users}{$user_id}{region_ids}) ;
		foreach (@region_ids) {
			$region_id_used{$_} = 1 ;
		}
	}	
	
	&db_switch_conn('aisa') ;
	
	&db_min_ro('event_quotes',"DISTINCT region_id AS 'region_id'","region_id <> '0'",'','') ;

	foreach my $_id (keys %{$db{event_quotes}}) {
		$region_id_used{$_id} = 1 ;
	}
	
	&db_min_ro('countries','id,name',"",'','') ; 
	
	&db_min_ro('users','id,region_ids',"region_ids <> '0'",'','') ; 
	
	foreach my $user_id (keys %{$db{users}}) {
		next if $region_id_used{$db{users}{$user_id}{region_ids}} ;
		my @region_ids = split(/\,/,$db{users}{$user_id}{region_ids}) ;
		foreach (@region_ids) {
			$region_id_used{$_} = 1 ;
		}
	}
	
	$region_id_used{13} = 1 ; ## OTHER
	
	&db_min_ro($table,'*','','','') ;
# extra_edit_hidden_inputs
	foreach my $id (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 ($glod_user_level >= 3) {
			$del_butt  = qq~&nbsp;<a class="btn btn-danger btn-xs" href="javascript:deleteMinItem('$db{$table}{$id}{regions}','$db{$table}{$id}{id}')"><i class="glyphicon glyphicon-trash icon-white"></i></a>~ ; 
		}
			
		if ($region_id_used{$id}) {
			if ($db{$table}{$id}{code} && $db{$table}{$id}{name}) {
				$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>~ ;   
			} elsif ($glod_user_level >= 3) {
				$edit_butt = qq~&nbsp;<a class="btn btn-info btn-xs" href="javascript:editMinItem('$db{$table}{$id}{id}');" title="In system use!<br>Fields Incomplete!" data-toggle="tooltip"><i class="glyphicon glyphicon-edit icon-white"></i></a>~ ; 
				$extra_edit_hidden_inputs .= qq~<input type="hidden" name="incomplete_but_in_system_use_$id" value="1">~ ;
			}
			
			$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>~ ;  
		}
		
		$print_tbody .= qq~<tr id="$id">
			<td>$id</td>
			<td>$db{$table}{$id}{code}</td>
			<td>$db{$table}{$id}{name}</td>
			<td>$db{$table}{$id}{latitude}</td>
			<td>$db{$table}{$id}{longitude}</td>
			<td>$db{countries}{$db{$table}{$id}{country_id}}{name}</td>
			<td nowrap>
				$edit_butt
				$del_butt
			</td>
		</tr>~ ;
	}

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

sub add_db_fields {
	
	&sort_fields;
		
	&hidden_db_fields;

	# $ignore{id} = 1 ;

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

sub edit_db_fields {
	
	&sort_fields;
		
	&hidden_db_fields;

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

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

sub hidden_db_fields {
	
	$ignore{iaction}        = 1 ;
	$required{code}         = 1 ; 
	$required{name}      = 1 ;

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

sub sort_fields {
	
	%sort_field = () ;
	
	$sort_field{1}  = 'name' ; $preferred_title{name} = 'Name' ;
	$sort_field{2}  = 'code' ; $preferred_title{code} = 'Code' ;
	$sort_field{3}  = 'latitude' ; $preferred_title{latitude} = 'Latitude' ;
	$sort_field{4}  = 'longitude' ; $preferred_title{longitude} = 'Longitude' ;
	$sort_field{5}  = 'country_id' ;  
	
	$select{country_id} = 1 ; 
	$required{country_id} = 1 ; 
	
	if ($i{id}) {
		
		my $incomplete_field = "incomplete_but_in_system_use_$i{id}" ; 
		
		if ($i{$incomplete_field}) {
			
			&db_min_ro($table,'*',"`id`='$i{id}'",'','') ;  
			$readonly{code} = "READONLY" if $db{$table}{$i{id}}{code} ; 
			$readonly{name} = "READONLY" if $db{$table}{$i{id}}{name} ; 
			$readonly{latitude} = "READONLY" if $db{$table}{$i{id}}{latitude} ; 
			$readonly{longitude} = "READONLY" if $db{$table}{$i{id}}{longitude} ; 
			
		}
	}
	
	$db{$table}{$i{id}}{name} = $i{name} ;
	$db{$table}{$i{id}}{code} = $i{code} ;
	$db{$table}{$i{id}}{latitude} = $i{latitude} ;
	$db{$table}{$i{id}}{longitude} = $i{longitude} ;
	$db{$table}{$i{id}}{country_id} = $i{country_id} ;
	
	if ($i{country_id}) {
		$trigger_jquery_raw .= qq~
			\$("#selectCountry_id").val("$i{country_id}").trigger("chosen:updated") ; 
		~ ; 
	}
	
} #-------------------------------------------------------------------------------

sub add_screen {

	# called from common_add_screen

	&select_opts ;

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

sub edit_screen {

	&select_opts ;

    # &common_min_select_opts('code','regions','name',$db{$table}{$i{id}}{name},'','',$sql_or,'name');
} #------------------------------------------------------------------------------------------

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>~ ;
	
	# 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('country_id','countries','name',$db{$table}{$i{id}}{country_id},'','','','','','','name') ;
	
} #------------------------------------------------------------------------------------------

sub thead {

	$print_thead = qq~<THEAD><TR><TH>ID</TH><TH>NAME</TH><TH>CODE</TH><TH>LONGITUDE</TH><TH>LATITUDE</TH><TH>COUNTRY</TH><TH>&nbsp;</TH></TR></THEAD>~ ;
    
} #-------------------------------------------------------------------------------

sub page_opts {

	our $glyphicon = 'globe' ;
	our $lcpage    = 'regions' ; $ucpage = uc $lcpage ; $ucfirstpage = ucfirst $lcpage ;
	our $table     = 'regions' ;
	
	$s{no}         = 1 ;

	&common_min_add_extras ;

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

use common ;
use today ;

1;