#!/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 $only_update_number = ($action eq 'add_number_only') ? 1 : 0 ;

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

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

$iaction = "add" if $iaction eq 'add_number_only' ;

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

# &db_switch_conn('aisa') ;

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

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

sub insert {

	&add_db_fields ; # $i{last_edited_by} = $userid ; $i{last_updated} = "$now_ccyy_mm_dd $now_hour:$now_min:$now_sec" ;
		
	our $input_name_cnt_in_table = 0 ; 
	our $input_name_without_number_in_table = 0 ; ;	
		
	&validate ;
	
	$ignore{sel_field} = 1 ;
	
	$i{id} = &db_min_get_max($table,'id') ;
	
	$ignore{id}         = 0 ; 
	
	if ($input_name_cnt_in_table == 1 && $input_name_without_number_in_table) {
		my %ii = %i ; 
		%i = () ; 
		$i{contact_nr} = $ii{contact_nr} ;
		&db_min_upd($table,"`name`='$ii{name}'") ; 
		%i = %ii ; 
	} else {
		&db_min_insert($table) ;
	}
	
} #------------------------------------------------------------------------------------------

sub validate {

	# called from common_min_action

	my $exists   = '' ;
	
	use utf8;
	
	my $orig_number = $i{contact_nr} ;
	
	$i{name} 		= &common_fix_str($i{name}) ;
	$i{name} 		= &common_fix_poc_name($i{name}) ;
	$i{contact_nr} 	= &common_fix_poc_numbers($i{contact_nr}) ;
	
	if (length($i{name}) > 50) { $alert = &common_min_alert('danger',"YOUR POC NAME CONTAINS TOO MANY CHARACTERS!",'warning-sign') ; %col_name = () ; &common_min_add_screen ; &screen1 ; exit ; }
	if (length($i{contact_nr}) > 50) { $alert = &common_min_alert('danger',"YOUR POC CONTACT NUMBER CONTAINS TOO MANY CHARACTERS!",'warning-sign') ; %col_name = () ; &common_min_add_screen ; &screen1 ; exit ; }
	if ($orig_number && $i{contact_nr} == "") { $alert = &common_min_alert('danger',"YOUR POC CONTACT NUMBER IS TOO SHORT/INVALID NUMBER!",'warning-sign') ; %col_name = () ; &common_min_add_screen ; &screen1 ; exit ; }
	
	&db_min_ro($table,'*',"name='$i{name}'",'','') ;

	foreach my $id (keys %{$db{$table}}) {
		if (lc $db{$table}{$id}{name} eq lc $i{name} && $db{$table}{$id}{contact_nr} eq $i{contact_nr}) {
			$exists = 1 ;
			last ;
		}
		if (lc $db{$table}{$id}{name} eq lc $i{name}) {
			$input_name_cnt_in_table++ ;
			$input_name_without_number_in_table = 1 if !$db{$table}{$id}{contact_nr} ;
		}
	}

	if ($exists) {
		$alert = &common_min_alert('danger',"'$i{name}' AS A POC NAME WITH '$i{contact_nr}' AS POC NUMBER ALREADY EXISTS!",'warning-sign') ;
		%col_name = () ;
		&common_min_add_screen;
		&screen1;
    }

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

sub add_screen {

	$box_header_title = 'Add POC' ;
	$hidden_action    = 'save' ;
	$head_col_width   = 2 ;
	
	$trigger_jquery_raw .= qq~
		if (parent.\$("#selectPoc_name").val() && !parent.\$("#selectPoc_contact_nr").val()) {
			\$("input[name='name']").val(parent.\$("#selectPoc_name").val()) ;
		}
	~;	
	
} #------------------------------------------------------------------------------------------

sub add_db_fields {

	$ignore{iaction}   		= 1 ;
	$ignore{id}        		= 1 ; 
	$hidden{sel_field} 		= 2 ;
	$required{name}    		= 1 ;
	# $required{contact_nr}	= 4 ;
	$required{contact_nr}	= 10 ;
	$input{name}    		= 1 ;
	$input{contact_nr}		= 1 ;
	$sort_field{1} = 'name' ; 
	$sort_field{2} = 'contact_nr' ; 
	$db{$table}{''}{sel_field} = $selectfield if $selectfield ;
	
	if ($only_update_number) {
		$hidden{name} = 1 ;
	}
	
} #------------------------------------------------------------------------------------------

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 {
	
	$trigger_jquery_raw = qq~
		parent.\$("#selectPoc_name").append("<option value='$i{name}'>$i{name}</option>").trigger("chosen:updated") ;
		parent.\$("#selectPoc_name").val("$i{name}").trigger("chosen:updated") ;
		parent.\$("#selectPoc_contact_nr").empty() ;
		parent.\$("#selectPoc_contact_nr").append("<option value=''></option>").trigger("chosen:updated") ;
		parent.\$("#selectPoc_contact_nr").append("<option value='$i{contact_nr}'>$i{contact_nr}</option>") ;
		parent.\$("#selectPoc_contact_nr").val("$i{contact_nr}").trigger("chosen:updated");
		if (!parent.\$("#add_poc_contact_nr").is(":hidden")) {
			parent.\$("#add_poc_contact_nr").hide() ;
		}
		parent.BootstrapDialog.closeAll() ;
	~;
	
	&common_min_dialog_save_screen ;

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

use today ;
use dialog ;
use common ;

1;