<%doc>

Add an IP service object from device page

</%doc>
%
%
<%attr>
showheader => 0
</%attr>
%
<%flags>
inherit => undef 
</%flags>
%
%
%
<%args>
$ip
$url
$service     =>  undef
$monitored   => undef
$contactlist => undef
$Add         =>  undef
$Cancel      =>  undef
</%args>
%
%
<%init>
my $DEBUG = 0;
my $ipobj;
my $cl;
my $dev;

print '%ARGS is  <pre>', Dumper(%ARGS), '</pre><br>' if $DEBUG;

if ( $ip ){
    unless ( $ipobj = Ipblock->retrieve($ip) ){
	$m->comp("error.mhtml", error => "Could not retrieve Ipblock id $ip");
    }
    if ( $ipobj->interface && ( $dev = $ipobj->interface->device ) ){
	if ( my @dcs = $dev->contacts ){
	    # Grab the first one
	    $cl = $dcs[0]->contactlist;
	}
    }
}

my $newsrvid;

# Notice the two actions (replace then reload).
# This guarantees that we don't reload the page using POST data
# when the last request was a POST 
my $bodyargs;
if ( $Add ){
    $bodyargs = "onUnload=\"opener.location.replace('$url');opener.location.reload()\"";
} else {
    $bodyargs = '';
}

</%init>


<html>
<head>
    <link rel="stylesheet" href="<% $r->dir_config('NetdotPath') %>css/style.css" type="text/css">
</head>      
<body class="bodytag" <% $bodyargs %>>

<div class="container">
    <div class="containerhead">
        Add IP Service
    </div>
    <div class="containerbody">

<%perl>
if ( $Add ){

    my %state;
    $state{ip}      = $ip;
    $state{service} = $service;

    if ( IpService->search( %state ) ){
	print ("Service already exists <br>");
    }else {
	$state{monitored} = 1 if ( defined($monitored) && $monitored eq 'on' );
	$state{contactlist} = $contactlist if defined $contactlist;
	eval{
	    IpService->insert(\%state);
	};
	if ( my $e = $@ ){
	    $m->comp("error.mhtml", error=>$e);
	}  
    }
</%perl>
    <br>
    <a href="#" onClick="window.close()">[done]</a>

%}elsif ( $Cancel ){
    <br>
    Cancelled.
    <br>
    <a href="#" onClick="window.close()">[done]</a><br>
    
%}else{

<form action="addservice.html" method="POST">
<input type="hidden" name="ip" value="<% $ip %>">
<input type="hidden" name="url" value="<% $url %>">

<%perl>
    my (@field_headers, @cell_data) = ();
    
    push( @field_headers, "IP: " );
    push( @cell_data, $ipobj->get_label );

    push( @field_headers, "Service: " );
    push( @cell_data, 
&{sub{
    my $ac = '<select name="service">';
    foreach my $o ( sort { $a->name cmp $b->name } Service->retrieve_all() ){
	    $ac .= '<option value="'.$o->id.'">'.$o->name.'</option>';
    }
    $ac .= '</select>';
    $ac;
    }}  );

    push( @field_headers, "Monitored: " );
    push( @cell_data, '<input type="checkbox" name="monitored" CHECKED>' );

    push( @field_headers, "Contact List: " );
    push( @cell_data, 
&{sub{
    my $ac = '<select name="contactlist">';
    foreach my $o ( sort { $a->name cmp $b->name } ContactList->retrieve_all() ){
	if ( defined $cl && $cl->id == $o->id ){
	    $ac .= '<option value="'.$o->id.'" SELECTED>'.$o->name.'</option>';
	}else{
	    $ac .= '<option value="'.$o->id.'">'.$o->name.'</option>';
	}
    }
    $ac .= '</select>';
    $ac;
    }}  );
</%perl>

<& /generic/attribute_table.mhtml, field_headers=>\@field_headers, data=>\@cell_data &>

    <input name="Add" value="Add" type="submit">
    <input name="Cancel" value="Cancel" type="submit">

</form>

%} #else

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