#!/usr/bin/perl

use strict;
use CGI;

use Opals::Context;
use Opals::Template qw(
    tmpl_read
    tmpl_write
    tmpl_preference
);
use Opals::Locale qw(
    loc_getMsgFile
    loc_write
);

use Opals::RecordInfoHTML qw(
    pf_getPfHTML
);
use Opals::Mail qw(
    mail_send
);
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 Time::localtime;

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

my $cgi = CGI->new;
my $input = $cgi->Vars();

my ($permission, $cookieList, $template) = tmpl_read(
    {
        dbh             => $dbh,
        cgi             => $cgi,
        tmplFile        => 'report/reserveNoticeEmail.tmpl',
         reqPermission   => 'rpt_notice'

    }
);

my $pref    = tmpl_preference($dbh);

my $rnCont      = "";
my $uid         = $input->{'uid'};
my $barcode     = $cgi->param('barcode'); 
my $email       = $cgi->param('email'); 
my $msg         = $input->{'msg'} || ""; 
     
my $libName          = $pref->{'libname'};
my $tm = localtime;
my $today = sprintf("%04d-%02d-%02d", $tm->year+1900, ($tm->mon)+1, $tm->mday);
 
    if($email ne '' && $barcode && $barcode ne "" && $uid){
        $rnCont=_getReserveNotice($dbh,$uid,$barcode,$msg);
    }

    if ($rnCont ne '' && mail_send($pref, $email, 'Reserve Notice', $rnCont)){
        $template->param(done=>1,email=>$email);
    }
    else{
        $template->param(error=>1);
    }
    $template->param(email=>$email );
   

#=======================================================
 my $msgValMap={
                 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 _getReserveNotice{
    my ($dbh,$uid,$barcode,$msg)=@_;
    my $template = _getTemplate("/htdocs/theme/opals/report/","reserveNoticeCont.inc");
    my $itemInfo;
    my $reserveList; 
    if($barcode && $barcode ne '' ){    
        $itemInfo  = circ_getItemInfo($dbh, $barcode);
        $reserveList =circ_getHoldListByRid($dbh,$itemInfo->{'rid'});
    }
    my $expiryDate;
    my @holdList=();
    my $email;
    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->{'email'}           = $userInfo->{'email'};
                $holdInfo->{'grade'}           = $userInfo->{'grade'};
                $holdInfo->{'notes'}           = $userInfo->{'notes'};
                $holdInfo->{'numOfHold'}       = @$reserveList[$i]->{'numOfHold'};
                if(defined $msg && $msg ne ""){
                    $holdInfo->{'msg'} =$msg;
                }
                push @holdList,$holdInfo;
                last;
            }
    }

  $template->param(
         holdList        =>\@holdList,
    );
  
  my $msgValMap={resNotice_msg01   => {expiryDate=> date_text($expiryDate,0)}};
  my $resNoticeMsgMap =loc_getMsgFile('circ/reserveNotice.msg',$msgValMap);
  loc_write($template,$resNoticeMsgMap);

return $template->output;

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

    return $userInfo;

}
#------------------------------------------------------------------------
sub _getTemplate{
    my($path,$fname)=@_;
    my $rootDir   = Opals::Context->config('rootDir');
    my $template = HTML::Template->new(    
                filename            => $fname ,
                path                => "$rootDir/$path",
                global_vars         => 1,
                die_on_bad_params   => 0,
                cache               => 1,
                shared_cache        => 0,
                loop_context_vars   => 1,);
    return $template;    
}

