#!/usr/bin/perl

use strict;
use CGI;

use Opals::Context;
use Opals::Constant;
use Opals::Template_ajax qw(
    tmpl_read
    tmpl_write
);

use Opals::ReserveShelf qw(
    rs_getReserveShelfByID
);


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        => '/ajax/rs/getReserveShelf.tmpl',
        reqPermission   => 'circ_rsrv',
        tmplLocalVars  =>1, 
    }
);

      
if ($permission && $permission->{'circ_rsrv'}) {
   my $shelfId          = $input->{'shelfId'};
   my $op          = $input->{'op'};
   my $rec= rs_getReserveShelfByID($dbh,$shelfId);
   $rec->{'shelfId'}="" if($op eq 'clone');
   foreach my $f qw(shelfId author title teacher reserveFrom reserveTo course introduction loanPeriod loanType){
        if($f eq 'reserveFrom' || $f eq 'reserveTo'){
            $rec->{$f} = substr($rec->{$f},0,10);
        }
        $template->param( "$f"  => $rec->{$f});
   }
   my @subjectList=();
   foreach my $sbj(@{$rec->{'subjectList'}}){
       push @subjectList,$sbj;
   }
   $template->param( subjectList  =>\@subjectList);   
   
   my @rsList=(); 
   foreach my $rs(@{$rec->{'rsList'}}){
       push @rsList,$rs;
   }
   $template->param(rsList =>\@rsList);   


   my @userList=();
   foreach my $u(@{$rec->{'userList'}}){
       push @userList,$u;
   }
   $template->param(userList   =>\@userList); 
}

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


#======================================================================







