<& /RG/Elements/ToggleSave, no_parent => 1 &>

<script><!--
toggleSave(<% $session{changed} %>);
--></script>

<form name="thisform" method="post" action="/Perm.html">
  <table width="100%" class="poptable">
          <tr> 
            <td height="509" valign="top">
              <table width="100%" border="0" cellspacing="0" cellpadding="0" height="100%">
                <tr> 
                  <td class="tableEmboss" width="57%" valign="top"> 
                    <table width="100%" border="0" cellspacing="3" cellpadding="0" class="poptable">
                      <tr>
                        <td>
                          <table width="451" border="0" cellspacing="0" cellpadding="1" align="center">
                            <tr> 
                              <td><&|/l&>Database</&>
			      </td><td>
                                <input onKeyPress="toggleSave(1)" type="text" name="db" value="<% $session{db} %>" style="width:120px">
                              </td>
                              <td><&|/l&>User Name</&>
			      </td><td>
                                <input onKeyPress="toggleSave(1)" type="text" name="user" value="<% $CF->{user} %>" style="width:120px">
                              </td>
                            </tr>
                            <tr> 
                              <td><&|/l&>Driver</&>
			      </td><td>
                                <select onChange="toggleSave(1)" name="driver" style="width:120px">
    <option value="<% $CF->{driver} %>" SELECTED><% $CF->{driver} %></option>
% foreach my $driver (sort DBI->available_drivers) {
%     next if ($driver eq $CF->{driver});
    <option value="<% $driver %>"><% $driver %></option>
% }
                                </select>
                              </td>
                              <td><&|/l&>Connection Password</&>
			      </td><td>
                                <input onKeyPress="toggleSave(1)" type="text" name="password" value="<% $CF->{password} %>" style="width:120px">
                              </td>
                            </tr>
                            <tr> 
                              <td colspan="4"><&|/l&>Connection String</&>
                                <input onKeyPress="toggleSave(1)" type="text" name="dsn" value="<% $CF->{dsn} %>" style="width:331px">
                              </td>
                            </tr>
                            <tr> 
                              </td>
			      <td align=right>
                              </td>
                            </tr>
                          </table>
		    </td></tr><tr><td bgcolor="#cbeded">
                    <table width="100%" border="0" cellspacing="0" cellpadding="3" class="poptable" bgcolor="#7EBEBE">
                      <tr>
                        <td><&|/l&>Category</&>:
                          <select name="Category" style="width:150px" onchange="thisform.submit()">
% my $count = 0;
% foreach my $category (@categories) {
                            <option value="<% $count %>" <% ($count == $Category) && 'SELECTED' %>><% $category %></option>
%   $count++;
% }
                          </select>
			</td>
                      </tr>
                    </table>
                    <table width="100%" border="0" cellspacing="1" cellpadding="2">
                      <tr> 
                        <td class="tableTitle" width="158"><&|/l&>Table Name</&></td>
                        <td class="tableTitle"><&|/l&>Display Name</&></td>
                      </tr>
                    </table>
		    <div style="OVERFLOW: auto; HEIGHT: 363px; WIDTH:100%" valign=top>
                      <table width="100%" border="0" cellspacing="1" cellpadding="2" style="border-bottom: 2px ridge">
% foreach my $table (@tables) {
%    my $is_chosen	= ($Table eq $table);
%    my $color		= $is_chosen ? '#ffffcc' : '#ffffff';
%    my $overcolor	= $is_chosen ? '#ffffcc' : '#cbeded';
                        <tr onMouseOver="this.style.backgroundColor='<% $overcolor %>'"  style='cursor:hand' onMouseOut="this.style.backgroundColor='<% $color %>'" bgcolor='<% $color %>'> 
                          <td onClick="location.href='?Op=DB&Category=<% $Category %>&Table=<% $table %>'"  height="24" width="158"><% $table %></td>
                          <td height="24"><input type="text" value="<% $CF->{tables}{$table}{description} %>" name="tdesc-<% $table %>" onKeyPress="toggleSave(1)" style="width:100%; margin: 0; spacing: 0; height: 20"></td>
                        </tr>
% }
                      </table>
                    </div>

                          <!-- #BeginLibraryItem "/Library/page.lbi" -->
			</td>
                      </tr>
                    </table>
                  </td>
                  <td width="3%"> 
                    <div align="center"><input type="image" src="/RG/img/ArrowLevel.png" width="13" height="25"></div>
                  </td>
                  <td class="tableEmboss" width="43%" valign="<% $Table ? 'top' : 'middle' %>" bgcolor="#cbeded"> 
% if ($Table) {
                    <table width="100%" border="0" cellspacing="1" cellpadding="2">
                      <tr valign=top> 
                        <td class="tableTitle" colspan="2"> 
                          <div align="left"><&|/l&>Table Name</&>: <% $Table %></div>
                        </td>
                      </tr>
                      <tr> 
                        <td class="tableTitle" width="158"><&|/l&>Field Name</&></td>
                        <td class="tableTitle"><&|/l&>Display Name</&></td>
                      </tr>
                    </table> 
		    <div style="OVERFLOW: auto; HEIGHT: 465; WIDTH:100%" >
                      <table width="100%" border="0" cellspacing="1" cellpadding="2" style="border-bottom: 2px ridge">
% foreach my $field (sort map lc, keys %{$CF->{tables}{$Table}{fields}}) {
                        <tr bgcolor=#ffffff > 
                          <td height="24" width="158"><% $field %></td>
                          <td height="24"><input type="text" name="fdesc-<% $Table %>-<% $field %>" value="<% $CF->{tables}{$Table}{fields}{$field}{description} %>" style="width:100%; margin: 0; spacing: 0; height: 20" onKeyPress="toggleSave(1)"></td>
                        </tr>
% }
                      </table>
                    </div>
                  </td>
% } else {
    <p align=center>
    <&|/l&>Please select a table on the left side to modify.</&>
% }
                </tr>
              </table>
      </td>
    </tr>
  </table>
</form>
 
<map name="Map2"> 
  <area shape="rect" coords="109,3,200,18" href="/Perm/Group.html" target="_top">
</map>
</body>
</html>

<%INIT>
require DBI;
our $Conf; # XXX - fold $Conf back to RT::*
my $dbh = $session{CurrentUser}->UserObj->dbh;
my $CF = $Conf->{DB}{$session{db}};

#foreach my $key (qw(user driver password dsn)) {
#    $CF->{$key} = $ARGS{$key} if defined $ARGS{$key};
#}

foreach my $key (keys %ARGS) {
    if ($key =~ /^tdesc-(.*)$/) {
	$CF->{tables}{$1}{description} = $ARGS{$key};
    }
    elsif ($key =~ /^fdesc-(.*)-(.*)$/) {
	$CF->{tables}{$1}{fields}{$2}{description} = $ARGS{$key};
    }
}

$Conf->{DB} = { %{$Conf->{DB}} };

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>
$Table	    => undef
$Category   => 0
</%ARGS>
