#!/usr/bin/perl

#use utf8;
use strict;
use CGI;

use Opals::Context;use POSIX qw(
    floor
);

use Time::localtime;
use Opals::Constant;

use Opals::Template qw(
    tmpl_read
    tmpl_write
    tmpl_preference
);
use Opals::User qw(
    user_list
    user_getInformationById
);
use Opals::Circulation qw(
    circ_getItemInfo
    circ_getRecInfo
    circ_getHoldListByRid
);
use Opals::Date qw(
    date_text

);
use Opals::Locale qw(
    loc_getMsgFile
    loc_write
);



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/reserveNotice.tmpl',
            reqPermission   => 'circ_rsrv_self|circ_return',
        }
);
my $syspref          = tmpl_preference($dbh);
my $validateBc       = $syspref->{'validateBarcode'};
my $barcodeType      = $syspref->{'barcodeType'}; 
my @cookieList       = ($cookie);
my $libName          = $template->param('libname');
my $today            = $template->param('today');
     
if ($permission && ($permission->{'circ_rsrv_self'} || $permission->{'circ_rsrv'}|| $permission->{'circ_return'})) {
    my ($op,$uidList, $barcode,$rid );
        $op      = $input->{'op'};
        $uidList = $input->{'uid'};
        $barcode = $input->{'barcode'};
        $rid     = $input->{'rid'};
    my $itemInfo;
    my $reserveList; 
    if($barcode && $barcode ne '' ){    
        $itemInfo  = circ_getItemInfo($dbh, $barcode);
        $reserveList =circ_getHoldListByRid($dbh,$itemInfo->{'rid'});
    }
    elsif($rid && $rid != '' ){    
        $itemInfo  = circ_getRecInfo($dbh, $rid);
        $reserveList =circ_getHoldListByRid($dbh,$rid);
    }
    
    my $expiryDate;
    my @uidArr = split /,/, $uidList;
    my @holdList=();
    my $email;
    foreach my $uid(@uidArr){
        for(my $i=0; $i < scalar(@$reserveList); $i++){
            if(@$reserveList[$i]->{'uid'} == $uid){
                my $holdInfo;
                my $userInfo=getUserInfo($dbh,$uid);
                $expiryDate = @$reserveList[$i]->{'dateExpiry'};
                $email       = $userInfo->{'email'};
                $holdInfo->{'expiryDate'}=date_text(@$reserveList[$i]->{'dateExpiry'},0);
                $holdInfo->{'libName'}         = $libName;
                $holdInfo->{'today'}           = $today;
                $holdInfo->{'title'}           = $itemInfo->{'title'};
                $holdInfo->{'author'}          = $itemInfo->{'author'};
                $holdInfo->{'pubName'}         = $itemInfo->{'pubName'};
                $holdInfo->{'pubDate'}         = $itemInfo->{'pubDate'};
                $holdInfo->{'callnumber'}      = $itemInfo->{'callNumber'};
                $holdInfo->{'user_uid'}        = $userInfo->{'uid'};
                $holdInfo->{'user_barcode'}    = $userInfo->{'userbarcode'};
                $holdInfo->{'user_username'}   = $userInfo->{'username'};
                $holdInfo->{'user_firstname'}  = $userInfo->{'firstname'};
                $holdInfo->{'user_lastname'}   = $userInfo->{'lastname'};
                $holdInfo->{'homeroom'}        = $userInfo->{'homeroom'};
                $holdInfo->{'teacher'}         = $userInfo->{'teacher'};
                $holdInfo->{'grade'}           = $userInfo->{'grade'};
                $holdInfo->{'notes'}           = $userInfo->{'notes'};
                $holdInfo->{'numOfHold'}       = @$reserveList[$i]->{'numOfHold'};
                push @holdList,$holdInfo;
                last;
            }
        }
        
    }
     
    $template->param(
         holdList        =>\@holdList,
        #expiryDate      => date_text($expiryDate,0)
    );
    
   #------------- Get User Info ------------ 
    #getUserInfo($dbh,$uid);
   
   my $msgValMap={resNotice_msg01        => {expiryDate      => date_text($expiryDate,0)},
                 sendReserveNotice_done => {borrower_email  => $email},
                 sendReserveNotice_error=> {borrower_email  => $email}
                };
    my $resNoticeMsgMap =loc_getMsgFile('circ/reserveNotice.msg',$msgValMap);
    loc_write($template,$resNoticeMsgMap);


}
tmpl_write($dbh, $cgi, \@cookieList, $template);
################################################################################
sub getUserInfo_bk{
    my ($dbh,$uid)=@_;
    my ($userInfo, $guardian) = user_getInformationById($dbh, $uid);

    $template->param(
        user_uid            => $userInfo->{'uid'},
        user_barcode        => $userInfo->{'userbarcode'},
        user_username       => $userInfo->{'username'},
        user_firstname      => $userInfo->{'firstname'},
        user_lastname       => $userInfo->{'lastname'},
        homeroom            => $userInfo->{'homeroom'},
        teacher             => $userInfo->{'teacher'},
        grade               => $userInfo->{'grade'},
        notes               => $userInfo->{'notes'},

    );
    $template->param(hiddenid => $userInfo->{'uid'});

}
sub getUserInfo{
    my ($dbh,$uid)=@_;
    my ($userInfo, $guardian) = user_getInformationById($dbh, $uid);

    return $userInfo;

}


