#!/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 $op = lc($input->{'op'});
my ($permission, $cookieList, $template) = tmpl_read(
    {
        dbh             => $dbh,
        cgi             => $cgi,
        tmplFile        => 'util/zservers.tmpl',
        reqPermission   => 'pref_edit',
    }
);

# See User.pm for the list of permissions
#if ($permission && $permission->{'reqPermission1'}) {
my $op_result;
if ($op eq 'add') {
    $op = 'edit';
    $template->param(
        zid => -1,
    );
}
elsif ($op eq 'edit') {
    my $zdb_param = get_zdb_param($dbh, $input->{'zid'});
    $template->param($zdb_param);
}
else {
    if ($op eq 'save') {
        $op_result = save_zdb($dbh, $input);
    }
    elsif ($op eq 'delete') {
        $op_result = delete_zdb($dbh, $input);
    }
    $op = 'list';
    my @zdbList = list_zDatabase($dbh);
    $template->param(
        zdbList => \@zdbList,
    );
}

$template->param(
    'op_'.$op => 1,
    $op.'_ok'    => ($op =~ m/^(save|delete)$/ && $op_result),
);

#}
#else { # input for login form
#    $template->param(
#        input => [
#            {name => '', value => $,},
#            {name => '', value => $,},
#        ],
#    );
#}

# Turn on/off search box
#$template->param(
#    search_off      => 1,
#);
$template->param(hlpUrl     => Opals::Constant->getHlpUrl('zservers') );

tmpl_write($dbh, $cgi, $cookieList, $template);
#$dbh->disconnect();
################################################################################

sub list_zDatabase {
    my ($dbh) = @_;

    my $sth = $dbh->prepare(<<_SQL_);
select  *
from    opl_zDatabase
order by zid asc
_SQL_

    my @zDatabase;
    $sth->execute();
    while (my $zdb = $sth->fetchrow_hashref) {
        $zdb->{'encoding_'.$zdb->{'encoding'}} = 1;
        push @zDatabase, $zdb;
    }
    $sth->finish;

    return @zDatabase;
}
########################################

sub get_zdb_param {
    my ($dbh, $zid) = @_;

    my $sql = <<_SQL_;
select  *
from    opl_zDatabase
where   zid = ?
_SQL_

    my @value = ($zid);
    my $zdb_param = $dbh->selectrow_hashref($sql, undef, @value) || return;

    $zdb_param->{'syntax_'      .$zdb_param->{'syntax'}}    = 1;
    $zdb_param->{'encoding_'    .$zdb_param->{'encoding'}}  = 1;
    $zdb_param->{'state_'       .$zdb_param->{'state'}}     = 1;
    $zdb_param->{'stateList_'   .$zdb_param->{'stateList'}} = 1;
    $zdb_param->{'circData_'    .$zdb_param->{'circData'}} = 1;
    $zdb_param->{'studentILL_'  .$zdb_param->{'studentILL'}} = 1;

    return $zdb_param;
}
########################################

sub save_zdb {
    my ($dbh, $in) = @_;
    my $sql;
    my @value;

    my $op = ($in->{'zid'} == -1) ? 'insert into' : 'update';

    my $sql = "$op opl_zDatabase set ";
    foreach my $col qw(host port name syntax encoding username password
            description state stateList filter circData studentILL locationId) {
        # check password
        if (   ($col eq 'password' && !$in->{$col})
            || ($in->{'zid'} == 0 && $col !~ m/^(description|state|stateList)$/)
           ) {
            next;
        }
        $sql .= "$col = ?,";
        push @value, $in->{$col};
    }
    $sql =~ s/,$//; # remove the last comma
    if ($op eq 'update') {
        $sql .= " where zid = $in->{'zid'}";
    }

    return $dbh->do($sql, undef, @value);
}
########################################

sub delete_zdb {
    my ($dbh, $in) = @_;
    if (!$in->{'zid'}) {
        return;
    }

    return $dbh->do("delete from opl_zDatabase where zid = $in->{'zid'}");
}
