<%doc>
Interface for MAC addresses
</%doc>
%
%
<%attr>
title   => 'MAC' 
section => 'Management'
</%attr>
%
%
<%args>
$id           => undef
$search       => undef
$search_live  => undef
$vlan         => undef
$delete       => undef
$fwt_limit    => Netdot->config->get('SHOW_LAST_N_FTE') || 10
$arp_limit    => Netdot->config->get('SHOW_LAST_N_ARP') || 10
$edit_attr    => undef
$user         => $ui->get_current_user($r)
$dowindow     => undef
</%args>
%
%
<%init>
my $DEBUG = 0;
my $o;
my @list;
my (@interfaces, @devices);
my $edge_port;
my ($fwt, $arp);
print "<pre>", Dumper(%ARGS), "</pre><br>" if $DEBUG;

</%init>

<%doc>
#######################################################################################
# Search
#
#######################################################################################
</%doc>

% if ( defined($search) ){
%        # Remove trailing and leading spaces
%        $search = $ui->rem_lt_sp($search);
%        @list = PhysAddr->search_like(address=>$search);

	<div class="container">
          <div class="containerhead">
              Query <em><% $search %></em> returned: <% scalar(@list) %> matches
          </div>
          <div class="containerbody">
%         if ( scalar(@list) > 1 ){
              <& /generic/sortresults.mhtml, object => \@list, dowindow => $dowindow &>
%         }elsif ( @list ){
%             $o = $list[0];
%         }else{
              <p>Would you like to perform a "live" search in the network? (It might take a while).
              <p>
              <form action="mac.html" method="POST">
  	        <select name="vlan">
                <option value="" SELECTED>Select VLAN ID</option>
%	        foreach my $v ( sort { $a->vid <=> $b->vid } Vlan->retrieve_all ){
		    <option value="<% $v->vid %>"><% $v->vid %></option>
%	        }
	        </select>
                <input name="submit" value="Live Search" class="btn" type="submit">
                <input type="hidden" name="search_live" value="<% $search %>">
              </form>
%         }
          </div>
       </div>
<%perl>
}elsif ( $search_live ){
    if ( !$vlan ){
	$m->comp('../generic/error.mhtml', error=>"Please select a VLAN ID");
    }
    my $info;
    my $address = PhysAddr->format_address($search_live);
    eval { $info = Device->search_address_live(mac=>$address, vlan=>$vlan) };
    if ( my $e = $@ ){
	$m->comp('/generic/error.mhtml', error=>$e);
    }
    if ( $info ){
	$m->comp('live_results.mhtml', info=>$info);
    }else{
	print "Not found";
    }
}


if ( $id && !$o ){
    unless ( $o = PhysAddr->retrieve($id) ){
	$m->comp('/generic/error.mhtml', error => "Could not retrieve PhysAddr id: $id");
    }
}

