#!/usr/bin/perl

#use utf8;
use strict;
use CGI;

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


my $dbh = Opals::Context->dbh();
END { $dbh->disconnect(); }

my $cgi = CGI->new;
my $input = $cgi->Vars();

my ($permission, $cookie, $template) = tmpl_read(
        {
            dbh             => $dbh,
            cgi             => $cgi,
            tmplFile        => 'util/itemloancat.tmpl',
            reqPermission   => 'pref_edit',
        }
);

if ( !$input->{'op'} ) { $input->{'op'} = 'view'; }

if ( $input->{'op'} eq 'view' || $input->{'op'} eq 'cancel' )
{
    ListItems($dbh, $template);    
    $template->param(ViewItem => 1);
}

elsif ( $input->{'op'} eq 'edit' )
{
    my $query = $dbh->prepare("select * from opl_typeCirc where id=?");
    $query->execute($input->{'id'});
    my $rec = $query->fetchrow_hashref;
    $query->finish;
    $template->param($rec);
    $template->param(EditItem => 1);
}

if ( $input->{'op'} eq 'delete' )
{
    if ( $input->{'id'} != 1 )
    {
        my $total = $dbh->selectrow_array("select count(*) from opl_item where typeCircId=" . $input->{'id'});
        if ( $total != 0 ) 
            { $template->param(ItemInUse => 1); }
        else
        {
            $dbh->do("delete from opl_typeCirc where id=" . $input->{'id'});
            $template->param(DelSuccess => 1);
        }
    }
    else { $template->param(DefaultRecord => 1); }

    ListItems($dbh, $template);    
    $template->param(ViewItem => 1);
}

elsif ( $input->{'op'} eq 'update' )
{
    my $sql = "update opl_typeCirc set ";
    $sql .= "code='" . $input->{'code'} . "'";
    $sql .= ", description='" . $input->{'description'} . "'";

    $input->{'gracePeriod'} = 0 if ( !$input->{'gracePeriod'} );
    $sql .= ", gracePeriod=" . $input->{'gracePeriod'};

    $input->{'loanPeriod'} = 0 if ( !$input->{'loanPeriod'} );
    $sql .= ", loanPeriod=" . $input->{'loanPeriod'};
    
    $input->{'renewalPeriod'} = 0 if ( !$input->{'renewalPeriod'} );
    $sql .= ", renewalPeriod=" . $input->{'renewalPeriod'};
    
    $input->{'reservePeriod'} = 0 if ( !$input->{'reservePeriod'} );
    $sql .= ", reservePeriod=" . $input->{'reservePeriod'};
    
    $input->{'holdPeriod'} = 0 if ( !$input->{'holdPeriod'} );
    $sql .= ", holdPeriod=" . $input->{'holdPeriod'};
    
    $input->{'maxRenewal'} = 0 if ( !$input->{'maxRenewal'} );
    $sql .= ", maxRenewal=" . $input->{'maxRenewal'};
    
    $input->{'restricted'} = 0 if ( !$input->{'restricted'} );
    $sql .= ", restricted=" . $input->{'restricted'};
    
    $input->{'fine'} = 0 if ( !$input->{'fine'} );
    $sql .= ", fine=" . $input->{'fine'};
    
    $sql .= " where id=" . $input->{'id'};
    $dbh->do($sql);
    
    ListItems($dbh, $template);    
    $template->param(ViewItem => 1);
}

elsif ( $input->{'op'} eq 'add' )
{
    my $sql = "insert into opl_typeCirc set ";
    $sql .= "code='" . $input->{'code'} . "'";
    $sql .= ", description='" . $input->{'description'} . "'";
    
    $input->{'gracePeriod'} = 0 if ( !$input->{'gracePeriod'} );
    $sql .= ", gracePeriod=" . $input->{'gracePeriod'};
    
    $input->{'loanPeriod'} = 0 if ( !$input->{'loanPeriod'} );
    $sql .= ", loanPeriod=" . $input->{'loanPeriod'};
    
    $input->{'renewalPeriod'} = 0 if ( !$input->{'renewalPeriod'} );
    $sql .= ", renewalPeriod=" . $input->{'renewalPeriod'};
    
    $input->{'reservePeriod'} = 0 if ( !$input->{'reservePeriod'} );
    $sql .= ", reservePeriod=" . $input->{'reservePeriod'};
    
    $input->{'holdPeriod'} = 0 if ( !$input->{'holdPeriod'} );
    $sql .= ", holdPeriod=" . $input->{'holdPeriod'};
    
    $input->{'maxRenewal'} = 0 if ( !$input->{'maxRenewal'} );
    $sql .= ", maxRenewal=" . $input->{'maxRenewal'};
    
    $input->{'restricted'} = 0 if ( !$input->{'restricted'} );
    $sql .= ", restricted=" . $input->{'restricted'};
    
    $input->{'fine'} = 0 if ( !$input->{'fine'} );
    $sql .= ", fine=" . $input->{'fine'};
    
    $dbh->do($sql);
    ListItems($dbh, $template);    
    $template->param(ViewItem => 1);
}

elsif ( $input->{'op'} eq 'new' )
{
    $template->param(AddItem => 1);
}

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

#----------------------------------------------------------
sub ListItems
{
    my ($dbh, $template) = @_;
    
    my $queryTotal = $dbh->prepare("select count(*) from opl_item where typeCircId=?");
    my $query = $dbh->prepare("select * from opl_typeCirc order by id");
    $query->execute();
    my @arrType = ();
    my $count = 0;
    while ( my $rec = $query->fetchrow_hashref )
    {
        $queryTotal->execute($rec->{'id'});
        my ($totalitem) = $queryTotal->fetchrow_array;
        $queryTotal->finish;

        $rec->{'totalitem'} = $totalitem;
        $rec->{'odd'} = ($count%2 ? 1:0);
        if ( $rec->{'id'} == 1 ) { $rec->{'default'} = 1; }
        push @arrType, $rec;
        $count++;
    }
    $query->finish;
    $template->param(listtype => \@arrType);
}

__END_OF_FILE:

