#!/usr/local/bin/perl
# table_form.cgi
# Display a form for creating a table

require './mysql-lib.pl';
&ReadParse();
&can_edit_db($in{'db'}) || &error($text{'dbase_ecannot'});
$access{'edonly'} && &error($text{'dbase_ecannot'});
&header($text{'table_title2'}, "", "table_form");
print "<hr>\n";

print "<form action=create_table.cgi>\n";
print "<input type=hidden name=db value='$in{'db'}'>\n";
print "<table border>\n";
print "<tr $tb> <td><b>$text{'table_header2'}</b></td> </tr>\n";
print "<tr $cb> <td><table>\n";

print "<tr> <td><b>$text{'table_name'}</b></td>\n";
print "<td><input name=name size=30></td> </tr>\n";

print "<tr> <td><b>$text{'table_copy'}</b></td>\n";
print "<td><select name=copy>\n";
print "<option value='' checked>$text{'table_copynone'}\n";
foreach $d (&list_databases()) {
	next if (!&can_edit_db($d));
	foreach $t (&list_tables($d)) {
		print "<option>$d.$t\n";
		}
	}
print "</select></td> </tr>\n";

print "<tr> <td><b>$text{'table_type'}</b></td>\n";
print "<td><select name=type>\n";
foreach $t ('', 'isam', 'myisam', 'heap', 'merge', 'innodb') {
	printf "<option value='%s'> %s\n", $t, $text{"table_type_$t"};
	}
print "</select></td> </tr>\n";

print "<tr> <td valign=top><b>$text{'table_initial'}</b></td>\n";
print "<td><table border>\n";
print "<tr $tb> <td><b>$text{'field_name'}</b></td> ",
      "<td><b>$text{'field_type'}</b></td> ",
      "<td><b>$text{'field_size'}</b></td> ",
      "<td><b>$text{'table_nkey'}</b></td> ",
      "<td><b>$text{'field_auto'}</b></td> </tr>\n";
for($i=0; $i<(int($in{'fields'}) || 4); $i++) {
	print "<tr $cb>\n";
	print "<td><input name=field_$i size=20></td>\n";
	print "<td><select name=type_$i>\n";
	print "<option selected>\n";
	foreach $t (@type_list) {
		print "<option>$t\n";
		}
	print "</select></td>\n";
	print "<td><input name=size_$i size=10></td>\n";
	print "<td><input type=checkbox name=key_$i value=1> ",
	      "$text{'yes'}</td>\n";
	print "<td><input type=checkbox name=auto_$i value=1> ",
	      "$text{'yes'}</td>\n";
	print "</tr>\n";
	}
print "</table></td> </tr>\n";

print "<tr> <td colspan=2 align=right><input type=submit ",
      "value='$text{'create'}'></td> </tr>\n";

print "</table></td></tr></table></form>\n";

print "<hr>\n";
&footer("edit_dbase.cgi?db=$in{'db'}", $text{'dbase_return'},
	"", $text{'index_return'});

