#!/usr/bin/perl

use strict;
use CGI;

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

use Opals::Equipment qw(

    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_authCtrlFields_getList

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


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

my $recordInfo = [];
my ($rFieldList,$iFieldList);
my $iDefList = eq_defItem_getList($dbh);
my $rDefList = eq_defRecord_getList($dbh);
my $eqItemTypeList = eq_itemType_getList($dbh);
my $categoryList = eq_category_getList($dbh);
my $authCtrlFields = eq_authCtrlFields_getList($dbh);
my $libCode     = $template->param('libcode');
    my $max_iid=0;
    my $iCount = 0;
    
    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      => ($f->{'name'} =~ m/Building Code/i)?$libCode: "" ,
             sfName     => $f->{'name'},
             sfReqD     => $f->{'reqD'},
             sfDisplay  => $f->{'display'},
             url_link   => ($f->{'fieldType'} eq '3')?1:0,
             sfType     => $f->{'fieldType'},
             dateType   => ($f->{'dataType'} eq "Date")?"1":"0"
        }
    }
    $template->param(
        iFieldList      => $iFieldList,
        rFieldList      => $rFieldList,
        eqItemTypeList  => $eqItemTypeList,
        categories      => $categoryList,
        authCtrlFields  => $authCtrlFields,
        iCount          => 1 ,
   );
   if ($permission && $permission->{'eq_record_edit'} && $op eq 'edit') {
        $template->param(   
            op => 'edit',
    );
   }
   if ($permission && $permission->{'eq_record_edit'} && $op eq 'add') {
        $template->param(   
            op => 'add',        
            );
   }

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


