# (C) 2001 Christian Jacobi
# (C) 2001 Christoph Berg
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# cb 010805
$TET_WINDOW_VERSION = '$Id: tet_window.pm,v 1.10 2002/07/29 12:49:04 tesh Exp $';

use Tk;
use Tk::NoteBook;
use Tk::ErrorDialog;


## Hilfsfunktionen ##

sub place_label {
	my ($win, $label, $row) = @_;
	$win->Label(-text => $label,
			-justify => 'left')
		->grid(-column => 0, -row => $row, -sticky => 'w');
}


sub place_text {
	my ($win, $var, $row, %opt) = @_;
	if(exists $opt{'-column'}) {
		$column = $opt{'-column'};
		$columnspan = 1;
		delete $opt{'-column'};
	} else {
		$column = 1;
		$columnspan = 2;
	}
	$opt{'-state'} ||= "normal";
	$win->Entry(-textvariable => $var,
			-justify => 'left',
			-state => $opt{'-state'},
			-show => $opt{'-show'})
		->grid(-column => $column, -row => $row,
			-columnspan => $columnspan, -sticky => 'w');
}


## Init-Kram ##

sub init_info_window {
	my $win = shift;
	my $row = 0;

	place_label($win, 'Nickname', $row);
	place_text($win, \$inp_nickname, $row);
	$win->Button(-text => 'Aktualisieren',
		-command => \&info)->grid(-row => $row++, -column => 2);
	place_label($win, 'Name', $row);
	place_text($win, \$inp_vorname, $row, -column => 1);
	place_text($win, \$inp_nachname, $row++, -column => 2);
	place_label($win, 'Kid', $row);
	place_text($win, \$inp_kid, $row++, -state => "disabled");
	place_label($win, 'Email',$row);
	place_text($win, \$inp_email, $row);
	$win->Button(-text => 'Änderungen speichern',
		-command => \&info_change_commit)->grid(-row => $row++, -column => 2);
	place_label($win, 'Paßwort', $row);
	place_text($win, \$inp_pw, $row++, -column => 1, -state => "disabled");
	place_label($win, 'Created', $row);
	place_text($win, \$inp_created, $row++, -state => "disabled");
	place_label($win, 'Last Contact', $row);
	place_text($win, \$inp_last_contact, $row++, -state => "disabled");
	place_label($win, 'Locked', $row);
	place_text($win, \$inp_locked, $row++);
	place_label($win, 'Guthaben', $row);
	place_text($win, \$inp_guthaben_euro, $row, -column => 1, -state => "disabled");
	place_text($win, \$inp_guthaben_dm, $row++, -column => 2, -state => "disabled");
	place_label($win, 'Bemerkung', $row);
	place_text($win, \$inp_bemerkung, $row++);
}

sub init_user_window {
	my $win = shift;

	my $row = 0;
	place_label($win, 'Nickname', $row);
	place_text($win, \$inp_nickname, $row, -column => 1);
	$win->Button(-text => 'Create User',
		-command => \&create_user)->grid(-row => $row++, -column => 2);
	place_label($win, 'Name', $row);
	place_text($win, \$inp_vorname, $row, -column => 1);
	place_text($win, \$inp_nachname, $row++, -column => 2);
	place_label($win, 'Email', $row);
	place_text($win, \$inp_email, $row++, -column => 1);
	#$win->Button(-text => 'Delete User',
	#	-command => \&delete_user)->grid(-row => $row++, -column => 2);
	place_label($win, 'Paßwort', $row);
	place_text($win, \$inp_pw1, $row, -column => 1, -show => '*');
	$win->Button(-text => 'Paßwort ändern',
		-command => \&change_pw)->grid(-row => $row++, -column => 2);
	place_text($win, \$inp_pw2, $row++, -column => 1, -show => '*');
}

sub euro_5 { $inp_betreff = "Einzahlung"; $inp_betrag = "5 EUR"; }
sub euro_10 { $inp_betreff = "Einzahlung"; $inp_betrag = "10 EUR"; }
sub euro_20 { $inp_betreff = "Einzahlung"; $inp_betrag = "20 EUR"; }

