<%doc>
User Interface for Cable Strand Operations 
</%doc>


<%args>
$id          => undef;
$backbone_id => undef;
$edit        => undef;
$_action     => undef;
$__splice1   => undef;
$__splice2   => undef;
$user        => $ui->get_current_user($r)
</%args>

<%attr>
title   => 'Cable Strand' 
</%attr>

<%init>
my $DEBUG       = 0;
my $o           = undef;
my $edit_strand = 0;

if ( $id && $id ne "NEW" ){
    $o = CableStrand->retrieve($id);
    $backbone_id = $o->cable;
}

$edit_strand = 1 if ($id eq "NEW" || $edit eq "strandinfo");

# For /generic/attribute_table.mhtml
my (@field_headers, @cell_data);

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

</%init>

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

# code for inserting/updating
# -----------------------------------------------------------------------------
if ( defined($_action) ) {
    print "ARGS is <pre>", Dumper(%ARGS), "</pre><br>" if $DEBUG;
    
    my %update_info = ();
    if ( !(%update_info = $ui->form_to_db(%ARGS)) ){
        $m->comp("../generic/error.mhtml", error => $ui->error());
    }

    if ( $update_info{CableStrand}{id} ){
        $id = (keys %{$update_info{CableStrand}{id}})[0];
        $edit_strand = 0;
    }
    
    # Do this to 'flush' the values associated with the object
    # before redisplaying
    $o = undef;
    $o = CableStrand->retrieve($id) if ( $id && $id ne "NEW" );

    # Special case for splices, store in many to many table...
    if ( defined($__splice1) || defined($__splice2) ){
        # first delete all splices for this cable strand.
	$o->delete_splices();
        
        # and now add back any splices...
        my @splices = ("__splice1", "__splice2");
        foreach my $sp ( @splices ){
            next if (!defined($ARGS{$sp}) || $ARGS{$sp} eq "");
	    Splice->insert({strand1=>$o, strand2=>$ARGS{$sp}});
        }
    }
}
# end insertion/update code
# -----------------------------------------------------------------------------
</%perl>     

% # interface...
% # ---------------------------------------------------------------------------
<!-- Header Table -->
<div class="container">
<div class="containerheadleft">
        Cable Strand:
% 	if (defined($o)) {
            <% $o->name %>
%	}
</div>
<div class="containerheadright">
        <form name="netdotform" action="cable_strand.html" method="POST">
        <input type="hidden" name="id" value="<% $id %>">
        <input type="hidden" name="_action" value="<% $id eq "NEW" ? "NEW" : "UPDATE" %>">
%       if ($edit_strand) {
            <input type="button" name="cancel_button" value="cancel" onClick="history.go(-1);"> <input name="submit" value="save" type="submit">
%       } else {
%           if ( $manager && $manager->can($user, 'access_admin_section', 'cable_strand:new') ){
                <a href="cable_strand.html?id=NEW">[new]</a> 
%           }
%           if ( $manager && $manager->can($user, 'edit', $o) ){
                <a href="cable_strand.html?id=<% $id %>&edit=strandinfo">[edit]</a> 
%           }
%           if ( $manager && $manager->can($user, 'delete', $o) ){
                <a href="delete.html?table=CableStrand&id=<% $id %>">[delete]</a>
%           }
            &nbsp;
%       }
</div>
<!-- End Header Table -->
<div class="containerbody">
<%perl>
(@field_headers, @cell_data) = ();
push( @field_headers, "Name: " );
push( @cell_data, $ui->form_field(object=>$o, table=>"CableStrand", column=>"name", edit=>$edit_strand, returnValOnly=>1) );
push( @field_headers, "Number: " );
push( @cell_data, $o ? $o->number : "" );
push( @field_headers, "Backbone: " );

if ( !$edit_strand ){
    push( @cell_data, '<a href="cable_backbone.html?id='.$o->cable.'">'.$o->cable->name.'</a>' );
}else{
   push( @cell_data, $ui->form_field(object=>$o, table=>"CableStrand", column=>"cable", lookup=>"BackboneCable", edit=>$edit_strand, returnValOnly=>1) );
}

push( @field_headers, "Status: " );
push( @cell_data, $ui->form_field(object=>$o, table=>"CableStrand", column=>"status", lookup=>"StrandStatus", edit=>$edit_strand, returnValOnly=>1) );
push( @field_headers, "Spliced with: " );
</%perl>

