#!/usr/bin/perl

#use utf8;
use strict;
use CGI;
use JSON;
use Opals::Context; 
my $dbh = Opals::Context->dbh();
END { $dbh->disconnect(); }

my $itParams=getItemTypeParam();
my $cgi     = CGI->new;
my $itemTypeInfo =$itParams->{'itemTypeInfo'};
my $itemTypeParam=$itParams->{'itemTypeParam'};

#open debug, ">/tmp/debugSaveItemType";
#while (my ($k,$v) = each %$itemTypeInfo){
#    print debug "$k=>$v \n";
#}
#close debug;
saveItemType($dbh,$itemTypeInfo);
saveItemTypeParam($dbh,$itemTypeInfo->{"id"}, $itemTypeParam);

print "Content-type: text/plain\n\n";
print  to_json({status=>1});
#===================================================================================================
sub saveItemType{
    my($dbh,$typeInfo)=@_;
    $typeInfo->{"id"} =~ s/^\s+|\s+$//g;
    if ($typeInfo->{"defaultType"}) {
        $dbh->do('update opl_itemType set defaultType = 0');
    }
    $dbh->do("delete from opl_itemType where id='$typeInfo->{'id'}' && itemCategory!= $typeInfo->{'itemCategory'} limit 1");

    my $sth=$dbh->prepare(<<_SQL_);
replace into opl_itemType
set     id          = ?,
        description = ?,
        defaultType = ?,
        itemCategory = ?,
        timeOnShelvingCart=?
_SQL_

    $sth->execute( 
                  $typeInfo->{"id"},
                  $typeInfo->{"description"},
                  $typeInfo->{"defaultType"},
                  $typeInfo->{"itemCategory"},
                  $typeInfo->{"timeOnShelvingCart"}
                  );
    $sth->finish;

}

#===================================================================================================
sub saveItemTypeParam{
    my($dbh,$itemTypId,$typeParam)=@_;
    my $sth=$dbh->prepare(<<_SQL_);
replace into opl_itemTypeParam
set     itemTypeId    = ?,
        userTypeId    = ?,
        loanPeriod    = ?,
        renewalPeriod = ?,
        reservePeriod = ?,
        holdPeriod    = ?,
        gracePeriod   = ?,
        gracePeriodUnit=?,
        maxRenewal    = ?,
        maxItemLoan   = ?
_SQL_

    foreach my $rec (@$typeParam) {
        my $maxRenewal      =$rec->{'maxRenewal'};
        my $maxItemLoan     =$rec->{'maxItemLoan'};
        my $userTypeId      =$rec->{'userTypeId'};
        my $loanPeriod      =getPeriodInHour($rec->{'loanPeriod'});
        my $reservePeriod   =getPeriodInHour($rec->{'reservePeriod'});
        my $holdPeriod      =getPeriodInHour($rec->{'holdPeriod'});
        my $renewalPeriod   =getPeriodInHour($rec->{'renewalPeriod'});

        my $gracePeriod     =getPeriodInHour($rec->{'gracePeriod'});
        my $gracePeriodUnit =($rec->{'gracePeriod'}->{'unit'} eq 'minute')?'minute':'hour';    
        
        $sth->execute($itemTypId,
                     $userTypeId,
                     $loanPeriod,
                     $renewalPeriod,
                     $reservePeriod,
                     $holdPeriod,
                     $gracePeriod,
                     $gracePeriodUnit,
                     $maxRenewal,
                     $maxItemLoan
                     ); 
    }


    $sth->finish;

}
#===================================================================================================
sub getPeriodInHour{
    my($p)=@_;
    my $ret=$p->{'len'};
    $ret= $ret*24 if($p->{'unit'} eq 'day');
    return $ret;
}

#===================================================================================================

sub getItemTypeParam{
 my $itParams;
  if ($ENV{'REQUEST_METHOD'} eq "POST") {
        my $json ="";
        while (<STDIN>) {
            $json .= $_;
        }
         $itParams = decode_json($json);
    }

   return $itParams;

}

#===================================================================================================
