#!/usr/bin/perl
# Thu, Apr 14, 2011 @ 10:59:51 EDT
# Add user sort field ( grad year, grade...) 
# Update Files: /htdocs/theme/opals/locale/en/circ/notice.msg /htdocs/theme/opals/locale/fr/circ/notice.msg  
#               /module/Opals/Report.pm  /htdocs/theme/opals/circ/odnotice_user.inc  
#               /htdocs/theme/opals/circ/odnotice.tmpl  /bin/circ/odnotice 
#               /htdocs/theme/opals/circ/odprint.tmpl  /bin/circ/odprint  /bin/circ/odemail
#    

#use utf8;
use strict;
use CGI;
use Encode;
use Opals::Context;

use Time::localtime;

use Opals::Date qw(
    date_parse
    date_today
    date_text
    date_f005
);

use Opals::Template qw(
    tmpl_read
    tmpl_write
    tmpl_rangedPageList
    tmpl_preference
);
use Opals::Locale qw(
    loc_getMsgFile
    loc_write
    
);
use Opals::Report qw(
    rpt_onloanList
    
);

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        => 'report/itemonloan.tmpl',
            reqPermission   => 'rpt_circ|rpt_notice',
        }
    );


my $pref = tmpl_preference($dbh);

my $libType = $pref->{'libraryType'} || 'k-12';
if($libType eq 'public'){
    $libType="public";
}
elsif($libType eq 'academy'){
    $libType="academy";
}

    my $itemOnLoanMsgMap =loc_getMsgFile('circ/itemOnLoan.msg');
    my $groupBy = $input->{'groupBy'};

    my $catList=getUserCatList($dbh);
 
    my $searchGroupFieldList ={"public" =>[{'val'=>'username'  ,'name'=>$itemOnLoanMsgMap->{'userTxt'}}],
                                "k-12"  =>[{'val'=>'username'  ,'name'=>$itemOnLoanMsgMap->{'userTxt'}},
                                          {'val'=>'homeroom'   ,'name'=>$itemOnLoanMsgMap->{'homeRoomTxt'}},
                                          {'val'=>'teacher'    ,'name'=>$itemOnLoanMsgMap->{'teacherTxt'}},
                                          {'val'=>'grade'      ,'name'=>$itemOnLoanMsgMap->{'gradeTxt'}},
                                          {'val'=>'yeargraduation','name'=>$itemOnLoanMsgMap->{'graduationYearTxt'}}
                                          ],
                                "academy"=>[
                                        {'val'=>'username'     ,'name'=>$itemOnLoanMsgMap->{'userTxt'}},
                                        {'val'=>'program'      ,'name'=>$itemOnLoanMsgMap->{'programTxt'}},
                                        {'val'=>'studies'      ,'name'=>$itemOnLoanMsgMap->{'studiesTxt'}}]
                                };
     
    my $dateSelOpt=[{'name'=>$itemOnLoanMsgMap->{'dueDateTxt'},'val'=>'dateDue'},
                    {'name'=>$itemOnLoanMsgMap->{'loanDateTxt'},'val'=>'dateLoan'}];
    my $formAuthList={
        categorycode        =>$catList,
        dateSelOpt          =>$dateSelOpt,
        searchGroupFieldList=>$searchGroupFieldList->{$libType},
    };
    my $formAuthListJson = to_json($formAuthList, {pretty => 1}) ;

 
   #$grpField ="username" if($grpField &&($grpField =~ /lastname|firstname/g));
    $template->param(libType=>$libType, "groupBy_$groupBy"=>1,groupBy=>$groupBy);        
    $template->param(hlpUrl     => Opals::Constant->getHlpUrl('odnotice') );

    $template->param(formAuthListJson=>$formAuthListJson);

    loc_write($template,$itemOnLoanMsgMap);
    tmpl_write($dbh, $cgi, $cookie, $template);
#-----------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------
sub getUserCatList{
    my($dbh)=@_;
    my $sth=$dbh->prepare("select catid val ,catname name,defaultPerm from opl_category order by catname");
    $sth->execute();
    my @catList=();
    while(my $rec=$sth->fetchrow_hashref){
        push @catList,$rec
     }
     return \@catList;

}