<&| "/generic/HERE.mhtml" &>
% if ( !$edit_strand ){
%     my @splices = $o->splices;
%     if (scalar(@splices)) {
          <a href="cable_strand.html?id=<% $splices[0]->strand2->id %>"><% $splices[0]->strand2->name %></a>
%     }
%     if (scalar(@splices) == 2) {
          , <a href="cable_strand.html?id=<% $splices[1]->strand2->id %>"><% $splices[1]->strand2->name %></a> 
%     }
% } elsif ($edit_strand && defined($o)) {
<%perl>
    # first we need to grab all backbones for start and end sites of this strand.
    my ($start_site, $end_site) = ($o->cable->start_closet->room->floor->site, $o->cable->end_closet->room->floor->site) 
    if ($o->cable->start_closet->room->floor && $o->cable->end_closet->room->floor);
        my @backbones = ();
        if ($start_site && $end_site){
            foreach my $closet (($start_site->closets, $end_site->closets)){
	        map { push (@backbones, $_) } (BackboneCable->search(start_closet=>$closet),
		   		               BackboneCable->search(end_closet=>$closet));
            }
    }
</%perl>
    <script language="JavaScript">
                <!--
                function lookupStrands(bb_id, field_name, form_name)
                {
                    if (!bb_id)
                    {
                        alert("You must select a backbone.");
                    }
                    var url = "strand_backbone_list_query.html?bb_id=";
                    url += bb_id + "&field_name=" + field_name;
                    url += "&form_name=" + form_name;
                    var wind = window.open(url, "tmp", "width=1,height=1");
                    wind.blur();
                }
                -->
    </script>
    <select name="backbone_list_srch1" onChange="lookupStrands(this.value, '__splice1', 'netdotform');">
    <option value="">Lookup by Backbone</option>
<%perl>
    my @splices = $o->splices;
    my %stored = ();
    foreach my $bb (sort { $a->name cmp $b->name } @backbones){
	next if ($bb->id == int($o->cable) || exists($stored{$bb}));
	$stored{$bb} = 1;
	printf("<OPTION VALUE=\"%s\">%s</OPTION>\n", $bb->id, $bb->name);
    }
</%perl>
    </select> &nbsp; 
    <select name="__splice1">
        <option value="">-- Make your selection --</option>
%       if (scalar(@splices)) {
            <option value="<% $splices[0]->strand2->id %>" SELECTED><% $splices[0]->strand2->name %></option>
%       }
        <option value="">[null]</option>
    </select>
    <br>
    <select name="backbone_list_srch2" onChange="lookupStrands(this.value, '__splice2', 'netdotform');">
    <option value="">Lookup by Backbone</option>
<%perl>
    %stored = ();
    foreach my $bb (sort { $a->name cmp $b->name } @backbones){
	next if ($bb->id == int($o->cable) || exists($stored{$bb}));
	$stored{$bb} = 1;
	printf("<OPTION VALUE=\"%s\">%s</OPTION>\n", $bb->id, $bb->name);
    }
</%perl>
    </select> &nbsp; 
    <select name="__splice2">
        <option value="">-- Make your selection --</option>
%       if (scalar(@splices) == 2) {
            <option value="<% $splices[1]->strand2->id %>" SELECTED><% $splices[1]->strand2->name %></option>
%       }
        <option value="">[null]</option>
        </select>
% } 
</&>

<%perl>
push( @cell_data, $_ );
if ( defined($o) && !$edit_strand ){
    push( @field_headers, "Part of Sequence:" );
    my @strands = ($o);
    push( @cell_data, $m->scomp( "display_sequence.mhtml", strands=>\@strands ) );
}

push( @field_headers, "Circuit: " );
push( @cell_data, $ui->form_field(object=>$o, column=>"circuit_id", lookup=>"Circuit", 
				  edit=>$edit_strand, returnValOnly=>1, linkPage => "circuit.html") );

push( @field_headers, "Fiber Type: " );
push( @cell_data, $ui->form_field(object=>$o, column=>"fiber_type", lookup=>"FiberType", edit=>$edit_strand, returnValOnly=>1) );
push( @field_headers, "Description: " );
push( @cell_data, $ui->form_field(object=>$o, column=>"description", edit=>$edit_strand, returnValOnly=>1) );
</%perl>

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

	<div class="containerseparator">Comments:</div>
        <% $ui->form_field(object=>$o, column=>"info", edit=>$edit_strand, htmlExtra=>"rows='3' cols='80'", returnValOnly=>1) %>
        </form>
</div>
</div>

<!-- End Cable Strand Table -->
