#!/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_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_item_getBarcodeList
    eq_itemType_getList
);

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

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

my $recordInfo = [];
my $itemList;
my $itemDefList = eq_defItem_getList($dbh);
my $barcodeList = eq_item_getBarcodeList($dbh,$rid);
my $eqItemTypeList = eq_itemType_getList($dbh);
if ($permission && $permission->{'eq_record_edit'} && $rid) {
    if ($edit ){
        my @fData   =  ($cgi->param('fData')) ;
        my $i = 0;
        if ($op =~ m/edit/i){
            my @fData   =  ($cgi->param('fData')) ;
            my @fId     =  ($cgi->param('fId')) ;
            my $rname   =  $input->{'rname'};
            if ($rname && $rname ne ""){
                 eq_record_update_rname($dbh,$rid,$rname);
            }

            foreach my $id (@fId){
                my $params = {rid=>$rid, fId=>$id, fValue=>$fData[$i]};
                eq_record_addUpdate($dbh, $params);
                $i++;
            }
        }
        my @iid         =  ($cgi->param('iid')) ;
        my @bc          = ($cgi->param('barcode')) ;
        my @itemType    = ($cgi->param('itemType')) ;
        my @delItem     = ($cgi->param('delItem'));

        my $subfieldData;
        foreach my $sf (@$itemDefList){
            my $subfield = "sf_" . $sf->{'id'};
            my @tmpArr =($cgi->param($subfield));
            $subfieldData->{$sf->{'id'}} = \@tmpArr;
        }
        my @eqItemList;
        my $n = scalar(@iid);
        for( my $i=0; $i<$n;$i++){
            my $item;
            $item->{'delete'} = $delItem[$i];
            $item->{'iid'} = $iid[$i];
            $item->{'barcode'} = $bc[$i];
            $item->{'typeId'} = $itemType[$i];
            my @sfs =();
            foreach my $fname (keys %$subfieldData){
                $item->{$fname}=$subfieldData->{$fname}->[$i];
            }
            push @eqItemList,$item;
        }
        foreach my $item (@eqItemList){
            if ($item->{'delete'} eq '1'){
                my $params = {rid=>$rid,iid=>$item->{'iid'}};
                eq_item_deleteBarcode($dbh,$params);
            }
        }
        $i = 0;
        foreach my $item (@eqItemList){
            $i++;
            if ($item->{'delete'} ne '1'){
                my $params =  {rid=>$rid,iid=>$item->{'iid'},barcode=>$item->{'barcode'},typeId=>$item->{'typeId'}, barcode=>$item->{'barcode'},typeId=>$item->{'typeId'}};
                eq_item_addUpdateBarCodeItemType($dbh, $params);
                foreach my $fn(keys %$item){
                    if ( $fn != 'iid') {
                        my $params = {iid=>$item->{'iid'}, rid=>$rid, sfId=>$fn,sfValue=>$item->{$fn}};
                        eq_item_addUpdate($dbh,$params);
                    }
                }
            }
        }
    }

    ($rid, $recordInfo) = eq_record_findByRId($dbh,{recordId=>$rid});
    $itemList = eq_item_findByRId($dbh, $rid);
    my $max_iid=0;
    foreach my $i(@$itemList){
        $i->{'onLoan'} = "";
        $max_iid=$i->{'iid'} if($i->{'iid'}>$max_iid);
    }
    my $onLoanList =  circ_getLoanListByRid($dbh,$rid);

    foreach my $ol (@$onLoanList){
        foreach my $i(@$itemList){
            if ($ol->{'barcode'} eq $i->{'barcode'}){
                $i->{'onLoan'} = 'onLoan';
                
            }
        }
    }

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

    $template->param(
        rid             => $rid,
        max_iid         =>$max_iid,
        recordInfo      => $recordInfo,
        itemList        => \@sorted,    #$itemList,
        itemDefList     => $itemDefList,
        eqItemTypeList  => $eqItemTypeList,
        barcodeList     => $barcodeList,
        );
   if ($permission && $permission->{'eq_record_edit'} && $op eq 'edit') {
        $template->param(   
            equipment_edit => 1,
            op => 'edit',
    );
   }
   if ($permission && $permission->{'eq_record_edit'} && $op eq 'add') {
        $template->param(   
            equipment_add => 1,
            op => 'add',        
            );
   }
}

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


