#!/usr/bin/perl

#use utf8;
use strict;
use CGI;

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


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

my $cgi = CGI->new;
my $input = $cgi->Vars();
my $op = $input->{'op'};
my ($permission, $cookie, $template) = tmpl_read(
        {
            dbh             => $dbh,
            cgi             => $cgi,
            tmplFile        => 'util/brwrtype.tmpl',
            reqPermission   => 'pref_edit',
        }
);

($op) || ($op = 'view');

if ($op eq 'view' || $op eq 'cancel') {
    ListUsers($dbh, $template);    
    $template->param(ViewUser => 1);
}
elsif ($op eq 'edit') {
    my $query = $dbh->prepare("select * from opl_category where catid=?");
    $query->execute($input->{'catid'});
    my $rec = $query->fetchrow_hashref;
    $query->finish;
    $template->param($rec);
    $template->param(EditUser => 1);
}

if ($op eq 'delete') {
    my $total = $dbh->selectrow_array("select count(*) from opl_user where categorycode=" . $input->{'catid'});

    if ($total != 0) {
        $template->param(UserInUse => 1);
    }
    else {
        $dbh->do("delete from opl_category where catid=" . $input->{'catid'});
        $dbh->do("delete from opl_itemTypeParam where userTypeId = " . $input->{'catid'});
        $template->param(DelSuccess => 1);
    }

    ListUsers($dbh, $template);    
    $template->param(ViewUser => 1);
}
elsif ($op eq 'update') {
    my $sql = <<_SQL_;
update  opl_category
set     catname         = ?,
        maxloans        = ?,
        maxreserv       = ?
where   catid           = ?
_SQL_
    my @bindVal = (
        $input->{'catname'}, 
        ($input->{'maxloans'})  ? $input->{'maxloans'}  : 0,
        ($input->{'maxreserv'}) ? $input->{'maxreserv'} : 0,
        $input->{'catid'}
    );
    $dbh->do($sql, undef, @bindVal);
    
    ListUsers($dbh, $template);    
    $template->param(ViewUser => 1);
}
elsif ($op eq 'add') {
    my $sql = <<_SQL_;
insert into opl_category
set     catname         = ?,
        maxloans        = ?,
        maxreserv       = ?
_SQL_
    my @bindVal = (
        $input->{'catname'}, 
        ($input->{'maxloans'})  ? $input->{'maxloans'}  : 0,
        ($input->{'maxreserv'}) ? $input->{'maxreserv'} : 0,
    );
    
    $dbh->do($sql, undef, @bindVal);

    addItemTypeParam($dbh, $input->{'catname'});
    ListUsers($dbh, $template);    
    $template->param(ViewUser => 1);
}
elsif ($op eq 'new') {
    $template->param(AddUser => 1);
}

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


sub ListUsers {
    my ($dbh, $template) = @_;
    
    my $queryTotal = $dbh->prepare("select count(*) from opl_user where categorycode=?");
    my $query = $dbh->prepare("select * from opl_category order by catid");
    $query->execute();
    my @arrUser = ();
    my $count = 0;
    while (my $rec = $query->fetchrow_hashref) {
        $queryTotal->execute($rec->{'catid'});
        my ($totaluser) = $queryTotal->fetchrow_array;
        $queryTotal->finish;

        $rec->{'totaluser'} = $totaluser;
        $rec->{'odd'} = ($count%2 ? 1:0);
        if ( $rec->{'catid'} == 1 ) { $rec->{'default'} = 1; }
        push @arrUser, $rec;
        $count++;
    }
    $query->finish;
    $template->param(listuser => \@arrUser);
}
########################################


sub addItemTypeParam {
    my ($dbh, $userType) = @_;

    my @bindVal = ($userType);
    my ($userTypeId) = $dbh->selectrow_array("select catid from opl_category where catname = ?", undef, @bindVal);

    if (!$userTypeId) {
        return;
    }

    my $sth = $dbh->prepare(<<_SQL_);
select  itemTypeId
from    opl_itemTypeParam
group by itemTypeId
_SQL_

    my $sth_addItemTypeParam = $dbh->prepare(<<_SQL_);
insert into opl_itemTypeParam
set     itemTypeId    = ?,
        userTypeId    = ?,
        loanPeriod    = 0,
        renewalPeriod = 0,
        reservePeriod = 0,
        holdPeriod    = 0,
        gracePeriod   = 0,
        maxRenewal    = 0,
        fine          = 0
_SQL_

    my $rv = $sth->execute() || return;
    while (my ($itemTypeId) = $sth->fetchrow_array) {
        $rv = $sth_addItemTypeParam->execute($itemTypeId, $userTypeId) || return;
    }
    $sth_addItemTypeParam->finish;
    $sth->finish;

    return $rv;
}