sub minus_euro_5 { $inp_betreff = "Abbuchung"; $inp_betrag = "5 EUR"; }
sub minus_euro_10 { $inp_betreff = "Abbuchung"; $inp_betrag = "10 EUR"; }
sub minus_euro_20 { $inp_betreff = "Abbuchung"; $inp_betrag = "20 EUR"; }

sub ueber_euro_5 { $inp_betreff = "Überweisung"; $inp_betrag = "5 EUR"; }
sub ueber_euro_10 { $inp_betreff = "Überweisung"; $inp_betrag = "10 EUR"; }
sub ueber_euro_20 { $inp_betreff = "Überweisung"; $inp_betrag = "20 EUR"; }

sub init_einzahlung_window {
	my $win = shift;

	my $row = 0;
	place_label($win, 'Nickname', $row);
	place_text($win, \$inp_nickname, $row);
	$win->Button(-text => 'Aktualisieren',
		-command => \&info)->grid(-row => $row++, -column => 2);
	place_label($win, 'Name', $row);
	place_text($win, \$inp_name, $row++, -state => "disabled");
	place_label($win, 'Guthaben', $row);
	place_text($win, \$inp_guthaben_euro, $row, -column => 1, -state => "disabled");
	place_text($win, \$inp_guthaben_dm, $row++, -column => 2, -state => "disabled");
	place_label($win, 'Betreff', $row);
	place_text($win, \$inp_betreff, $row++);
	place_label($win, 'Betrag', $row);
	place_text($win, \$inp_betrag, $row);
	$win->Button(-text => 'Einzahlen',
		-command => \&einzahlen)->grid(-row => $row++, -column => 2);
	$win->Button(-text => '5 Euro',
		-command => \&euro_5)->grid(-row => $row, -column => 0);
	$win->Button(-text => '10 Euro',
		-command => \&euro_10)->grid(-row => $row, -column => 1);
	$win->Button(-text => '20 Euro',
		-command => \&euro_20)->grid(-row => $row++, -column => 2);
}

sub einzahlung_raise {
	info();
	if($inp_betreff eq "" or $inp_betreff eq "Abbuchung" or $inp_betreff eq "Überweisung") {
		$inp_betreff = "Einzahlung";
	}
}

sub init_abbuchung_window {
	my $win = shift;

	my $row = 0;
	place_label($win, 'Nickname', $row);
	place_text($win, \$inp_nickname, $row);
	$win->Button(-text => 'Aktualisieren',
		-command => \&info)->grid(-row => $row++, -column => 2);
	place_label($win, 'Name', $row);
	place_text($win, \$inp_name, $row++, -state => "disabled");
	place_label($win, 'Guthaben', $row);
	place_text($win, \$inp_guthaben_euro, $row, -column => 1, -state => "disabled");
	place_text($win, \$inp_guthaben_dm, $row++, -column => 2, -state => "disabled");
	place_label($win, 'Betreff', $row);
	place_text($win, \$inp_betreff, $row++);
	place_label($win, 'Betrag', $row);
	place_text($win, \$inp_betrag, $row);
	$win->Button(-text => 'Abbuchen',
		-command => \&abbuchen)->grid(-row => $row++, -column => 2);
	$win->Button(-text => '5 Euro',
		-command => \&minus_euro_5)->grid(-row => $row, -column => 0);
	$win->Button(-text => '10 Euro',
		-command => \&minus_euro_10)->grid(-row => $row, -column => 1);
	$win->Button(-text => '20 Euro',
		-command => \&minus_euro_20)->grid(-row => $row++, -column => 2);
}

sub abbuchung_raise {
	info();
	if($inp_betreff eq "" or $inp_betreff eq "Einzahlung" or $inp_betreff eq "Überweisung") {
		$inp_betreff = "Abbuchung";
	}
}

