%# New table display.
<%PERL>
if ($ARGS{"$id-Commit"} and %{$tables}) {
    # yes! let's rock and roll!
    require RG::Table;
    my $table = RG::Table->new({
	DBH	=> $dbh,
	TABLE => [ sort keys %{$tables} ],
    });
    $table->_init;

    # $part = bless({}, 'RG::Parser');
    my $src = bless(\( $table->serialize(0) ), '_Fake::SRC');
    my $this = {
	format  => {
	    body	=> [],
	    head	=> [],
	},
	table	=> $src,
	border  => {
	    vertical    => [],
	    horizontal  => [],
	},
    };
    $this->{src} = $src if $type eq 'TABLE';
    %{$part} = (
	attr => { ($type eq 'GRAPH') ? (
	    title	    => $part->{attr}{_title},
	    accentclr	    => 'black',
	    encoding	    => 'utf-8',
	    height	    => '300',
	    title_font	    => 'kai',
	    title_fontsize  => '16',
	    width	    => '300',
	    type	    => 'bars',
	    style	    => 'bar',
	) : (
	    descr	    => '',
	) },
	this => $this,
	text => [ ($type eq 'TABLE') ? (
	    bless({
		attr => {},
		text => [ $part->{attr}{_title} ],
		type => 'CAPTION',
		this => $this,
	    }, 'RG::Parser'),
	    map bless({
		attr => {},
		text => [
		    bless({
			attr => {},
			text => [],
			type => 'TR',
			this => $this,
		    }, 'RG::Parser'),
		],
		type => $_,
		this => $this,
	    }, 'RG::Parser'), qw(THEAD TBODY),
	) : ( '' ) ],
	type => $type,
    );

    $m->print('Skidoo');
    return;
} else {
    my $CF = $Conf->{DB}{$session{db}};
</%PERL>

<table width=100% border=1 cellpadding=2 bgcolor="#c9c9c9">
<tr><th style="background: #7cbebe; color: black"><&|/l, loc($type) &>New [_1]</&></th></tr>
<tr valign=bottom><td align=center>
<&|/l&>title</&>: <input type="text" name="<% $id %>-Caption" value="<% $part->{attr}{_title} %>" size=30>
</td></tr>
<tr><td align=center><table>
<tr><td>
<&|/l&>Available Database Tables:</&>
</td><td width="90" height="2">&nbsp;</td><td>
<&|/l&>Included Database Tables:</&>
</td></tr><tr><td>
<select name="Category" style="width:300px; height:30px" onchange="form.submit()">
% my $count = 0;
% foreach my $category (@categories) {
<option value="<% $count %>" <% ($count == $Category) && 'SELECTED' %>><% $category %></option>
%   $count++;
% }
</select><BR>
<select name="source" size="3" multiple style="width:300px; height: 80px">
% foreach my $table (@tables) {
%    next if $tables->{$table};
%    my $descr = $CF->{tables}{$table}{description} or next;
<option value="<% $table %>"><% $descr %></option>
% }
</select>
</td><td valign=middle align=center>
<input type="submit" name="<% $id %>-Add" value="<&|/l&><< Add</&>"><br>
<input type="submit" name="<% $id %>-Remove" value="<&|/l&>Remove >></&>"><br>
<input type="submit" name="<% $id %>-RemoveAll" value="<&|/l&>Remove All >></&>">
</td><td>
<select name="target" size="5" multiple style="width:300px; height: 110px">
% foreach my $table (sort keys %{$tables}) {
%    my $descr = $CF->{tables}{$table}{description} or next;
<option value="<% $table %>"><% $descr %></option>
% }
</select>
</td></tr>
</table></td></tr>
<tr><td colspan=3 align=center>
<input type="submit" name="<% $id %>-Commit" value=" <&|/l&>Change</&> ">
<input type="button" onclick="location.href='/RG/Report/View.html?View=Edit&Op=Delete'" value=" <&|/l&>Cancel</&> ">
</td></tr>
</table>
% }
<%INIT>
my $id = $part->{id};
my $tables = $part->{attr}{_tables} ||= {};
if ($ARGS{"$id-Add"}) {
    $tables->{$_}++ foreach @source;
}
if ($ARGS{"$id-Remove"}) {
    delete $tables->{$_} foreach @target;
}
if ($ARGS{"$id-RemoveAll"}) {
    %{$tables} = ();
}
if (exists $ARGS{"$id-Caption"}) {
    $part->{attr}{_title} = $ARGS{"$id-Caption"};
}

my @categories = sort @{$dbh->selectcol_arrayref(
    "select distinct FUNCTION_NAME from hrms_tables"
) || []};

my @tables = sort map lc, @{$dbh->selectcol_arrayref(
    "select TABLE_NAME from hrms_tables where FUNCTION_NAME = " .
    $dbh->quote($categories[$Category])
) || []};

@categories = map {
    Encode::decode('big5', $_)
} @categories;

</%INIT>
<%ARGS>
$part
$dbh
$type	=> 'TABLE'
@source	=> []
@target => []
$Category   => 0
</%ARGS>
