#!/usr/bin/perl

#use utf8;
use strict;
use CGI;

use Opals::Context;

use Opals::Template qw(
    tmpl_read
    tmpl_write
);

use Opals::Equipment qw(
 
    eq_record_findByRId

);

use Opals::Eq_Circulation qw(
    
    circ_infoRecord
);

use Opals::Constant;

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

my $cgi = CGI->new;
my $input = $cgi->Vars();

my ($permission, $cookieList, $template) = tmpl_read(
    {
        dbh             => $dbh,
        cgi             => $cgi,
        tmplFile        => 'eqmnt/ajax/getRecInfo.tmpl',
        reqPermission   => 'eq_record_edit',
    }
);

    my $rid = $input->{'rId'};
    my $recInfo =  eq_record_findByRId($dbh, {recordId=>$rid});
    my $rname = $recInfo->[0]->{'rname'};
    my ($numTotal, $numLoan, $numReserve, $numHold, $itemList,$totalCirc) = circ_infoRecord($dbh, $rid);

    foreach my $i (@$itemList){
        if ($i->{'dateDue'}){
            $i->{'status'} = "On Loan";
        }
        elsif($i->{'missing'}){
            $i->{'status'} = "Missing";
        }
        elsif($i->{'damaged'}){
            $i->{'status'} = "Damaged";
        }
        elsif($i->{'lost'}){
            $i->{'status'} = "lost";
        }
        else{
            $i->{'status'} = "Available";
        }
    }
    $template->param (

        rid         => $rid,
        rname       => $rname,
        totItems    => $numTotal,
        totLoan     => $numLoan,
        totReserve  => $numReserve,
        totHold     => $numHold,
        totAvailable=> $numTotal - $numLoan - $numHold,
        rInfo       => $recInfo,
        iInfo       => $itemList,

    );

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