<%doc>
    Cable Plant Section
</%doc>
%
%
<%args>
$user => $ui->get_current_user($r)
$showheader => 1
</%args>
%
%
<%attr>
title       => 'Cable Plant' 
section     => 'Plant'
</%attr>
%
%
<%shared>
# Change this when it is all working
my $PAGE_ATTRIBUTE = "CABLE_PLANT_PAGE";
my $SECTION = "Plant";
</%shared>
%
%
<%init>
my $DEBUG = 0;
print '%ARGS is  <pre>', Dumper(%ARGS), '</pre><br>' if $DEBUG;

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

my $caller_args = $m->caller_args(-1);
if (exists($caller_args->{page})) {
    $user->setAttribute($r, $PAGE_ATTRIBUTE, $caller_args->{page});
}

my $page = $user->getAttribute($PAGE_ATTRIBUTE);

if (exists($caller_args->{tasks})){
    $user->setAttribute($r, "SHOW_TASKS", $caller_args->{tasks});
}
my $show_tasks = $user->getAttribute("SHOW_TASKS");
if ($show_tasks eq "") {
    $user->setAttribute($r, "SHOW_TASKS", "show");
    $show_tasks = "show";
}
*print_showtaskslink = $m->comp('SELF:.sub_print_showtaskslink'); 

my @sites = Site->retrieve_all();
@sites = sort { $a->name cmp $b->name } @sites;

my @bbsites      = Site->search_with_backbones();
my @closet_sites = Site->search_with_closets();

my $hideheader;
if( !$showheader ) {
    $hideheader = 'style="display:none"';
}
</%init>

<%method .section_meta_data>
    <%doc>
        Returns the meta data used to generate this sections header in section1.mhtml
    </%doc>
    <%init>
        return {  
	    section      => 'Plant',
	    page         => 'cable_plant/',
	    title        => 'Cable Plant',
	    attribute    => 'CABLE_PLANT_PAGE',
	    sub_sections => [
		{ section => 'SITES',      title => 'Sites' },
		{ section => 'CLOSETS',    title => 'Closets' },
		{ section => 'BACKBONE',   title => 'Backbone Cables' },
		{ section => 'HORIZONTAL', title => 'Horizontal Cables' },
		{ section => 'CIRCUITS',   title => 'Circuits' }
	    ]
        };
    </%init>
</%method>

