#!/usr/bin/perl

#use utf8;
use strict;
use CGI;

use Opals::Context;use POSIX qw(
    floor
);

use Time::localtime;
use Opals::Constant;

use Opals::Template qw(
    tmpl_read
    tmpl_write
    tmpl_preference
);
use Opals::User qw(
    user_currentUser
    user_getInformationById
);

use Opals::MarcXml qw(
    mxml_del_rec_holding
);
use Opals::Session qw(
    SessionHdl_clearVar
);
use URI::Escape;

use Opals::UnionRequest qw(
    urq_createUnionRequest
);
use Opals::Locale qw(
    loc_getMsgFile
    loc_write
);

use Opals::BarcodeMgmt qw(
    bcm_validateBc
);

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        => 'shelfReserve_1.tmpl',
            reqPermission   => 'report',
        }
);
$template->param(hlpUrl     => Opals::Constant->getHlpUrl('delExpFromHitlist'));
my ($errCode, $myCookie, $user) = user_currentUser($dbh, $cgi);
my $uid = $user->{'uid'};

my $msgValMap ={};
my $op       =  $input->{'op'};
my $source   =  $input->{'source'};
my $bcList   =  $input->{'bcList'};
my $sql = <<_SQL_;
select m.*,i.callNumber from opl_marcRecord m inner join opl_item i using(rid) where i.barcode= ? 
_SQL_

my $syspref     = tmpl_preference($dbh);
my $validateBc  = $syspref->{'validateBarcode'}; 
my $barcodeType = $syspref->{'barcodeType'}; 


my $bc   =  $input->{'bc'};
open debug,">/tmp/ss"; print debug $bc."\n"; close debug;
if( $bc ne ''){
    my $itemInfo;
    my $retVal;
         if($validateBc eq '1'){
             $bc=bcm_validateBc($dbh,$bc,$barcodeType);
         }
open debug,">/tmp/ss2"; print debug $bc."\n"; close debug;
        $itemInfo = $dbh->selectrow_hashref($sql, undef, $bc);
        if($itemInfo){
       	     $retVal->{'title'}      = $itemInfo->{'title'};
             $retVal->{'callNumber'} = $itemInfo->{'callNumber'};
             $retVal->{'author'}     = $itemInfo->{'author'};
             $retVal->{'pubPlace'}   = $itemInfo->{'pubPlace'};
             $retVal->{'pubName'}    = $itemInfo->{'pubName'};
             $retVal->{'pubDate'}    = $itemInfo->{'pubDate'};
             $retVal->{'rid'}        = $itemInfo->{'rid'};
             $retVal->{'commentNote'}= '';
        }

	$template->param(
	      itemInfo => "$retVal",
	);
}


my $msgMap            =loc_getMsgFile('report/reports.msg',$msgValMap);
loc_write($template,$msgMap);

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


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