if ( $o ){
    
    my $manager = $ui->get_permission_manager($r);
    unless ( $manager && $manager->can($user, "view", $o) ){
	$m->comp('/generic/error.mhtml', error=>"You don't have permission to view this object");
    }

    $id ||= $o->id;
    # Get latest forwarding table entries
    $fwt = $o->get_last_n_fte($fwt_limit);
    # Get latest arp entries
    $arp = $o->get_last_n_arp($arp_limit);
    # Devices whose base_mac address is this one
    @devices = $o->devices;
    # Interfaces with this address
    @interfaces = $o->interfaces;
    # Edge port
    $edge_port  = $o->find_edge_port;
    
    my (@headers, @data) = ();
    
    my @fields = ('address');
    $ui->add_to_fields(o=>$o, edit=>0, fields=>\@fields, 
		       field_headers=>\@headers, cell_data=>\@data);
    
 # Add Vendor
    push(@headers, 'Vendor');
    my $vendor = $o->vendor;
    my $oui    = $o->oui;
    my $srch   = "$oui*";
    my $slink  = "<a href=\"../generic/search_obj.html?table=PhysAddr&address=$srch&res=1\">$vendor ($oui)</a>";
    push(@data, $slink);
    
    @fields = ('static', 'first_seen', 'last_seen');
    $ui->add_to_fields(o=>$o, edit=>0, fields=>\@fields,
		       field_headers=>\@headers, cell_data=>\@data);

</%perl>
<!-- Display main object values -->
<div class="container">
    <div class="containerhead"><strong>MAC Address</strong></div>
    <div class="containerbodyoutside">
        <& /generic/attribute_table.mhtml, field_headers=>\@headers, data=>\@data, 
           width=>"1", headercolwidth=>"25%", datacolwidth=>"75%" &>
    </div>
</div>
<!-- End Display main object values -->

%if ( @devices ){
<!-- Display Device -->
<div class="container">
    <div class="containerhead"><strong>Devices using this address</strong></div>
    <div class="containerbodyoutside">
        <& /generic/sortresults.mhtml, object=>\@devices &>
    </div>
</div>

<!-- EndDisplay Device -->
%}

%if ( @interfaces ){
<!-- Display Interfaces -->
<div class="container">
    <div class="containerhead"><strong>Interfaces using this address</strong></div>
    <div class="containerbodyoutside">
        <& /generic/sortresults.mhtml, object=>\@interfaces &>
    </div>
</div>

<!-- EndDisplay Interfaces -->
%}

<%perl>
    if ( $edge_port ){
	my $iface = Interface->retrieve($edge_port);
	my ($jack, $room);
	if ( $iface->jack ){
	    $jack = $iface->jack->jackid;
	    $room = $iface->jack->room->name if $iface->jack->room;
	}else{
	    $jack = $iface->jack_char;
	    $room = $iface->room_char;
	}
	$jack ||=  'n/a';
	$room ||=  'n/a';
	my $lbl = $iface->get_label;
	my $lnk = "<a href=\"interface.html?id=$edge_port\">$lbl</a>";
</%perl>
<!-- Display Location -->
<div class="container">
    <div class="containerhead"><strong>Edge Port</strong></div>
    <div class="containerbodyoutside">
        <& /generic/data_table.mhtml, field_headers=>['Interface', 'Room', 'Jack', 'Description'], 
           data=>[[$lnk, $room, $jack, $iface->description]] &>
    </div>
</div>
<!-- End Display Location -->
%    }

<%perl>
    if ( $fwt && scalar @$fwt ){
        my @rows;
	my %tstamps;
        foreach my $row ( @$fwt ){
            my ($tstamp, $iid) = @$row;
            my $iface = Interface->retrieve($iid);
            my $lbl   = $iface->get_label;
            my $lnk   = "<a href=\"interface.html?id=$iid\">$lbl</a>";
	    push @{$tstamps{$tstamp}}, $lnk;
        }
	foreach my $tstamp ( reverse sort keys %tstamps ){
            push @rows, [$tstamp, join ', ', @{$tstamps{$tstamp}}];
	}
</%perl>
<!-- Display FWT -->
<div class="container">
    <div class="containerheadleft"><strong>Last <% $fwt_limit %> forwarding table entries</strong></div>
    <div class="containerheadright">
      <form name="netdotform" action="mac.html" method="POST">
	<input type="hidden" name="id" value="<% $id %>">
	Last <input type="text" size="3" name="fwt_limit" value="<% $fwt_limit %>"> FWT entries <input type="submit" name="submit" value="show">
      </form>
    </div>
    <div class="containerbodyoutside">
        <& /generic/data_table.mhtml, field_headers=>['Time Seen', 'Interfaces'], data=>\@rows, style=>['', 'width: 80%']  &>
    </div>
</div>
<!-- End Display FWT -->
%     }

<%perl>
    if ( $arp && scalar @$arp ){
        my @rows;
	my %tstamps;
        foreach my $row ( @$arp ){
            my ($iid, $ipid, $tstamp) = @$row;
            my $lbl    = Interface->retrieve($iid)->get_label;
            my $lnk   = "<a href=\"interface.html?id=$iid\">$lbl</a>";
	    push @{$tstamps{$tstamp}{$ipid}}, $lnk;
        }
	foreach my $tstamp ( reverse sort keys %tstamps ){
	    foreach my $ipid ( keys %{$tstamps{$tstamp}} ){
		my $iplbl   = Ipblock->retrieve($ipid)->get_label;
		my $iplnk   = "<a href=\"../management/ip.html?id=$ipid\">$iplbl</a>";
		push @rows, [$tstamp, $iplnk, (join ', ', @{$tstamps{$tstamp}{$ipid}})];
	    }
	}
       
</%perl>
<!-- Display ARP -->
<div class="container">
    <div class="containerheadleft"><strong>Last <% $arp_limit %> ARP entries</strong></div>
    <div class="containerheadright">
      <form name="netdotform" action="mac.html" method="POST">
	<input type="hidden" name="id" value="<% $id %>">
	Last <input type="text" size="3" name="arp_limit" value="<% $arp_limit %>"> ARP entries <input type="submit" name="submit" value="show">
      </form>
    </div>
    <div class="containerbodyoutside">
        <& /generic/data_table.mhtml, field_headers=>['Time Seen', 'IP', 'Interfaces'], data=>\@rows, style=>['', '', 'width: 60%']  &>
    </div>
</div>
<!-- End Display ARP -->

%   }

%   if ( my @scopes = $o->dhcp_hosts ){
<!-- Display DHCP Host Scopes -->
  <div class="container">
    <div class="containerhead"><strong>DHCP Scopes</strong></div>
    <div class="containerbody">
        <& /generic/sortresults.mhtml, object=>\@scopes &>
    </div>
  </div>
<!-- End Display DHCP Scopes -->
%   }

<!-- Display Custom Attributes -->
% my @attributes = $o->attributes;
  <div class="container">
    <div class="containerheadleft"><strong>Custom Attributes</strong></div>
    <div class="containerheadright">
%     if ( @attributes ) {
        <a href="mac.html?id=<% $o %>&edit_attr=1">[edit]</a>
%     }
        <a href="edit.html?table=PhysAddrAttr&physaddr=<% $o %>">[add]</a>
    </div>
    <div class="containerbody">
        <& /generic/sortresults.mhtml, object=>\@attributes, withedit=>$edit_attr, return_args=>"?id=$id" &>
    </div>
  </div>
<!-- End Display Custom Attributes -->


% }