sub init_ueberweisung_window {
	my $win = shift;

	my $row = 0;
	place_label($win, 'Nickname', $row);
	place_text($win, \$inp_nickname, $row);
	$win->Button(-text => 'Aktualisieren',
		-command => \&ueberweisung_info)->grid(-row => $row++, -column => 2);
	place_label($win, 'Name', $row);
	place_text($win, \$inp_name, $row++, -state => "disabled");
	place_label($win, 'Guthaben', $row);
	place_text($win, \$inp_guthaben_euro, $row, -column => 1, -state => "disabled");
	place_text($win, \$inp_guthaben_dm, $row++, -column => 2, -state => "disabled");
	place_label($win, 'Betreff', $row);
	place_text($win, \$inp_betreff, $row);
	$win->Button(-text => 'Tauschen',
		-command => \&tauschen)->grid(-row => $row++, -column => 2);
	place_label($win, 'Betrag', $row);
	place_text($win, \$inp_betrag, $row++);

	place_label($win, 'Überweisung an', $row);
	place_text($win, \$inp_an_nickname, $row);
	$win->Button(-text => 'Überweisen',
		-command => \&ueberweisen)->grid(-row => $row++, -column => 2);
	place_label($win, 'Name', $row);
	place_text($win, \$inp_an_name, $row++, -state => "disabled");
	place_label($win, 'Guthaben', $row);
	place_text($win, \$inp_an_guthaben_euro, $row, -column => 1, -state => "disabled");
	place_text($win, \$inp_an_guthaben_dm, $row++, -column => 2, -state => "disabled");

	$win->Button(-text => '5 Euro',
		-command => \&ueber_euro_5)->grid(-row => $row, -column => 0);
	$win->Button(-text => '10 Euro',
		-command => \&ueber_euro_10)->grid(-row => $row, -column => 1);
	$win->Button(-text => '20 Euro',
		-command => \&ueber_euro_20)->grid(-row => $row++, -column => 2);
}

sub ueberweisung_raise {
	ueberweisung_info();
	if($inp_betreff eq "" or $inp_betreff eq "Einzahlung" or $inp_betreff eq "Abbuchung") {
		$inp_betreff = "Überweisung";
	}
}

sub exec_pgaccess {
	system "pgaccess &";
}

sub init_misc_window {
	my $win = shift;

	$win->Button(-text => 'SQL Window',
		-command => \&init_sql_window)->grid(-row => 0, -column => 2);
	$win->Button(-text => 'SQL Trace Window',
		-command => \&init_sql_trace_window)->grid(-row => 1, -column => 2);
	$win->Button(-text => 'pgaccess',
		-command => \&exec_pgaccess)->grid(-row => 2, -column => 2);
	$win->Button(-text => 'Restart TET',
		-command => \&restart_tet)->grid(-row => 3, -column => 2);
	$win->Button(-text => 'Exit',
		-command => \&exit_tet)->grid(-row => 4, -column => 2);
	$win->Label(-text => 'PGHOST')->grid(-row => 5, -column => 1);
	$win->Label(-textvariable => \$PGHOST)->grid(-row => 5, -column => 2);
}

sub init_sql_trace_window {
	return if $sql_trace_win;

	$sql_trace_win = MainWindow->new;
	$sql_trace_win->title("TET SQL Trace");

	$sql_trace_win->Label(-textvariable => \$query_queue,
			-justify => 'left',
			-wraplength => 800)
		->pack;
}

sub init_sql_window {
	return if $sql_win;

	$sql_win = MainWindow->new;
	$sql_win->title("TET SQL");
	$sql_topframe = $sql_win->Frame->pack(-side => 'top');
	$sql_topframe->Entry(-textvariable => \$inp_sql_query)->pack(-side => 'left');
	$sql_topframe->Button(-text => 'Execute',
			-command => \&sql_exec) -> pack(-side => 'left');
	$sql_win->Label(-textvariable => \$sql_output,
			-justify => 'left',
			-wraplength => 800)->pack;
}

sub increase_limit {
	$inp_limit += 10;
	buchungen();
}

