#!/usr/bin/perl

#use utf8;
use strict;
use CGI;
use Digest::SHA qw(
    sha1_base64
    sha1_hex
);

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

use Opals::Date qw(
    date_f005
);
use Opals::Utility qw(
    util_escapeXml
);
use Opals::Search qw(
    srch_barcodeDuplicated
    srch_F852Default_marcxml
);
use Opals::MarcXml qw(
    mxml_update
);

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

my $cgi      = CGI->new;
my $input    = $cgi->Vars();
my $op       = $input->{"op"};
my $title    = $input->{'title'};
my $author   = $input->{'author'};
my $itemType = $input->{'itemType'};
my $barcode  = $input->{'barcode'};
my $sf852a   = $input->{'sf852a'};
my $sf852b   = $input->{'sf852b'};
my $sf852k   = $input->{'sf852k'};
my $sf852h   = $input->{'sf852h'};
my $sf852i   = $input->{'sf852i'};

my $isTempItem = $input->{'tempItem'};

my ($status,$errorCode,$errorMsg)    ;
my ($permission, $cookie, $template) = tmpl_read(
        {
            dbh             => $dbh,
            cgi             => $cgi,
            tmplFile        => 'ajaxForm/ajaxItemQE.tmpl',
            reqPermission   => 'marc_edit',
        }
    );


   my $dupBc = srch_barcodeDuplicated($dbh,$barcode);
   if($dupBc ne ''){
       $template->param(status    => 'fail',
                        errorCode => 1,
                        errorMsg  => 'ERROR: duplicate barcode. Record cannot be saved!',
                     );
   }
   else{
      $title    = util_escapeXml($title);
      $author   = util_escapeXml($author);
      $itemType = util_escapeXml($itemType);
      $barcode  = util_escapeXml(uc($barcode));
      my $f005  = date_f005();
      
      my $marcxml = 
      "<record>\n" .
      "  <leader>00496nam  2200157 a 4500</leader>\n" .
      "  <controlfield tag=\"001\">0</controlfield>\n" .
      "  <controlfield tag=\"005\">$f005</controlfield>\n" .
      "  <controlfield tag=\"008\">960618s1973    xxu           000 0 eng d</controlfield>\n".
      "  <datafield tag=\"100\" ind1=\"1\" ind2=\" \">\n"  .   
      "    <subfield code=\"a\">$author</subfield>\n" .
      "  </datafield>\n" .
      "  <datafield tag=\"245\" ind1=\"1\" ind2=\" \">\n"  .   
      "    <subfield code=\"a\">$title</subfield>\n" .
      "  </datafield>\n" .
      "  <datafield tag=\"852\" ind1=\"1\" ind2=\" \">\n"  .     
      "    <subfield code=\"a\">$sf852a</subfield>\n" .
      "    <subfield code=\"b\">$sf852b</subfield>\n" .
      "    <subfield code=\"p\">$barcode</subfield>\n" .
      "    <subfield code=\"3\">$itemType</subfield>\n" ;

      $marcxml .= "    <subfield code=\"k\">$sf852k</subfield>\n" if($sf852k ne '');
      $marcxml .= "    <subfield code=\"h\">$sf852h</subfield>\n" if($sf852h ne '');
      $marcxml .= "    <subfield code=\"i\">$sf852i</subfield>\n" if($sf852i ne '');

      $marcxml .= "  </datafield>\n" .
      "</record>\n";
      
      my $incomplete   = "true";
      my $temporaryILL = "";
         $temporaryILL =  $input->{'temporaryILL'}; 
      my $bcList={$barcode=>{status=>'new'}};   
      my $rid = mxml_update($dbh, {rid=>0, marcXml=>$marcxml, bcList=>$bcList,tempIll=>$temporaryILL,inComplete=>$incomplete} ) ;
      if($rid){
          $template->param(
              status       =>  'OK',                           
          );
      }
      else{
          $template->param(
              status       =>  'fail',
              errorCode    =>   2,
              errorMsg     =>   'Record cannot be saved. Please try again.'
          );
      }
  }
  
tmpl_write($dbh, $cgi, $cookie, $template);
#------------------------------------------------------------------------------
