#!/usr/bin/perl

#use utf8;
use strict;
use CGI;
use Encode;

use Opals::Context;

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

use Opals::Utility qw(
    util_escapeXml
);
use Opals::Search qw(
    srch_barcodeDuplicated
    srch_F852Default_marcxml
);
use Opals::Locale qw(
    loc_getMsgFile
    loc_write
);

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        => 'marc21/illItemEntry.tmpl',
                reqPermission   => 'marc_edit',
            }
    );

my ($zid,$op);

$zid     = 0;
$op      = $input->{'op'};

my $loginuid = $template->param('curUserId');
my $syspref = tmpl_preference($dbh);
my $self = $ENV{'SCRIPT_NAME'};

my $f852 =srch_F852Default_marcxml($dbh,1);
my ($sf852a,$sf852b)=("","");
if($f852 =~ m/<subfield code="a">(.*)<\/subfield>/){
    $sf852a=$1
}
if($f852 =~ m/<subfield code="b">(.*)<\/subfield>/){
    $sf852b=$1
}
my $loadILLRecByType = $input->{'loadILLRecByType'};
my $editMode    = $input->{'eMode'};
$template->param("editMode_$editMode"     => 1,
                  isTmpIll =>($editMode ne '',
                  loadILLRecByType =>$loadILLRecByType)
                 );           
if ($permission && $permission->{'marc_edit'}) {
     # GET LIST OF ITEM TYPE FROM opl_itemType TABLE
        my $itemTypeList = getItemTypeList($dbh);
        $template->param(
            itemTypeList => $itemTypeList,
            sf852a       => $sf852a,
            sf852b       => $sf852b,
        );

}
 my $msgValMap={
                    
                    };
$template->param(hlpUrl     => Opals::Constant->getHlpUrl('quickItemEntry') );
  my $marc21EditMsgMap =loc_getMsgFile('marc21/edit.msg');
  loc_write($template,$marc21EditMsgMap);
  my $illMsgMap =loc_getMsgFile('ill/ill.msg');
  loc_write($template,$illMsgMap);
 
tmpl_write($dbh, $cgi, $cookie, $template);
#$dbh->disconnect();
##############################################################################
### Add 2008 02 14 : Item type Authority
sub getItemTypeList{
    my ($dbh)= @_;
    my $sql = <<_SQL_;
SELECT     distinct id ,defaultType 
FROM       opl_itemType
WHERE      id <>""
ORDER BY    id
_SQL_

    my $sth = $dbh->prepare($sql);
    $sth->execute();
    my @retVal;
    my $itentypeStr=""; 
    while (my $rec = $sth->fetchrow_hashref) {
          push @retVal, $rec;
    }
    $sth->finish;
           
    return \@retVal;
}

