#!/usr/bin/perl

#use utf8;
use strict;
use CGI;

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

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

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

);

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


################################################################################
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/bEmail.tmpl',
            reqPermission   => 'circ_rsrv_self|circ_rsrv',
        }
);
    my $dateToday = date_f005();
    $dateToday =~ s/([\d]{4})([\d]{2})([\d]{2})[\d]+\.(0|1)/$1-$2-$3/;
my $pref    = tmpl_preference($dbh);
my $from    = $pref->{'emailAddress'};
my $smtp    = $pref->{'emailSmtp'};
my $circulationSound = $pref->{'circulationSound'};

my @cookieList  = (@$cookie);
my $tm          = localtime;
my $todayStr    = sprintf("%04d-%02d-%02d", $tm->year+1900, ($tm->mon)+1, $tm->mday);
my $to          = $cgi->param('email');

my $bid = $input->{'selBid'};
my $msgMap   =loc_getMsgFile('circ/booking.msg');
if ($permission && ($permission->{'circ_rsrv_self'}|| $permission->{'circ_rsrv'})){   
    if(!$to){
        $template->param(
            openForm    =>1,
            email       =>$to,
            bookingId   => $bid
       );
    }
    else{
        $to =~ s/ +/,/g;
        $to =~ s/;+/,/g;
        $to =~ s/,+/,/g;
        $to =~ s/(^,|,$)//g;
       
        my $bookingCont = getContent($dbh,$cgi,$bid,$msgMap); 
        if (mail_send($pref, $to, 'Booking', $bookingCont)){
           $template->param(done=>1);
         }
        else{
           $template->param(error=>1);
        }
        $template->param(email=>$to );
   }
    
         
}#if ($permission && $permission->{'circ_loan'})
loc_write($template,$msgMap);
tmpl_write($dbh, $cgi, \@cookieList, $template);

###############################################################################
sub getContent{
    my($dbh,$cgi,$bid,$msgMap)=@_;
    my ($permission, $cookie, $template) = tmpl_read(
        {
            dbh             => $dbh,
            cgi             => $cgi,
            tmplFile        => 'circ/bViewCont.inc',
        }
    );
    my $booking =bs_getBookingById_libCat($dbh,$bid);

            $template->param(
                department          => $booking->{'department'},
                course              => $booking->{'course'},
                reqFirstname        => $booking->{'reqFirstname'},
                reqLastname         => $booking->{'reqLastname'},
                reserveFrom         => $booking->{'reserveFrom'},
                reserveTo           => $booking->{'reserveTo'},
                startDate           => $booking->{'startDate'},
                startTime           => $booking->{'startTime'},
                endDate             => $booking->{'endDate'},
                endTime             => $booking->{'endTime'},
                itemList            => $booking->{'itemList'},
                reserve4UserList    => $booking->{'reserve4UserList'},
            );
    loc_write($template,$msgMap);
    
    return $template->output;
}

