#!/usr/bin/perl

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

use PDF::API2;
use POSIX qw(
    ceil
    floor
);

use Opals::Context;


use Opals::Template qw(
    tmpl_read
    tmpl_write
    tmpl_rangedPageList
    tmpl_preference

);


my $dbh = Opals::Context->dbh();
my $cgi = CGI->new;
my $input = $cgi->Vars();


my $rootDir = Opals::Context->config('rootDir');
my @allFontDirs = PDF::API2::addFontDirs "$rootDir/font/barcode/";

    use constant    X_TOP_RIGHT =>  594;    
    use constant    Y_TOP_RIGHT =>  756;   
    use constant    X_BOT_LEFT  =>  58;
    use constant    Y_BOT_LEFT  =>  36;   
    use constant    ROW_HEIGHT  =>  110;    
    use constant    COL_WIDTH  =>   268;
    use constant    COL1_WIDTH  =>  184;
    use constant    COL2_WIDTH  =>  198;
    use constant    COL3_WIDTH  =>  193;




umask 007;
if ( ! -d '/tmp/pdf-tmp') {
    `mkdir -p '/tmp/pdf-tmp'`;
}
my $mypdf=`/bin/mktemp -p '/tmp/pdf-tmp'`;#'/tmp/tttbbbsss';#
END {  }
#----------------------------------------------------------------------------------------------------

my $pdf  = PDF::API2->new(-file => $mypdf);
$pdf->mediabox(612,792);
my $normalFont = $pdf->corefont('Helvetica');  
my $boldFont = $pdf->corefont('Times New Roman-bold');  
my $barcodeFont = $pdf->ttfont('FREE3OF9.TTF');  
#my $barcodeFont = $pdf->ttfont('FRE3OF9X.TTF');  
#my $barcodeFont = $pdf->ttfont('BARCOD39.TTF');  
#my $barcodeFont = $pdf->ttfont('C39HDW2.TTF');  
#my $barcodeFont = $pdf->ttfont('C39HDW3.TTF');  
#my $barcodeFont = $pdf->ttfont('C39W2.TTF');  
#my $barcodeFont = $pdf->ttfont('C39W3.TTF');  

#----------------------------------------------------------------------------------------------------
 
    printLabel_BC();
   #================= OPEN PDF File ========================
#my $mypdf ="../circ/mypdf.pdf";
  open PDF, "<$mypdf";
    print $cgi->header(
        -type           => 'application/pdf',
        -attachement    => 'spineLabel.pdf'
    );
    while (<PDF>) {
        print $_;
    }
    close PDF;
    open ccc, '>/tmp/mypdf.lst';
    unlink $mypdf || warn "$mypdf: having trouble deleting $mypdf: $!\n";
    close ccc;


   
 
#----------------------------------------------------------------------------------------------------
sub printLabel_BC
   {
    my  $page  ;
    my $pIndex=0;
    my($x1,$y1)=(400,570);

my @cmdArr=(
            {cmd=>"Switch User",bc=>"OPSWUSER"},
            {cmd=>"Loans",      bc=>"OPLOAN"},
            {cmd=>"Returns",    bc=>"OPRETURN"},
            {cmd=>"Reserve",    bc=>"OPRESERVE"},
            {cmd=>"Renewals",   bc=>"OPRENEW"},
            {cmd=>"Item Status",bc=>"OPISTAT"},
            {cmd=>"In-Library Use",bc=>"OPINLIB"},
            {cmd=>"Notices",    bc=>"OPNOTICE"},
            {cmd=>"Payment",    bc=>"OPPAY"}
            );
    
    $page = $pdf->page;
    $page = $pdf->openpage($pIndex++);   
     my $txt = $page->text;
    $txt->lead(9);
    $txt->textstart;
     my $overflowTxt;
    $txt->font($normalFont, 16);
        $txt->translate(175,740);
        $overflowTxt = $txt->paragraph("Circulation Functions Barcode Sheet",400,9);
   
    writeLabel_card($page,260,$y1,
                        @cmdArr[0]->{"cmd"},
                        @cmdArr[0]->{'bc'});
       
    for(my $i=1;$i<8;$i+=2){
        $y1 -=ROW_HEIGHT;  
        if($i==3){
            $y1 -=ROW_HEIGHT; 
        }
        writeLabel_card($page,$x1 - COL_WIDTH,$y1,
                         @cmdArr[$i]->{"cmd"},
                        @cmdArr[$i]->{'bc'});
        writeLabel_card($page,$x1,$y1,
                        @cmdArr[$i+1]->{"cmd"},
                        @cmdArr[$i+1]->{'bc'});
                        
               
    }  
  
                
     $pdf->save;
     $pdf->end();
}
 


#----------------------------------------------------------------------------------------------------
sub writeLabel_card{
    my($page,$x,$y,$cmdName,$bc)=@_;
    my $i=0;
    $y = $y +ROW_HEIGHT -15 ;
    my $txt = $page->text;
    $txt->lead(9);
    $txt->textstart;
     my $overflowTxt;
    $txt->font($normalFont, 16);
        $txt->translate($x,$y);
        $overflowTxt = $txt->paragraph("$cmdName",100,9);
    
    # write BC 
    $txt->font($barcodeFont, 24);
    $txt->translate($x,$y-44);
    $overflowTxt = $txt->paragraph("*$bc*",160,28);
    $txt->font($normalFont, 8);
    $txt->translate($x,$y-53);
    $overflowTxt = $txt->paragraph($bc,120,9);
     $txt->textend;
}
    
   
