<%doc>
    Delete one or more objects
</%doc>

<%args>
$table         => undef
$id            => undef
$user          => $ui->get_current_user($r)
$submit        => "Delete"
$sid           => undef
$selectall     => undef
$selectall_ids => undef
$return_url    => undef
$warning       => undef
</%args>

<%init>
my $DEBUG = 0;
my $session;
my @objs;
print '%ARGS is  <pre>', Dumper(%ARGS), '</pre><br>' if $DEBUG;

my $manager = $ui->get_permission_manager($r);

</%init>

<div id="sectiondetail">

<div class="container">
    <div class="containerbody">

%if ( $submit eq "Delete" ){
   <strong>Are you sure you want to delete the following objects?</strong>
   <p>
   <ul>
<%perl>
   eval {
       $session = $ui->mk_session();
   };
   if ( my $e = $@ ){
       $m->comp('error.mhtml', error=>$e);
   }
   if ( $selectall && $selectall_ids && $table ){
       # This means that we were passed a list of ids for a given table
       foreach my $id ( split /,/, $selectall_ids ){
	   my $key = $table . "__" . $id;
	   $ARGS{$key} = "";
       }
   }elsif ( $table && $id ){
       # We were passed a single id for a given table
       my $key = $table . "__" . $id;
       $ARGS{$key} = "";
   }
   foreach my $arg ( sort keys %ARGS ){
       next unless $arg =~ /__/;
       my ($table, $id) = split /__/, $arg;
       my $obj;
       unless ( $obj = $table->retrieve($id) ){
	   $m->comp('error.mhtml', error => "Error retrieving $table id $id");
       }
       my $lbl = $obj->get_label;
       print "<li>$table $lbl</li>";
       push @objs, [$table, $obj->id];
   }
   $session->{objects} = \@objs;
</%perl>
    </ul> 
%  if ( !($warning eq "") ){
    <nobr>
    <p>
    <strong>WARNINGS: </strong>
     <% $warning %></p>
    </nobr>
    <br>
%} 
    <form method="POST" action="delete.html">
    <input type="hidden" name="sid" value="<% $session->{_session_id} %>">
    <input type="hidden" name="return_url" value="<% $return_url %>">
    <input name="submit" value="Confirm" type="submit">
    <input type="button" name="cancel" value="Cancel" onClick="history.go(-1);">
    </form>
<%perl>


}elsif ( $submit eq "Confirm" ){
    eval{
	$session = $ui->get_session($sid);
    };
    if ( my $e = $@ ){
	$m->comp('error.mhtml', error=>$e);
    }

    my @records = @{$session->{objects}};
    my @objs;
    foreach my $l ( @records ){
	my ($table, $id) = @$l;
	push @objs, $table->retrieve($id);
    }
    my @lbls = map { "$table " . $_->get_label } @objs;
    eval {
	Netdot::Model->do_transaction(sub{
	    foreach my $obj ( @objs ){
		unless ( $manager && $manager->can($user, "delete", $obj) ){
		    $ui->throw_user("You don't have permission to delete this object");
		}
		$obj->delete();
	    }
				      });
    };
    if ( my $e = $@ ){
	$m->comp('error.mhtml', error=>$e);
    } 

    print "<ul>\n";
    foreach my $lbl ( @lbls ){
	print "<li>$lbl has been deleted</li>";
    }
    print "</ul>\n";

    if ( $return_url ){
	print '<p><br>';
	print "&nbsp; <a href=\"../$return_url\"><strong>[return]</strong></a>";
    }
    
}elsif ( $submit eq "Cancel" ){
    print "<p><br>";
    print "Delete cancelled";
    if ( $session ){
	$ui->rm_session($session);
    }
}


</%perl>


</div> <!-- close containerbody -->
</div> <!-- close container -->

</div> <!-- close sectiondetail -->

