use utf8;
use warnings; no warnings "redefine";
use strict;
use Dbase::Globals qw(content);
use Dbase::Help qw(DoFn);
use Fehler qw(report_fehler warnung);
use Loader qw(
  line_in
  list_messstelle
  log_view
  set_messstelle_datum
  set_messstelle_info
  set_messstelle_name
  set_messstelle_oid
  set_messstelle_typ
  strip_kn
);

sub edit_messstelle($;$) {
    my ( $idi, $kn ) = @_;

    my ( $name ) = DoFn("SELECT name FROM mess_stelle WHERE id = $idi");

    $kn = strip_kn($kn) . 'Messstelle';
    while ( content( my $act = line_in "$kn $idi:$name>", 4 ) ) {
        if ( $act eq '?' ) {
            print <<_;
l     Details der Messstelle
n     Namen ändern (Abbruch, falls Name innerhalb eines RZ bereits vorhanden ?????)
oid   OID-Suffix ändern (Abbruch, falls OID auf SNMP-Host bereits vergeben)
t     Typ ändern
x     Beginn- und/oder Ende-Datum bearbeiten
i     Info

H     History anzeigen
_
            next;
        }
        if ( $act eq 'l' )   { list_messstelle($idi); next; }
        if ( $act eq 'n' )   { set_messstelle_name($idi,  $kn); next; }
        if ( $act eq 'oid' ) { set_messstelle_oid($idi,   $kn); next; }
        if ( $act eq 't' )   { set_messstelle_typ($idi,   $kn); next; }
        if ( $act eq 'x' )   { set_messstelle_datum($idi, $kn); next; }
        if ( $act eq 'i' )   { set_messstelle_info($idi,  $kn); next; }
        if ( $act eq 'del' ) {
            my ($mt) = DoFn "SELECT typ FROM mess_stelle WHERE id = $idi";
            my ($cnt) = DoFn "SELECT count(*) FROM mess_stelle WHERE typ = $mt";
            if ( $cnt == 1 ) {
                warnung "Es ist die letzte Messstelle des Messtyps #$mt!";
            }
            delete_messstelle( $idi, $kn );
            next;
        }
        if ( $act eq 'H' )   { log_view $kn, mess_stelle => id => $idi; next; }
        print "Aktion '$act' unbekannt.\n";
        next;
        report_fehler(4);
    }
    $idi;
}

1;
