#!/usr/bin/perl

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

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

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

&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";

&common_min_action;

exit;

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

sub validate {

my $exists   = '' ;

$i{username} = lc $i{username} ;
$i{username} =~ s/ //gi ;

&db_min_ro($table,'*',"username = '$i{username}'",'username','') ;

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

if ($exists) {
    $alert = &common_min_alert('warning',"USERNAME ALREADY EXISTS!",'ok') ;
	%col_name = ();
    &common_min_add_screen;
    &common_min_screen1;
    }

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

sub insert {

&add_db_fields ;

my $crypted_password = apache_md5_crypt($i{password},$useropts{salt}) ;
$i{password} = $crypted_password ;
$i{lastupdate} = "$now_ccyy_mm_dd $now_hour:$now_min:$now_sec" ;
$i{email}    = lc $i{email} ;

&db_min_insert($table) ;

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

sub update {

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

&edit_db_fields ;

unless ($i{block}) { $i{block} = '0' ; }

if ($i{new_password}) { 
    my $crypted_password = apache_md5_crypt($i{new_password},$useropts{salt}) ;
    $i{password} = $crypted_password ;
    $ignore{password} = 0 ;
    $hidden{password} = 2 ;    
    }
	
$i{lastupdate} = "$now_ccyy_mm_dd $now_hour:$now_min:$now_sec" ;

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

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

sub list_screen {

&db_min_ro($table,'*',"user_type like 'casual%'",'username','') ;

foreach my $id (keys %{$db{$table}}) {
	my $disp_blocked = qq(<span class="label label-success" title="Active!" data-toggle="tooltip"><i class="glyphicon glyphicon-ok"></i></span>); 
	if ($db{$table}{$id}{block}) { $disp_blocked = qq(<span class="label label-danger" title="Blocked!" data-toggle="tooltip"><i class="glyphicon glyphicon-remove"></i></span>); }
	
	if ($useropts{it}{$db{$table}{$id}{username}} and $username ne 'rory') { next ; }
	
    $opts{$lcpage} .= qq(<option value="$db{$table}{$id}{username}">$db{$table}{$id}{name}</option>) ;
    $print_tbody .= qq(<tr id="$id">
		<td>$id</td>
		<td>$db{$table}{$id}{username}</td>
		<td>$db{$table}{$id}{name}</td>
		<td>$db{$table}{$id}{email}</td>
		<td>$disp_blocked</td>
		<td>$db{$table}{$id}{based_in}</td>
		<td>$db{$table}{$id}{lastupdate}</td>
		<td>$db{$table}{$id}{lastvisitdate}</td>
		<td nowrap>
			&nbsp;<a class="btn btn-info btn-xs" href="javascript:editMinItem('$db{$table}{$id}{id}');"><i class="glyphicon glyphicon-edit icon-white"></i></a>
			&nbsp;<a class="btn btn-danger btn-xs" href="javascript:deleteMinItem('$db{$table}{$id}{username}','$db{$table}{$id}{id}')"><i class="glyphicon glyphicon-trash icon-white"></i></a>
		</td>
	</tr>) ;
    }

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

sub add_db_fields {

$ignore{lastvisitdate} = 1 ;
$ignore{iaction} = 1 ;
$ignore{block}   = 1 ;
$ignore{id} = 1 ; 
$hidden{lastupdate} = 2 ;
$required{username} = 1 ;
$required{name} = 1 ;
$required{password} = 1 ;

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

sub edit_db_fields {

$ignore{lastvisitdate} = 1 ;
$ignore{password} = 1 ;
$ignore{iaction} = 1 ;
$hidden{id} = 1 ; # 1 = bypass for db update
$hidden{lastupdate} = 2 ; # 2 = update db
$readonly{username} = 'READONLY' ;
$checkbox{block}    = 1 ;
$required{username} = 1 ;

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

sub add_screen {

# called from common_add_screen

&select_opts ;

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

sub edit_screen {

our ($lcol,$fcol) = &common_min_columns($label_col,$field_col) ;

$selected{based_in}{$db{$table}{$i{id}}{based_in}}   = "SELECTED" ;
$selected{user_type}{$db{$table}{$i{id}}{user_type}} = "SELECTED" ;

$extra_form_fields .= &common_min_form_input('new_password','','password') ; 

&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>~ ;
					 
$required{user_type} = 1 ;
$select{user_type} = 1 ;
$opts{user_type} = qq~
					  <!-- <option value="permanent" $selected{user_type}{'permanent'}>Permanent</option> -->
					  <!-- <option value="parttime" $selected{user_type}{'parttime'}>Part-time</option> -->
					  <!-- <option value="manager" $selected{user_type}{'manager'}>Manager</option> -->
					  <!-- <option value="it" $selected{user_type}{'it'}>IT</option> -->
					  <!-- <option value="external" $selected{user_type}{'external'}>External</option> -->
					  <option value="casual_a" $selected{user_type}{'casual_a'}>Operator A</option>
					  <option value="casual_b" $selected{user_type}{'casual_b'}>Operator B</option>
					  ~ ;

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

sub thead {

$print_thead = qq(<THEAD><TR><TH>ID</TH><TH>USERNAME</TH><TH>NAME</TH><TH>EMAIL</TH><TH>ACTIVE</TH><TH>BASE</TH><TH>LASTUPDATE</TH><TH>LASTVISITDATE</TH><TH>&nbsp;</TH></TR></THEAD>) ;
    
} #-------------------------------------------------------------------------------

sub page_opts {

our $glyphicon = 'user' ;
our $lcpage    = 'casual' ; $ucpage = uc $lcpage ; $ucfirstpage = ucfirst $lcpage ;
our $table     = 'users' ;

&common_min_add_extras ;

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

use common ;
use today ;

1;