#!/usr/bin/perl

#use utf8;
use strict;
use CGI;

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

use Opals::Template qw(
    tmpl_read
    tmpl_write
    tmpl_preference
);
use Opals::User qw(
    user_currentUser
    user_permission
    user_permission_1
    user_StrPermission
    user_StrPermission_1

);


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

my $cgi = CGI->new;
my $input = $cgi->Vars();
my $bid=$input->{'bid'};
my ($permission, $cookie, $template) = tmpl_read(
        {
            dbh             => $dbh,
            cgi             => $cgi,
        tmplFile        => 'ebook/reader_simple.tmpl',
        }
);

my ($errCode, $cookie, $user) = user_currentUser($dbh, $cgi);
my ($uid,$authorized,$mod)=(0,0,'book');
my $username =$cgi->param('username');
if($errCode==0){
    $uid =$user->{'uid'};
    $authorized=(isAuthorized($dbh,$uid,$bid) || $permission->{'ebook_mgr'});
}

#if($loginuid){
    my $toc =getBookToc($dbh,$bid);
    my ($title,$author)=getBook($dbh,$bid);
    my $spine=getBookSpine($dbh,$bid);

    $template->param(bid                =>$bid,
                     title              =>$title,
                     author             =>$author,
                     spine              =>$spine,
                     toc                =>$toc,
                     username           =>$username,
                     uid                =>$uid,
                     readerAuthorized   =>$authorized,
                     mod                => $mod);
#}

tmpl_write($dbh, $cgi, $cookie, $template);

################################################################################

sub getBook{
    my ($dbh,$bid)=@_;
    my $sth=$dbh->prepare("select title,author from eb_record where bid=?");
    $sth->execute($bid);
    if(my ($title,$author)=$sth->fetchrow_array){
        return ($title,$author);
    }
    return (undef,undef);
    
}
################################################################################

sub getBookToc{
    my ($dbh,$bid)=@_;
    my $sth=$dbh->prepare("select * from eb_toc where bid=? order by playOrder");
    $sth->execute($bid);
    my $toc=[];
    while(my $rec=$sth->fetchrow_hashref){
        push @$toc,$rec;
    }
    return $toc;
    
}
################################################################################
sub getBookSpine{
    my ($dbh,$bid)=@_;
    my $sth=$dbh->prepare("select fid spineItem from eb_spine where bid=? order by playOrder");
    $sth->execute($bid);
    my $spine=[];
    while(my ($fid)=$sth->fetchrow_array){
        push @$spine,{spineItem=>"$bid/$fid"};
    }
    return $spine;
    
}
################################################################################

sub isAuthorized{
    my($dbh,$uid,$bid)=@_;
    my $auth=0;
    my $sth =$dbh->prepare(<<_STH_);
select l.* from eb_ridBid e 
    inner join opl_item i using(rid) 
    inner join opl_loan l using(barcode) 
where e.bid=? && uid=? && l.dateReturn is null && l.dateDue>=now()
_STH_

    $sth->execute($bid,$uid);
    if(my $rec = $sth->fetchrow_hashref){
        $auth=1;
    }
    return $auth;
}

################################################################################

