package Opals::NoticeEmailSvc;

require Exporter;
@ISA       = qw(Exporter);

@EXPORT_OK = qw(
    emailSvc_holdReady
    emailSvc_cancelReserve
    emailSvc_reject

   );
use Opals::Mail qw(
    mail_send
);

use Opals::Template qw(
    tmpl_preference
);
use Opals::Date qw(
    date_text
);
use Opals::Context;
use Opals::Locale qw(
    loc_getMsgFile
    loc_write
);
use JSON;
# Version number
$VERSION   = 0.01;      
use Time::localtime;
my $tm = localtime;
my $today = sprintf("%04d-%02d-%02d", $tm->year+1900, ($tm->mon)+1, $tm->mday);

#======================================================================
sub emailSvc_holdReady{
    my ($dbh,$param)=@_;
    my $pref    = tmpl_preference($dbh);
    my $status=0;
    my $rnCont =_getReserveNotice($dbh,$param);
    #printf("%s\n\n%s\n",$rnCont,$param->{'email'});
     if ($rnCont ne '' && $param->{'userInfo'}->{'noticeByEmail'} 
          && mail_send($pref, $param->{'email'}, $param->{'emailSubject'} , $rnCont)){
        $status=1;
    }
    return $status;
}

#======================================================================
sub emailSvc_cancelReserve{
    my ($dbh,$param)=@_;
    my $pref    = tmpl_preference($dbh);
    my $status=0;
    my $rnCont =_getContentNotice($dbh,$param);
    if ($rnCont ne '' && mail_send($pref,$param->{'email'} ,$param->{'emailSubject'} , $rnCont)){
        $status=1;
    }
    return $status;
}

#======================================================================
sub emailSvc_reject{
    my ($dbh,$param)=@_;
    my $pref    = tmpl_preference($dbh);
    my $status=0;
    my $rnCont =_getContentNotice($dbh,$param);
    if ($rnCont ne '' && mail_send($pref, $param->{'email'},$param->{'emailSubject'}, $rnCont)){
        $status=1;
    }

    return $status;
}

#======================================================================
sub _getContentNotice{
    my ($dbh,$param)=@_;
    my $template = _getTemplate("/htdocs/theme/opals/util/template/","emailContent.inc");
    my @rsList =();
    my $rs={};
    $rs->{'libName'}    =$param->{'libName'};
    $rs->{'today'}      =$today;
    foreach my $f qw(title author pubName pubDate callnumber){
        $rs->{$f}=$param->{'itemInfo'}->{$f};
    }
    foreach my $f qw(uid  username firstname lastname){
        $rs->{"user_$f"}=$param->{'userInfo'}->{$f};
    }  
    push @rsList,$rs;
    $template->param(rsList =>\@rsList);
    #open debug,">/tmp/sss"; print debug to_json($template->param("rsList"),{pretty=>1});close debug;
    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 _getReserveNotice{
    my ($dbh,$param)=@_;
    my $template = _getTemplate("/htdocs/theme/opals/report/","reserveNoticeCont.inc");
    my @holdList =();
    my $expiryDate;
    
    foreach my $h(@{$param->{'reserveInfo'}->{'holdList'}}){
        my $hold={};
        $expiryDate  = $h->{'dateExpiry'};

        foreach my $f qw(title author pubName pubDate callnumber){
            $hold->{$f}=$param->{'itemInfo'}->{$f};
        }
        $hold->{"user_barcode"}=$param->{'userInfo'}->{'userbarcode'};
        foreach my $f qw(homeroom teacher grade notes email){
            $hold->{$f}=$param->{'userInfo'}->{$f};
        }   
        foreach my $f qw(uid  username firstname lastname){
            $hold->{"user_$f"}=$param->{'userInfo'}->{$f};
        }  
        $hold->{'numOfHold'} =1;
        $hold->{'expiryDate'}=$h->{'dateExpiry'};
        $hold->{'libName'}=$param->{'libName'};
        $hold->{'libType'}=$param->{'libType'};
        $hold->{'today'}=$today;
        $hold->{'msg'}=$param->{'msg'};
        push  @holdList,$hold;
    }
   
   $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 _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;    
}

############################################################################################
1;

