#!/usr/bin/perl

#use utf8;
use strict;
use CGI;
use Encode;
use PDF::Create;
use POSIX qw(
    floor
    ceil
);

use Opals::Context;

use Time::localtime;

use Opals::Date qw(
    date_parse
    date_today
    date_text
    date_f005
);

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

use Opals::Locale qw(
    loc_getMsgFile
    loc_write
    
);

use Opals::Transactions qw(
    trans_getUnpaidFineList
    trans_getBalance
    trans_getLastBalance
    trans_getTotalCharge
    trans_getTotalFgvPmt
    );


use Opals::Mail qw(
    mail_enqueueMail
    mail_validateEmail
);

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

my $cgi    = CGI->new;
my $input  = $cgi->Vars();
my $pref   = tmpl_preference($dbh);
my $libname    = decode('utf8', $pref->{'libname'});
my $libAddress = decode('utf8', $pref->{'libAddress'});
my $libCity    = decode('utf8', $pref->{'libCity'});
my $libState   = decode('utf8', $pref->{'libState'});
my $libZip     = decode('utf8', $pref->{'libZip'});
my $libPhone   = decode('utf8', $pref->{'libPhone'});
my $libFax     = decode('utf8', $pref->{'libFax'});
my $libEmail   = decode('utf8', $pref->{'libEmail'});

#-------------------------------------------------------------------------------------

my $op       = $input->{'op'}; #print or email
my $sort1    = $input->{'sort1'};
my $sort2    = $input->{'sort2'};
my $optTitle = $input->{'optTitle'};
my $optBilg  = $input->{'optBilg'};
my $prtType  = $input->{'prtType'};
my $optPrice = $input->{'optPrice'};
my $optPage  = $input->{'optPage'};
my $printGreeting =$input->{'optGreeting'};

my $msgTopCbox = $input->{'msgTopCbox'};
my $msgBotCbox = $input->{'msgBotCbox'};
my $srcCall    = $input->{'srcCall'};
my $incOpt     = $input->{'incOpt'};

my @msgArr=();
my @lang;
my $l;
if($optBilg eq 'enSp'){
   @lang=('en','sp');
   $l ='en';
}
elsif($optBilg eq 'frOnly'){
   @lang=('fr');
   $l='fr';
}
else{
   @lang=('en');
   $l='en';
}
#-------------------------------------------------------------------------------------
use constant    X_PAGE         =>  220;
use constant    X_LEFT_MARGIN  =>  15;
use constant    X_RIGHT_MARGIN =>  25;
use constant    TOP_MARGIN     =>  15;
use constant    Y_PAGE         =>  792;
use constant    X_TABLE        =>  558;
use constant    ROW_HEIGHT     =>   18;
use constant    ROW_HEIGHT_10  =>   12;
use constant    X_ADDRESS      =>   86;
use constant    Y_ADDRESS_FR   =>   772;
use constant    Y_ADDRESS_TO   =>   640;
use constant    Y_BODY         =>   578;
use constant    HEADER_HEIGHT_1=>   50;
use constant    HEADER_HEIGHT_2=>   50;
use constant    FOOTER_HEIGHT  =>   20;


   my $todayStr ="";                         
   my  $inMsgTop = decode('utf8',$input->{'msgTop'});
       $inMsgTop =~ s/_EOL_/\n/;
       $inMsgTop =~ s/\n+/\n/;
      
   my  $inMsgBot = decode('utf8',$input->{'msgBot'});
       $inMsgBot =~ s/_EOL_/\n/;
       $inMsgBot =~ s/\n+/\n/;
      

    my $mypdf=`/bin/mktemp`;
    my $pdf = new PDF::Create('filename' => $mypdf,
                              'Version'  => 1.2,
                              'PageMode' => 'UseOutlines',
                              'Title'    => 'My title',
                         );

    my $root = $pdf->new_page('MediaBox' => [ 0, 0, X_PAGE,Y_PAGE ]);

    my $f1 = $pdf->font('Subtype'  => 'Type1',
                        'Encoding' => 'WinAnsiEncoding',
                        'BaseFont' => 'Helvetica');
    my $f2 = $pdf->font('Subtype'  => 'Type1',
                        'Encoding' => 'WinAnsiEncoding',
                        'BaseFont' => 'Helvetica-Bold');
    my $f3 = $pdf->font('Subtype'  => 'Type1',
                        'Encoding' => 'WinAnsiEncoding',
                        'BaseFont' => 'Times-Italic');

  

   
          
     
   my $pageN = $root->new_page;
   my $pTxtArr = printOd_pdf();
 
        open PDF, "<$mypdf";
        print $cgi->header(
            -type           => 'application/pdf',
            -attachement    => 'OverdueNotice.pdf'
        );
         createPdf($pTxtArr);
         $pdf->close;

            while (<PDF>) {
                print $_;
            }
        close PDF;
    

