use utf8;
use warnings; no warnings "redefine";
use strict;

use Loader qw(line_in line_printer log_update log_view editor);
use Fehler qw(report_fehler problem);
use Dbase::Help qw(DoFn Do qquote);
use Dbase::Globals qw(find_descr gen_descr content);

sub edit_template($$;$) {
	my($typ,$name,$okn) = @_;
	my $id = DoFn("select id from $typ where name=${\qquote $name}") || "";

	while(1) {
		my $kn = "$okn:$id:$name";
		my $act = line_in "$kn >",4; last if $act eq "";
		if($act eq '?') {
			print <<'END';
l    Gespeicherten Mustertext ausgeben
e    Gespeicherten Mustertext editieren (AHEM!!)
r/w  Mustertext aus Datei lesen / in Datei schreiben
del  diesen Text löschen
END
			next;
		}
		if($act eq "H") { log_view($kn,$typ,"id",$id); next; }
		if($act eq 'n') {
			content ( my $nname = line_in "Neuer Name: " ) or next;
			Do "update $typ set name=${\qquote $nname} where id=$id";
			$name = $nname;
			next;
		}
		if($act eq 'e' or $act eq 'r') {
			if($id and $typ eq "rech_fuss" and DoFn("select count(*) from rechlauf where template=$id") > 0) {
				problem "Diese Vorlage wurde in einem Rechnungslauf verwendet.","Sie kann daher nicht mehr verändert werden.";
				next;
			}
		}
		if($act eq 'e') {
			my $data = $id ? DoFn("select inhalt from $typ where id=$id") : undef;
			$data = editor($data,"$typ:$id:$name");
			unless(defined $data) {
				print "Fehler...nicht geupdatet.\n";
				next;
			}
			if($id and length($data) > 4) {
				log_update($typ,"id",$id,undef,"text");
				Do("update $typ set inhalt = ${\qquote $data} where id=$id");
			} elsif($id) {
				log_update($typ,"id",$id,undef,"-");
				Do("delete from $typ where id=$id");
				$id=undef;
			} elsif(length($data) > 4) {
				$id = Do("insert into $typ set name=${\qquote $name}, inhalt=${\qquote $data}");
				log_update($typ,"id",$id,undef,"+");
			}
			next;
		}
		if($act eq "r") {
			my $path = line_in "Dateiname: ";
			next unless $path;
			open(FD,'<',$path) or do {
				warn "$path: $!";
				next;
			};
			my $data;
			{ local($/)=undef; $data=<FD>; }
			close(FD);
			if($id) {
				Do("update $typ set inhalt = ${\qquote $data} where id=$id");
			} else {
				$id = Do("insert into $typ set name=${\qquote $name}, inhalt=${\qquote $data}");
			}
			log_update($typ,"id",$id,undef,"text");
			next;
		}
		unless($id) {
			print "... gibt es (noch?) nicht.\n";
			next;
		}
		if($act eq 'l') {
			my $data = DoFn("select inhalt from $typ where id=$id");
			no warnings 'once';
			line_printer(1);
			print $Db::pr_fh $data;
			next;
		}
		if($act eq 'del') {
			Do("delete from $typ where id=$id");
			return undef;
		}
		if($act eq "w") {
			my $path = line_in "Dateiname: ";
			next unless $path;
			if(-e $path) {
				warn "Die Datei existiert bereits!\n";
				next;
			}
			my $data = DoFn("select inhalt from $typ where id=$id");
			open(FD,">",$path) or do {
				warn "$path: $!";
				next;
			};
			print FD $data or warn "$path: konnt nicht schreiben: $!\n";
			close(FD);
			next;
		}
		print "Aktion '$act' kenne ich nicht. Liste mit '?'.\n";
		next; fehler: report_fehler(4);
	}
}

1;
