#!/usr/bin/perl

#use utf8;
use strict;
use CGI;

use Opals::Context;
use Date::Calc::Object qw(
    :all
);
use Time::localtime;

use Opals::Template qw(
    tmpl_read
    tmpl_write
    tmpl_preference
    tmpl_redirect
);

use Opals::Locale qw(
    loc_getMsgFile
    loc_write
);
use Opals::User qw(
    user_getHR_Grade_TeacherList
);
use JSON;
################################################################################
my $dbh = Opals::Context->dbh();
END { $dbh->disconnect(); }

my $cgi = CGI->new;
my $input = $cgi->Vars();
my ($permission, $cookie, $template) = tmpl_read(
        {
            dbh             => $dbh,
            cgi             => $cgi,
            tmplFile        => 'circ/loan.tmpl',
            reqPermission   => 'circ_loan',
        }
);
my $syspref          = tmpl_preference($dbh);
my $chargeFine       = $syspref->{'charge_overdue'};
my $chargeDamage     = $syspref->{'charge_damage'};
my $chargeLost       = $syspref->{'charge_lost'};
my $validateBc       = $syspref->{'validateBarcode'}; 
my $barcodeType      = $syspref->{'barcodeType'}; 
my $slipPrinter        = (defined $syspref->{'slipPrinter'} && $syspref->{'slipPrinter'} >0) ?1: 0; 
my $circulationSound = $syspref->{'circulationSound'};
my $loginuid         = $template->param('curUserId');

my @cookieList = (@$cookie);
my $tm = localtime;
my $todayStr = sprintf("%04d-%02d-%02d", $tm->year+1900, ($tm->mon)+1, $tm->mday);
my $dFirst = Opals::Context->preference('dateFirst');
my $dLast  = Opals::Context->preference('dateLast');
my $libType = Opals::Context->preference('libraryType')|| 'k-12';

if ($permission && $permission->{'circ_loan'}) 
{   
    $template->param(
                 circulation => 1,
                 circulationSound => $circulationSound,
                 loan        => 1,
                 hlpUrl     => Opals::Constant->getHlpUrl('loan')
                 );

    if($slipPrinter){
        $template->param(slipPrinter   => $slipPrinter);
    }
    my $tmpList=user_getHR_Grade_TeacherList($dbh,'teacher');
    my $teacherList=[];
    my $homeroomList=[];
    foreach my $e(@$tmpList){
        push @$teacherList,$e if($e->{'val'} && $e->{'val'} ne '');
    }
    $tmpList=user_getHR_Grade_TeacherList($dbh,'homeroom');
    foreach my $e(@$tmpList){
        push @$homeroomList,$e if($e->{'val'} && $e->{'val'} ne '');
    }

    my $userListOpt=[{msgKey=>"userNameBcTxt",name=>"User Name/Barcode",type=>"nameBc"},
                    {msgKey=> "customGroupListTxt", name=>"Custom group list",type=>"preSelListId", list=> getpeSelListList($dbh)}
                    ];
    if($libType eq 'k-12'){
        push @$userListOpt,{msgKey=> "teacherTxt",name=>"teacher",   type=>"teacher",    list=> $teacherList};
        push @$userListOpt,{msgKey=> "homeRoomTxt",name=>"homeroom",  type=>"homeroom",   list=> $homeroomList};
    }
    $template->param(userListOpt   =>to_json($userListOpt),libType=>$libType);
    $template->param(userTypeList   =>to_json(getUserTypeList($dbh)));

}
my $qeMsgMap            =loc_getMsgFile('circ/quickItemAjaxFrm.msg');
my $userqeMsgMap        =loc_getMsgFile('circ/quickUserAjaxFrm.msg');
my $userSelPanMsgMap    =loc_getMsgFile('circ/userSelPan.msg');
my $finePanMsgMap       =loc_getMsgFile('circ/fine.msg');
my $confirmMsgMap       =loc_getMsgFile('circ/confirmDlg.msg');

loc_write($template,$qeMsgMap);
loc_write($template,$userqeMsgMap);
loc_write($template,$userSelPanMsgMap);
loc_write($template,$finePanMsgMap);
loc_write($template,$confirmMsgMap);

tmpl_write($dbh, $cgi, \@cookieList, $template);


#---------------------------------------------------------
sub getpeSelListList{
    my($dbh)=@_;
    my @rsList=();my $uidList=[];
    my $sth =$dbh->prepare("select id as val ,name from opl_preSelUserList order by dateCreated");
    $sth->execute();
    while(my $r =$sth->fetchrow_hashref){
        push @rsList,$r;
    }
    return \@rsList;

}
#---------------------------------------------------------
sub getUserTypeList{
    my($dbh)=@_;
    my @rsList=();
    my $sth =$dbh->prepare("select catid ,catname,if(p.val is null,0,1) isDefault from opl_category c left outer join opl_preference p on p.var='defCategory' && p.val=c.catid;");
    $sth->execute();
    while(my $r =$sth->fetchrow_hashref){
        push @rsList,$r;
    }
    return \@rsList;

}

################################################################################

