#!/usr/bin/perl

#use utf8;
use strict;
use CGI;

use Opals::Context;
use Opals::Template qw(
    tmpl_read
    tmpl_write
    tmpl_redirect
);
use Opals::User qw(
    user_update
    user_update_1
    user_balance
    user_getInformationById
    user_LoadCategory
    user_LoadCategory_1
    user_list
    user_list_ext
    user_browse_ext

    user_permission
    user_permission_1
    user_listPermission
    user_listPermission_1
    user_StrPermission
    user_StrPermission_1

    user_currentUser
);

use Opals::Permission qw(

    permission_getList
    permission_getDefaultPermission

);

use Opals::Circulation qw(
    circ_userListLoan
);
use Opals::Search qw(
    srch_searchRecord
);
use Opals::Date qw(
    date_parse
    date_today
    date_text
);
use Opals::Locale qw(
    loc_getMsgFile
    loc_write
);
use Opals::UrlRegistry qw(
    ureg_getUrlRegistry
);

use Opals::Constant;

my $dbh   = Opals::Context->dbh();
END { $dbh->disconnect(); }

my $cgi = CGI->new;
my ($permission, $cookie, $template) = tmpl_read(
        {
            dbh             => $dbh,
            cgi             => $cgi,
            tmplFile        => 'user/quickUserList.tmpl',
            reqPermission   => 'user_edit',
        }
);

my $input = $cgi->Vars();
my $quickEntry = $input->{'qk'} ;

my $errUser = 0;
my ($userInfo, $guardian);

#----- Get uid
my @cookieList = (@$cookie);
my $FromCookie = 0;

my $uInput = $cgi->param('uInput');
my $uid = -1;
    $template->param(quickBorrowerEntry => 1);
my $url_edit=undef;
my @uList;
    # Tue, Nov 11, 2008 @ 14:31:04 EST
    $errUser = 1;
    if ($uInput eq '*' || $uInput eq '' ) {
        @uList = user_browse_ext($dbh, '',1); # Tue, Nov 18, 2008 @ 09:33:48 EST
    }
    else{
        @uList = user_list_ext($dbh, $uInput,'',1);
        $template->param(quickBorrowerEntry => 0); # Force go to edit user page instead of quick entry page
    }    
    if (scalar(@uList) == 0) { # no user has this name/barcode
        $template->param(userNotMatch  => 1, uInput => $uInput );
    }
    elsif(scalar(@uList) == 1) { # list of users 
        my $scriptRegistry=ureg_getUrlRegistry($permission);
        my $scriptName="";
        foreach my $s(keys %$scriptRegistry){
            if($s eq "url_updateuser"){
                $scriptName=$scriptRegistry->{$s};
                last ;
            }
        }

        $url_edit=$scriptName."?uid=" . @uList[0]->{'uid'};
    }
    else { # list of users 
        $template->param(uList => \@uList);
    }

$template->param(noQkUserList=>1) if(scalar(@uList)==0 && $quickEntry);

 
#Thu, Jan 07, 2010 @ 13:50:35 EST
my $msgValMap ={};
my $msgMap            =loc_getMsgFile('user/userInfo.msg',$msgValMap);
loc_write($template,$msgMap);
if(defined $url_edit){
    tmpl_redirect($cgi, $url_edit);
}
else{    
    tmpl_write($dbh, $cgi, \@cookieList, $template);
}