% if ( $page eq "BACKBONE" || $page eq "" ) {

<div id="sectiontools" <% $hideheader %>>
 <div class="container">
    <div class="containerheadleft">
        Backbone Cable
    </div>
    <div class="containerheadright">
%       print_showtaskslink($show_tasks);
%       if ( $manager && $manager->can($user, 'access_admin_section', 'cable_plant:new_backbone') ){
            <a href="cable_backbone.html?id=NEW">[new]</a>
%       }
    </div>
    <div class="containerbody" id="tasks" style="display:<% ($show_tasks eq "show"?"block":"none") %>">

    <table>
      <tr>
	<td>
        <fieldset class="large">
            <legend>Find</legend>
            <form name="backbone_form" action="cable_plant.html" method="POST">
                <input type="hidden" name="page_type" value="BACKBONE">
	        <p>
                <label for="search">Connected from:</label>
                <select name="start_id" id="start_id" onChange="jsrsSendqueryBB(end_id, start_id.value)">
                    <option value="">-- Select --</option>
                    <%perl>
                    foreach my $site ( @bbsites ){
                       printf("<option value=\"%s\">%s</option>\n", $site->id, $site->name);
                    }
                    </%perl>
               </select>
               <p>
               <label for="search">To:</label>
               <select name="end_id" id="end_id">
               </select>
               <input name="submit_site" value="Go!" type="submit">
               </p>
           </form>
           <form name="backbone_srch" action="cable_plant.html" method="POST">
               <p>
               <input type="hidden" name="page_type" value="BACKBONE">
               <label for="search">Backbone Name: </label>
               <input name="backbone_srch" size="20">
               <input type="submit" name="submit_backbone_search" value="Search">
               </p>
           </form>
           <form name="site_srch" action="cable_plant.html" method="POST">
               <p>
               <input type="hidden" name="page_type" value="BACKBONE">  
               <label for="search">Site Name: </label>
               <input name="site_srch" size="20">
               <input type="submit" name="submit_site_search" value="Search">
               </p>
           </form>
        </fieldset>
        <fieldset class="large">
            <legend>Graph</legend>
	    <a href="backbone_graph.html">[show graph]</a>
        </fieldset>
        </td>
      </tr>
    </table>

    </div> <!-- close containerbody -->
 </div> <!-- close container -->
</div> <!-- close sectiontools -->

% } elsif ( $page eq "HORIZONTAL" ) {


<div id="sectiontools" <% $hideheader %>>
<div class="container">
    <div class="containerheadleft">
        Horizontal Cable
    </div>
    <div class="containerheadright">
%       print_showtaskslink($show_tasks);
%       if ( $manager && $manager->can($user, 'access_admin_section', 'cable_lant:new_horizontal') ){
            <a href="edit.html?table=HorizontalCable">[new]</a>
%       }
    </div>
    <div class="containerbody" id="tasks" style="display:<% ($show_tasks eq "show"?"block":"none") %>">

        <table><tr><td>
        <fieldset class="large">
            <legend>Find</legend>
	    <form name="cable_form" action="cable_plant.html" method="GET">
  	        <input type="hidden" name="page_type" value="HORIZONTAL">
<%doc>
 Name and id of this select field are different. see note below, in CLOSETS section
</%doc>
                <p>
                <label for="Site">Site:</label>
                <select name="id" id="site_id" onChange="jsrsSendqueryTB('Floor', floor_id.name, site_id.value, 'site'); jsrsResetSelection('room_id'); jsrsResetSelection('closet_id'); jsrsResetSelection('jack_id')">
                    <option value="">-- Select --</option>
                    <%perl>
                    foreach my $site (@sites) {
                        printf("<option value=\"%s\">%s</option>\n", $site->id, $site->name);
                    }
                    </%perl>
                </select>
                <input name="submit_site" value="Browse by Site" type="submit">
                </p>
<%doc>
 When selecting by floor, default to populating our jack list based upon the closet 
 lists contents            
</%doc>

                <p>
                <label for="Floor">Floor:</label>
                <select name="floor_id" id="floor_id" onChange="jsrsSendqueryTB('Room', room_id.name, floor_id.value, 'floor'); jsrsSendqueryCL(closet_id, floor_id.value); jsrsResetSelection('jack_id')">
                </select>
                </p>

<%doc>
 Because both the room list and the closet list can affect the jack list, 
 we explicitly tell the jacklist to look at this list.
</%doc>
 
                <p>
                <label for="Room">Room: </label>
                <select name="room_id" id="room_id" onChange="jsrsSendqueryTB('HorizontalCable', jack_id.name, room_id.value, 'room')">
                </select>
 
<%doc>
 Like above, make sure the jacklist uses our closet selection instead 
 of the room selection.            
</%doc>

                <p>
		<label for="Closet">or Closet:</label>
                <select name="closet_id" id="closet_id" onChange="jsrsSendqueryTB('HorizontalCable', jack_id.name, closet_id.value, 'closet')">
                </select>
                </p>

                <p>
	        <label for="Jack">Jack:</label>
                <select name="jack_id" id="jack_id">
                </select>
                <input type="button" name="go" value="Go!" onClick="if (jack_id.value && jack_id.value != -1) { location.href = ('view.html?table=HorizontalCable&id=' + jack_id.value); } else if (document.cable_form.id.value) { location.href = ('view.html?table=HorizontalCable&id=' + document.cable_form.id.value); } return true;">
                </p>

                <hr>

                <p>
	        <label for="Jack ID">Jack ID:</label>
%               my $srchf = 'jack_srch';  # name of the form input tag
	        <input type="text" name="<% $srchf %>" value="By Jack ID(s)" onFocus="if (this.value == 'By Jack ID(s)') { this.value = ''; } return true;">
    	        <input type="button" name="" value="Lookup Jack" onClick="jsrsSendquery('HorizontalCable', <% 'jack_lookup.name' %>, <% $srchf . '.value' %>)">
                </p>

                <p>
	        <label for="Lookup Results">Lookup Results:</label>
                <select name="jack_lookup" id="jack_lookup">
                </select>
                <input type="button" name="go_lookup" value="Go!" onClick="if (jack_lookup.value) { location.href = ('view.html?table=HorizontalCable&id=' + jack_lookup.value); return true; }">
                </p>
            </form>
        </fieldset>
        </td></tr></table>

    </div> <!-- end containerbody -->
</div> <!-- end container -->
</div> <!-- close sectiontools -->

% } elsif ($page eq "SITES" && $m->request_comp->name ne "site_tasks.html") {

% $m->comp('site_tasks.html', %ARGS);

% } elsif ( $page eq "CLOSETS" ) {

<div id="sectiontools" <% $hideheader %>>
<div class="container">
    <div class="containerheadleft">
        Closets
    </div>
    <div class="containerheadright">
%       print_showtaskslink($show_tasks);
%       if ( $manager && $manager->can($user, 'access_admin_section', 'cable_plant:new_closet') ){
            <a href="closet.html?id=NEW">[new]</a>
%       }
    </div>
    <div class="containerbody" id="tasks" style="display:<% ($show_tasks eq "show"?"block":"none") %>">

    <table><tr><td>
        <fieldset class="large">
            <legend>Find</legend>
            <form name="cable_form" action="cable_plant.html" method="GET">
                <input type="hidden" name="page_type" value="CLOSETS">
                <p>
                <label for="search">Site:</label>
<%doc>
 name and id of this select field are different. name is for form submitting, because
 cable_plant.html needs the site id named "id". id is for the javascript queries. when
 it was named "id", id.value returned an undefined value. not quite sure why this is, maybe
 javascript doens\'t allow fields to be named "id"?
</%doc>
                <select name="id" id="site_id" onChange="jsrsSendqueryTB('Floor', floor_id.name, site_id.value, 'site'); jsrsResetSelection('closet_id');">
                    <option value="">-- Select --</option>
                    <%perl>
                    foreach my $site ( @closet_sites ) {
                        printf("<option value=\"%s\">%s</option>\n", $site->id, $site->name);
                    }
                    </%perl>
                </select> <input name="submit_site" value="Browse by Site" type="submit">
                </p>

                <p>
                <label for="search">Floor:</label>
                <select name="floor_id" id="floor_id" onChange="jsrsSendqueryCL(closet_id, floor_id.value);">
                </select>
                </p>

                <p>
                <label for="search">Closet:</label>
                <select name="closet_id" id="closet_id">
                </select>
                <input type="button" name="go" value="Go!" onClick="if (closet_id.value && closet_id.value != -1 && closet_id.value != 0) { location.href = ('closet.html?id=' + closet_id.value); } else if (document.cable_form.id.value) { location.href = ('cable_plant.html?id=' + document.cable_form.id.value + '&page_type=CLOSETS'); } return true;">
                </p>
           </form>

           <hr>

           <form name="closet_form" action="cable_plant.html" method="POST">
               <input type="hidden" name="page_type" value="CLOSETS">
                <p>
                <label for="search">Closet Name:</label>
                <input name="closet_srch" size="25">
                <input type="submit" name="submit_closet_search" value="Search">
                </p>
           </form>
         </fieldset>
         </td></tr></table>

    </div> <!-- close containerbody -->
</div> <!-- close container -->
</div> <!-- close sectiontools -->

% } elsif ($page eq "CIRCUITS") {

<div id="sectiontools" <% $hideheader %>>
<div class="container">
    <div class="containerheadleft">Circuits</div>
    <div class="containerheadright">
%       print_showtaskslink($show_tasks);
%       if ( $manager && $manager->can($user, 'access_admin_section', 'cable_plant:new_circuit') ){
            <a href="edit.html?table=Circuit">[new]</a>
%       }
    </div>

    <div class="containerbody" id="tasks" style="display:<% ($show_tasks eq "show"?"block":"none") %>">
      <p>Search Circuits by keywords
      <br>(Relevant fields include: Circuit ID, Site Link name, Near and Far Sites, Entity)
      <p>
      <form name="circuit_srch_form" action="circuit.html" method="POST">
          <input type="hidden" name="_action" value="SEARCH"> 
          <input type="text" name="search" size="30"> <input type="submit" name="submit_circuit_search" value="Search">
      </form>
    </div> <!-- close containerbody -->
</div> <!-- close container -->
</div> <!-- close sectiontools -->

% }


% #############################################################################
%
% if ( $m->fetch_next ){
%  $m->call_next;
% }