#----------------------------------------------------------------------------------------------------
 sub printOd_pdf{
    my ($x,$y)=(X_LEFT_MARGIN, Y_PAGE-15);
    my $n=0;
    my $txtArr=[
    'Millstone Township Middle School Library',
    'Millstone Township NJ 08535',
    '',
    'Adamo, Alexis',
    '150789',
    '5 Dawson Court NJ 012345',
    '',
    '',
    'Materials being checked out/renewed:',
    '',
    'Item title Item titl Item titl Item titl Item titl Item titl Item titlItem titlItem titl Item titl Item titl',
    'bc223399',
    'Item author',
    '01/17/12',
    '--------------------------------------------',
    'Other materials currently checked out:',
    '2  Item title Item titl Item titl Item titl Item titl Item titl Item titlItem titlItem titl Item tit Item 2',
    'bc223399',
    'Item author',
    '01/17/12',
    '3  Item title Item titl Item titl Item titl Item titl Item titl Item titlItem titlItem titl Item titl It 3',
    'bc223399',
    'Item author',
    '01/17/12',
    '--------------------------------------',
    '4 Item title Item titl Item titl Item titl Item titl Item titl Item titlItem titlItem titl Item titl    4',
    'bc223399',
    'Item author',
    '01/17/12',
    '--------------------------------------',
    '5  Item title Item titl Item titl Item titl Item titl Item titl Item titlItem titlItem titl Item  wwww 5',
    'bc223399',
    'Item author',
    '01/17/12',
    '6  Item title Item titl Item titl Item aaaaaaaa dddd gggggg hhhhhhh ',
    'bc223399',
    'Item author',
    '01/17/12',
    '7 Item title Item titl Item titl Item titl Item titl Item titl Item mmmmm ggggggg',
    'bc223399',
    'Item author',
    '01/17/12',
    '8  Item title Item titl Item titl Item titl Item titl Item titl Item titlItem titlItem titl Item titl Item titl',
    'bc223399',
    'Item author',
    '01/17/12',
    '',
    
    '9  Item title Item titl Item titl Item titl Item titl Item ',
    'bc223399',
    'Item author',
    '01/17/12',
    '',
     '--------------------------------------',
    '',
    '10  Item title Item titl Item titl Item titl Item titl Item titl Item titlItem titlItem titl Item  wwww 5',
    'bc223399',
    'Item author',
    '01/17/12',
    '',
   
    '11  Item title Item titl Item titl Item aaaaaaaa dddd gggggg hhhhhhh ',
    'bc223399',
    'Item author',
    '01/17/12',
    '',
    '12 Item title Item titl Item titl Item titl Item titl Item titl Item mmmmm ggggggg',
    'bc223399',
    'Item author',
    '01/17/12',
    '',
    '13  Item title Item titl Item titl Item titl Item titl Item titl Item titlItem titlItem titl Item titl Item titl',
    'bc223399',
    'Item author',
    '01/17/12',
    '',
    
    '14  Item title Item titl Item titl Item titl Item titl Item ',
    'bc223399',
    'Item author',
    '01/17/12',
    '',
    
    '15  Item title Item titl Item titl Item aaaaaaaa dddd gggggg hhhhhhh ',
    'bc223399',
    'Item author',
    '01/17/12',
    '',
    '16 Item title Item titl Item titl Item titl Item titl Item titl Item mmmmm ggggggg',
    'bc223399',
    'Item author',
    '01/17/12',
    '',
    '17  Item title Item titl Item titl Item titl Item titl Item titl Item titlItem titlItem titl Item titl Item titl',
    'bc223399',
    'Item author',
    '01/17/12',
    '',
    
    '18  Item title Item titl Item titl Item titl Item titl Item ',
    'bc223399',
    'Item author',
    '01/17/12',
    '',
   '19 Item title Item titl Item titl Item titl Item titl Item titl Item titlItem Weighing & balancing Weighing & balancing Weighing & balancing Weighing & balancing',
    'bc223399',
    'Item author',
    '01/17/12',
    ''
    ]; 



    my $pdfTxtArr=[];


  
     foreach my $txt (@$txtArr){
         my $a=formatStr_nRow($pageN,$f2,10,$txt,200,100);
         @$pdfTxtArr =(@$pdfTxtArr, @$a);
     }

     return $pdfTxtArr;
    #createPdf($pdfTxtArr);                      
    #$pdf->close;


}

