#!/usr/bin/perl

use strict;
use CGI;

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

use Opals::Equipment qw(

    eq_defItem_getLists
    eq_defItem_getList
    eq_defRecord_getList

    eq_record_findByRId
    eq_record_addUpdate
    eq_record_update_rname
    
    eq_item_findByRId
    eq_item_addUpdateBarCodeItemType
    eq_item_addUpdate

    eq_item_delete
    eq_item_deleteSubFields
    eq_item_deleteBarcode
    eq_isBarcodeExist
    eq_itemType_getList
);

use Opals::Eq_Search qw(
      
    eq_category_getList
    eq_category_getListById

);

use Opals::Eq_Circulation qw(

    circ_getLoanListByRid

);

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/record/editRec.tmpl',
        reqPermission   => 'eq_record_edit',
    }
);

my $categoryList = eq_category_getList($dbh);

my $rid     = $input->{'rId'};
my $edit    = $input->{'edit'};
my $op      = $input->{'op'};
    $op = "edit" if (!$op ||$op eq "");

my $recordInfo = [];
my $itemList;

my ($rFieldList,$iFieldList);
my $iDefList = eq_defItem_getList($dbh);
my $rDefList = eq_defRecord_getList($dbh);
my $eqItemTypeList = eq_itemType_getList($dbh);

#my $barcodeList = eq_item_getBarcodeList($dbh,$rid);

    ($rid, $recordInfo) = eq_record_findByRId($dbh,{recordId=>$rid});
    $itemList = eq_item_findByRId($dbh, $rid);
    my $max_iid=0;
    my $iCount = 0;
    
    my $onLoanList =  circ_getLoanListByRid($dbh,$rid);
    foreach my $i(@$itemList){
        $iCount++;
        $i->{'onLoan'} = "";
        $max_iid=$i->{'iid'} if($i->{'iid'}>$max_iid);
        foreach my $ol (@$onLoanList){
            if ($ol->{'barcode'} eq $i->{'barcode'}){
                $i->{'onLoan'} = 'onLoan';
            }
        }
    }
    foreach my $f (@$rDefList){
        push @$rFieldList , {
           fId      => $f->{'id'},
           fVal     => "",
           fName    => $f->{'name'},
           fDisplay => $f->{'display'},
           fType    => $f->{'fieldType'},
        }
    }
    foreach my $f(@$iDefList){
        push @$iFieldList, {
             sfId       => ($f->{'sfId'})? $f->{sfId}:$f->{'id'},
             sfVal      => "",
             sfName     => $f->{'name'},
             sfReqD     => $f->{'reqD'},
             sfDisplay  => $f->{'display'},
             url_link   => ($f->{'fieldType'} eq '2')?1:0,
             sfType     => $f->{'fieldType'},
             dateType   => ($f->{'dataType'} eq "Date")?"1":"0"
        }
    }

    my @sorted = sort { $a->{'iid'} cmp $b->{'iid'} || $a->{'copyNo'} cmp $b->{'copyNo'}} @$itemList;

    $template->param(
        rid             => $rid,
        max_iid         => $max_iid,
        recordInfo      => $recordInfo,
        itemList        => \@sorted,    #$itemList,
        iFieldList      => $iFieldList,
        rFieldList      => $rFieldList,
        eqItemTypeList  => $eqItemTypeList,
        iCount          => ($iCount)? $iCount : 0,
        #barcodeList     => $barcodeList,
        categories      => $categoryList,
        cat             => $recordInfo->[0]->{'category'},
    );
    
    if ($permission && $permission->{'eq_record_edit'} && $op eq 'edit') {
        $template->param(   
            op => 'edit',
        );
    }

    if ($permission && $permission->{'eq_record_edit'} && $op eq 'add') {
        $template->param(   
            equipment_add => 1,
            op => 'add',        
        );
    }

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


# This will get value for input box based on fields Id
sub eq_getDistinctItemValues {

    my ($dbh) = @_;
    my $sql = "select sfId, group_concat(distinct sfValue) from eq_itemFields where sfValue <> '' group by sfId ";
    my $sth = $dbh->prepare($sql);

    while (my ($sfId, $sfValue) = $sth->fetchrow_array()){

    }

    $sth->finish;

}