sub init_buchungen_window {
	my $win = shift;

	my $row = 0;
	place_label($win, 'Nickname', $row);
	place_text($win, \$inp_nickname, $row, -column => 1);
	$win->Button(-text => 'Buchungen',
		-command => \&buchungen)->grid(-row => $row++, -column => 2);
	place_label($win, 'Einträge', $row);
	place_text($win, \$inp_limit, $row, -column => 1);
	$win->Button(-text => 'Mehr',
		-command => \&increase_limit)->grid(-row => $row++, -column => 2);

	$win->Label(-textvariable => \$buchungen_output,
			-justify => 'left',
			-wraplength => 800)
		->grid(-row => $row++, -column => 0, -columnspan => 3);
	$inp_buchungen_output = "Buchungen";
}

sub init_summe_window {
	my $win = shift;

	my $row = 0;
	place_label($win, 'Interval', $row);
	place_text($win, \$inp_timeinterval, $row, -column => 1);
	$win->Button(-text => 'Summe',
		-command => \&summe)->grid(-row => $row++, -column => 2);

	$win->Label(-textvariable => \$summe_output,
			-justify => 'left',
			-wraplength => 800)
		->grid(-row => $row++, -column => 0, -columnspan => 3);
	$summe_output = "summe";
}

sub init_schuldner_window {
	my $win = shift;

	my $row = 0;
	place_label($win, 'Grenze', $row);
	place_text($win, \$inp_grenze, $row, -column => 1);
	$win->Button(-text => 'Schuldner',
		-command => \&schuldner)->grid(-row => $row++, -column => 2);

	$win->Label(-textvariable => \$schuldner_output,
			-justify => 'left',
			-wraplength => 800)
		->grid(-row => $row++, -column => 0, -columnspan => 3);
	$schuldner_output = "schuldner";
}

sub init_menu {
	my $win = shift;

	$menu = $win->Menu(-title => "Datei");
	$menu->add("command");
}

sub clear_pw {
	$inp_pw1 = "";
	$inp_pw2 = "";
}

sub init_window {
	$win = MainWindow->new;
	$win->title($NAME);

	#$topframe = $win->Frame->pack(-side => 'top');
	#$topframe->Entry(-textvariable => \$kunde)->pack(-side => 'left');
	#$topframe->Button(-text => 'Info',
			#-command => \&info) -> pack(-side => 'left');

	init_menu($win);

	$notebook = $win->NoteBook->pack(-side => 'top');

	$user_window = $notebook->add("users",
			-label => "Users", -raisecmd => \&clear_pw);
	init_user_window($user_window);

	$info_window = $notebook->add("info",
			-label => "Info", -raisecmd => \&info);
	init_info_window($info_window);

	$einzahlung_window = $notebook->add("einzahlung",
			-label => "Einzahlung", -raisecmd => \&einzahlung_raise);
	init_einzahlung_window($einzahlung_window);

	$abbuchung_window = $notebook->add("abbuchung",
			-label => "Abbuchung", -raisecmd => \&abbuchung_raise);
	init_abbuchung_window($abbuchung_window);

	$ueberweisung_window = $notebook->add("ueberweisung",
			-label => "Überweisung", -raisecmd => \&ueberweisung_raise);
	init_ueberweisung_window($ueberweisung_window);

	$buchungen_window = $notebook->add("buchungen",
			-label => "Buchungen", -raisecmd => \&buchungen);
	init_buchungen_window($buchungen_window);

	$summe_window = $notebook->add("summe",
			-label => "Summe", -raisecmd => \&summe);
	init_summe_window($summe_window);

	$schuldner_window = $notebook->add("schuldner",
			-label => "Schuldner", -raisecmd => \&schuldner);
	init_schuldner_window($schuldner_window);

	$misc_window = $notebook->add("misc",
			-label => "misc");
	init_misc_window($misc_window);

	$notebook->raise("info");

	$win->Label(-textvariable => \$output,
			-justify => 'left',
			-wraplength => 800)
		->pack;
}

$sql_trace_win_active = 0;

1;