#----------------------------------------------------------------------------------------------------
sub createPdf{
    my ($txtArr)=@_;
    my $height=scalar(@$txtArr) * ROW_HEIGHT_10;
    my ($x,$y)=(X_LEFT_MARGIN, Y_PAGE - TOP_MARGIN);
    foreach my $t(@$txtArr){
        if($y - ROW_HEIGHT_10  < FOOTER_HEIGHT){
             $pageN = $root->new_page;
             $y =  Y_PAGE - TOP_MARGIN ;
        }

        $pageN->stringl($f2, 10,$x, $y ,   $t) if($t ne '');
        $y -=ROW_HEIGHT_10;

    }
    #$pageN->stringl($f2, 10,$x, $y ,   "aaaaaaaa");
}
#----------------------------------------------------------------------------------------------------
sub formatStr_nRow{
    my($pageN,$f,$fs,$inStr,$len,$nRow)=@_;
    my $outStr=$inStr;
    my @outStrArr=();
    $outStr =~ s/\s+$//g;
    my $n=1;
    my $sWidth= $fs * $pageN->string_width($f,$outStr);
    if($sWidth<=$len){
        $outStr =~ s/(\(|\))/\\$1/g;
        push @outStrArr,$outStr;
        return \@outStrArr;
    }
    while($n<$nRow && $outStr ne ''){
        while($sWidth > $len){
           $outStr =~ s/\S+$//g;
           $outStr =~ s/\s+$//g;
           $sWidth= $fs * $pageN->string_width($f,$outStr );
        }
        push @outStrArr,$outStr;
        $inStr =substr($inStr,length($outStr)); 
        $inStr =~ s/^\s+//g;
        $outStr=$inStr;
        $sWidth= $fs * $pageN->string_width($f,$outStr);
        $n++;
    }
    if($n >=$nRow && $inStr ne ''){
        push @outStrArr,formatStr($pageN,$f,$fs,$inStr,$len);
    }
    foreach my $str(@outStrArr){
         $str=~ s/(\(|\))/\\$1/g;
    }
    return \@outStrArr  ;
}

sub formatStr{
    my($pageN,$f,$fs,$inStr,$len)=@_;
    my $outStr=$inStr;
    $outStr =~ s/\s+$//g;

    my $sWidth= $fs * $pageN->string_width($f,$outStr);
    if($sWidth<=$len){
        return $outStr;
    }
    while($sWidth > $len){
       $outStr =~ s/\S+$//g;
       $outStr =~ s/\s+$//g;
       $sWidth= $fs * $pageN->string_width($f,$outStr );
    }
    return $outStr  ;
}

