#!/usr/bin/perl

use strict;
use CGI;

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

use Opals::Equipment qw(
    eq_defRecord_getList
    eq_defRecord_getLists
    eq_defItem_getList
    eq_defItem_getLists
    eq_defRequired_getList
    eq_itemType_getList
    
    eq_record_add
    eq_record_idGen

    eq_item_add
    eq_item_getBarcodeList
);

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


my @fDataReq    =   ($cgi->param('fDataReq')) ;
my @fIdReq      =   ($cgi->param('fIdReq')) ;
my @fData       =   ($cgi->param('fData')) ;
my @fId         =   ($cgi->param('fId')) ;
my $save        =   $input->{'save'};

my ($fiId, $fiData);
if ($save){
    my $rid = eq_record_idGen($dbh);
    my $i = 0;
    foreach my $id (@fIdReq){
        my $reqField = 1;
        my $params = {rid=>$rid, fId=>$id, fValue=>@fDataReq[$i], reqField=>1};
        eq_record_add($dbh, $params );
        $i++;
    }
    $i = 0;
    foreach my $id (@fId){
        my $reqField = 0;
        my $params = {rid=>$rid, fId=>$id, fValue=>@fData[$i]};
        eq_record_add($dbh, $params);
        $i++;
    }
    my $sfDataReqList = eq_defRequired_getList($dbh, 'item');
    my $sfDataReq;
    my $n ;
    foreach my $sfReq (@$sfDataReqList){
        my $subfieldReq = "sfDataReq_" . $sfReq->{'id'};
        my @tmpArrReq =($cgi->param($subfieldReq));
        $n =scalar(@tmpArrReq);
        $sfDataReq->{$sfReq->{'id'}} = \@tmpArrReq;
    }
    my ($bcList,$itList);
    foreach my $sfReq (@$sfDataReqList){
        if ($sfReq->{'name'} eq 'Bar Code'){
            $bcList = $sfDataReq->{$sfReq->{'id'}};
        }
        if ($sfReq->{'name'} eq 'Item Type'){
            $itList = $sfDataReq->{$sfReq->{'id'}};
        }
    }
    my $sfList = eq_defItem_getList($dbh);
    my $subfieldData;

    $n = 0;
    foreach my $sf (@$sfList){
        my $subfield = "sf_" . $sf->{'id'};
        my @tmpArr =($cgi->param($subfield));
        $n =scalar(@tmpArr);
        $subfieldData->{$sf->{'id'}} = \@tmpArr;
    }
    my @eqItemList;
    for( my $i=0; $i<$n;$i++){
        my $item;
        foreach my $fname (keys %$subfieldData){
            $item->{$fname}=$subfieldData->{$fname}->[$i];
        }
        push @eqItemList,$item;
    }
    my $iid=0;
    my $i = 0;
    
    if (scalar(@eqItemList)>0){
        foreach my $item (@eqItemList){
            $iid++;
            my $params = {iid=>$iid, rid=>$rid, barcode=>$bcList->[$i], typeId=>$itList->[$i], reqField=>1};
            eq_item_add($dbh,$params);
            foreach my $fn(sort keys %$item){
                my $params = {iid=>$iid, rid=>$rid, sfId=>$fn,sfValue=>$item->{$fn}};
                eq_item_add($dbh,$params);
            }
            $i++;
        }
    }
    else{
         foreach my  $b(@$bcList){
            $iid++;
            my $params = {iid=>$iid, rid=>$rid, barcode=>$bcList->[$i], typeId=>$itList->[$i], reqField=>1};
            eq_item_add($dbh,$params);
            $i++;
        }
    }

}
    #my $barcodeList = eq_item_getBarcodeList($dbh);
    
    my $defRecordList = eq_defRecord_getLists($dbh);
    my $defItemLists = eq_defItem_getLists($dbh);
    my $itemTypeList = eq_itemType_getList($dbh);

    $template->param (
        defRecordList   => $defRecordList,
        defItemLists     => $defItemLists,
        itemTypeList    => $itemTypeList,
        equipment_new   => 1,
        
);

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