<%doc>
Circuit Interface
</%doc>

<%args>
$id             => undef
$edit           => undef
$strand_sort    => undef
$search         => undef
$_action        => undef
$sequence_list  => undef
$add_interfaces => undef
$new_interface  => undef
$add_strands    => undef
$user           => $ui->get_current_user($r)
</%args>
<%attr>
title   => 'Circuit' 
</%attr>

<%init>
my $DEBUG = 0;
print "<pre>", Dumper(%ARGS), "</pre><br>" if $DEBUG;

my $o = Circuit->retrieve($id) if ($id && $id ne "NEW");
my $manager = $ui->get_permission_manager($r);
my @list;

if ( $_action ){
    if ( $_action eq "SEARCH" ) {
	if ( length($search) ){
	    $search = Netdot->rem_lt_sp($search);
	    unless ( @list = Circuit->search_by_keyword($search) ){
		print  "<p><b>No results</b>";
		$m->abort;
	    }
	    my $num = scalar(@list);
	    if ( $num == 1 ){
		# Don't offer list.  Just display this circuit
		$o      = $list[0];
		$id     = $o->id;
		$search = undef;
	    }
	}else{
	    # Show all
	    @list = Circuit->retrieve_all();
	}

    }elsif( $_action eq "ADD_STRANDS" && $sequence_list ){
	my @sequences;
	if ( ref($sequence_list) eq "ARRAY" ) {
	    @sequences = @{$sequence_list};
	}else {
	    push(@sequences, $sequence_list);
	}
	if ( scalar @sequences ){
	    foreach my $seq ( @sequences ){
		my @strands = split(/,/o, $seq);
		foreach my $strandid ( @strands ){
		    my $strand = CableStrand->retrieve($strandid);
		    $strand->update({circuit_id=>$o});
		}
	    }
	}else{
	    $m->comp("/generic/error.mhtml", error=>"Empty sequence list");
	}
    }elsif( $_action eq "UPDATE_SEQUENCES" ){
	foreach my $key ( keys(%ARGS) ) {
	    next if ($key !~ /^__unassign_(\d+)/o);
	    my @strands = split(/,/o, $ARGS{"__sequence_" . $1});
	    foreach my $strandid ( @strands ){
		eval {
		    my $strand = CableStrand->retrieve($strandid);
		    $strand->update({circuit_id=>""});
		};
		if ( my $e = $@ ){
		    $m->comp("/generic/error.mhtml", error=>"Problem updating strand: $e");
		}
	    }	    
	}
    }elsif( $_action eq "ADD_INTERFACE" ){
	if ( $new_interface && $new_interface !~ /\D/o){
	    eval {
		my $i = Interface->retrieve($new_interface);
		$i->update({circuit=>$o});
	    };
	    if ( my $e = $@ ){
		$m->comp("/generic/error.mhtml", error=>"Problem updating interface: $e");
	    }
	}else{
	    $m->comp("/generic/error.mhtml", error=>"No interface selected, or invalid value");
	}
    }elsif( $_action eq "UPDATE_INTERFACES" ){
	my @ints;
	foreach my $key  ( keys %ARGS ){
	    my $id;
	    if ( $key =~ /^_un_interface_(\d+)$/og ){
		$id = $1;
	    }else{
		next;
	    }
	    eval {
		my $i = Interface->retrieve($id);
		$i->update({circuit=>''});
	    };
	    if ( my $e = $@ ){
		$m->comp("/generic/error.mhtml", error=>"Problem unassigning interface: $e");
	    }
	}
    }
}
</%init>

% if ( defined $search && @list ){
<div class="container">
  <div class="containerhead">
    <% scalar(@list) %> results for <i><% $search %></i>
  </div>
  <div class="containerbody">
      <& /generic/sortresults.mhtml, object => \@list, view => "row", page => "circuit.html", withdelete => 1, withedit => 1 &>
  </div>
</div>

% }elsif ( $o ){

<div id="sectiondetail">

<!-- Header Table -->
<div class="containeroutside">
    <div class="containerheadoutside">
%    if ( $o->linkid ){
        Circuit for Site Link: <a href="view.html?table=SiteLink&id=<% $o->linkid %>"><% $o->linkid->name %></a>
%    }else{
        Circuit ID: <a href="circuit.html?id=<% $o->id %>"><% $o->cid %></a>
%    }
    </div>
    <div class="containerbodyoutside">

<!-- Circuit Table -->
<form name="netdotform" action="circuit.html" method="POST">
<div class="container">
    <div class="containerheadleft"><strong>Circuit</strong></div>
    <div class="containerheadright">
        <a href="circuit.html?id=<% $o->id %>">[refresh]</a>
%   if ( $manager && $manager->can($user, 'access_admin_section', 'circuit:new') ){
        <a href="edit.html?table=Circuit">[new]</a>
%   }
%   if ( $manager && $manager->can($user, 'edit', $o) ){
        <a href="edit.html?table=Circuit&id=<% $o->id %>">[edit]</a>
%   }
%   if ( $manager && $manager->can($user, 'delete', $o) ){
        <a href="delete.html?table=Circuit&id=<% $o->id %>">[delete]</a>
%   }
          <a href="#" onClick="opentextwindow(jspopoutstring,'js','');">[text]</a>
    </div>
    <div class="containerbodyoutside">
        <& /generic/form.mhtml, table=>"Circuit", id=>$o->id, edit=>$edit &>
    </div>
</div>
</form>
<!-- End Circuit Table -->

%# Interfaces
<& circuit_interfaces.mhtml, interfaces=>[$o->interfaces], id=>$id, add=>$add_interfaces &>

%#  Strand Sequences
<p>
<& display_sequence.mhtml, strands=>[$o->strands], id=>$id, add=>$add_strands, table_view=>1 &>
<p>

%#  Contacts
% my $cl;
% if ( $o->vendor && int($cl = $o->vendor->contactlist) && $cl->contacts ){
<!-- Contacts Table -->
<div class="container">
    <& /generic/contactlist.mhtml, id=>$cl->id, edit=>0, show_buttons=>0 &>
</div>
<!-- End Contacts Table -->
% }


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

%}
