<%doc>
    Show a list of IP blocks with relevant info.
    The hybrid character of IPblock objects (blocks and individual addresses) makes it necessary to
    display different info in each case.
    
    We expect an arrayref of Ipblock objects
    The array is supposed to be sorted.
    This is because objects are stored in decimal in the DB
    so it makes more sense to have the DB order them at 
    retrieval time.  After that, objects are "inflated" to
    a string, which is harder to sort.

    
</%doc>

<%args>
$parent
$objects
$type             => undef
$edit             => 0
$sid              => undef
$sort             => 'Address'
$show_utilization => 1
</%args>

<%init>
my @text_headers;
my $session;
my $osession;
my %objindex;
my %orderbrief;
my @orderbrief;
my $page;
my $i;

my (@headers, @rows);
if ( $type ){
    unless ( $type =~ /^address|block$/ ){
	$m->comp('error.mhtml', error => "Unknown type: $type");
    }
}else{
    if ( ($objects->[0])->is_address ){ # We're dealing with addresses
	$type = "address";
    }else{
	$type = "block";
    }
}
</%init>

<%perl>

if ( $type eq "address" ){
    if ( $edit ){
	push @headers, '[del]';
    }
    # @headers: header for html
    push @headers, ( 'Address', 'Name', 'Status', 'Used by', 'Description', 'Last Seen' );
    foreach my $header ( @headers ){
	unless ( $sort eq $header ){
	    $header = "<a href=\"ip.html?id=$parent&view_format=list&ip_list_sort=$header\">$header</a>";
	}
    }
    # @text_headers: header for csv text
    push @text_headers, ( 'Address', 'Name', 'Status', 'Used by', 'Description', 'Last Seen' ); 

    foreach my $o ( @{ $objects } ) {
        # @row: cell_data for html text
	my @row = ();
        # @text_row: cell_data for csv text
        my @text_row = ();

	if ( $edit ){	 
	    push @row, '<input type="checkbox" name="' . "Ipblock__" . $o->id . "__delete" . '" >';
	}
	push( @row, '<a href="ip.html?id=' . $o->id . '">' . $o->address . '</a>' );
        push( @text_row, $o->address );
	if ( my @rraddrs = $o->arecords ){
	    my (@names, @text_names);
	    foreach my $rraddr ( @rraddrs ){
		push @names, $ui->form_field(object=>$rraddr, column=>"rr", edit=>$edit, returnValOnly=>1, linkPage=>'host.html');
		push @text_names, $rraddr->rr->get_label;
	    }
	    push @row, join ', ', @names;
            push @text_row, join ', ', @text_names;
	} else {
	    push( @row, "");
            push( @text_row, "");
	}

	$ui->add_to_fields(o=>$o, edit=>$edit, fields=>['status', 'used_by', 'description'], 
			   cell_data=>\@row, linkpages=>['', '', '']);
        push( @text_row, $o->status->get_label );
        push( @text_row, $o->used_by->get_label );
        push( @text_row, $o->description );      
  
	$ui->add_to_fields(o=>$o, edit=>0, fields=>['last_seen'], cell_data=>\@row, );
        push( @text_row, $o->last_seen );

	push( @rows, \@row );
        push( @$page, \@text_row);
    }
} elsif( $type eq "block" ) {

    $show_utilization = 0 if $edit;

    my @text_row = ();
    if ( $show_utilization ){
	@headers = ( 'Address/Prefix', 'Status', 'Utilization', 'Owner', 'Used by', 'Description' );
	@text_headers = ( 'Address/Prefix', 'Status', 'Utilization(Address Usage/Subnet Usage)', 'Owner', 'Used by', 'Description' );
    }else{
	if ( $edit ){
	    push @headers, '[del]';
	}
	push @headers, ( 'Address/Prefix', 'Status', 'Owner', 'Used by', 'Description' );
	@text_headers = @headers;
    }
    foreach my $o ( @{ $objects } ) {
	my $status = ($o->status)? $o->status->get_label: "";
	my @row = ();
        my @text_row = ();
	if ( $edit ){	 
	    push @row, '<input type="checkbox" name="' . "Ipblock__" . $o->id . "__delete" . '" >';
	}
	push( @row, '<a href="ip.html?id=' . $o->id . '">' . $o->get_label . '</a>' );
        push( @text_row, $o->get_label );

	push @row, $ui->form_field(object=>$o, column=>"status", edit=>$edit, returnValOnly=>1);
        push( @text_row, $status );

	if ( $show_utilization ){
	    if ( $status eq "Subnet") {
		my $title;
		push( @row, $ui->percent_bar(percent => (($o->num_children)/($o->num_addr)*100) ) );
		$title = int(($o->num_children)/($o->num_addr)*1000)/10;
		push( @text_row , $title."%" );
	    } elsif ( $status eq "Container" ) {
		my $title1;
		my $title2;
		my $total = new Math::BigInt($o->num_addr);
		my $used1 = new Math::BigInt($o->address_usage);
		my $used2 = new Math::BigInt($o->subnet_usage);
		my $percent1 = ($used1*100/$total);
		my $percent2 = ($used2*100/$total);
		
		push( @row, $ui->percent_bar2( title1=>"Address Usage: ", title2=>"Subnet Usage: ", percent1=>$percent1, percent2=>$percent2 ) );
		$title1 = int($percent1*10)/10;
		$title2 = int($percent2*10)/10;
		
		push( @text_row, $title1."%/".$title2."%" );
	    } else {
		push( @row, "" );
		push( @text_row, "" );
	    }
	}
	$ui->add_to_fields(o=>$o, edit=>$edit, fields=>['owner', 'used_by', 'description'],
			   cell_data=>\@row, linkpages=>['view.html', 'view.html', '']);

	push( @text_row, $o->owner->get_label )   if int($o->owner);
	push( @text_row, $o->used_by->get_label ) if int($o->used_by);
	push( @text_row, $o->description );
	
	push( @rows, \@row );
        push( @$page, \@text_row);
     }
}

# Create new session and get a new window for csv2 output
$i=0;
$session = $ui->mk_session();

@orderbrief = @text_headers;
$session->{orderbrief}  = \@orderbrief;
$session->{page}        = $page;
</%perl>
<& /generic/data_table.mhtml, field_headers=>\@headers, data=>\@rows &>

<div class="containerheadright">
    <a href="#" onClick="opentextwindow('','csv2','sid=<% $session->{_session_id} %>')">[csv]</a>
</div>

