<%doc>
Closet interface 

</%doc>

<%args>
$id         => undef;
$edit       => undef;
$bb_sort    => undef;
$cable_sort => undef;
$pic_sort   => undef;
$user       => $ui->get_current_user($r)
</%args>

<%flags>

</%flags>

<%attr>
title   => 'Closet' 
section => 'Plant'
</%attr>

<%init>
my $DEBUG          = 0;
my $o              = undef;
my $editCloset     = 0;
my $editPictures   = 0;
my $editBackbones  = 0;
my $editHorizontal = 0;

if ( defined $id && $id ne "NEW" ){
    $o = Closet->retrieve($id);
    $m->comp('/generic/error.mhtml', error=>"Cannot retrieve Closet id $id") 
	unless $o;
}

$editCloset     = 1 if ($id eq "NEW" || $edit eq "closetinfo");
$editPictures   = 1 if ($edit eq "closetpictures");
$editBackbones  = 1 if ($edit eq "bbinfo");
$editHorizontal = 1 if ($edit eq "cableinfo");

my (@field_headers, @cell_data); # For /generic/attribute_table.mhtml calls
my (@headers, @rows);	# For /generic/data_table.mhtml calls.  Aware there need not really be 4 variables.
my $manager = $ui->get_permission_manager($r);
</%init>


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

# code for inserting/updating
# -----------------------------------------------------------------------------

if ( defined($ARGS{_action}) && $ARGS{_action} eq "ADD_PICTURE" && defined($ARGS{__picture}) && $ARGS{__picture} ne "" ) {
    eval {
	use Apache2::Upload;
	my $bindata;
	my $upload    = $r->upload("__picture");
	my $upload_fh = $upload->fh;
	my $size      = $upload->slurp($bindata);
	ClosetPicture->insert({closet=>$id, filename=>$ARGS{__picture}, bindata=>$bindata, filesize=>$size});
    };
    if ( my $e = $@ ){
	$m->comp('/generic/error.mhtml', error=>$e);
    }
}elsif ( defined($ARGS{_action}) ) {
    my %update_info = ();
    print "ARGS is  <pre>", Dumper(%ARGS), "</pre><br>" if $DEBUG;
    eval{
	%update_info = $ui->form_to_db(%ARGS);
    };
    if ( my $e = $@ ){
	$m->comp('/generic/error.mhtml', error=>$e);
    }

    if ( $update_info{Closet}{id} ) {
        $id = (keys %{$update_info{Closet}{id}})[0];
        $editCloset = 0;
    }

    # Do this to 'flush' the values associated with the object
    # before redisplaying
    $o    = undef;
    if ( $id && $id ne "NEW" ){
        $o = Closet->retrieve($id);
    }
}


# end insertion/update code
# -----------------------------------------------------------------------------
</%perl>

<div id="sectiondetail">

<!-- Closet Table -->
<div class="container">
    <form name="closetform" action="closet.html" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="id" value="<% $id %>">
    <input type="hidden" name="_action" value="1">

    <div class="containerheadleft">
        Closet
    </div>
    <div class="containerheadright">
%       if ($editCloset) {
            <input type="button" name="cancel_button" value="cancel" onClick="history.go(-1);"> &nbsp;&nbsp; <input name="submit" value="save" type="submit">
%       } else {
            &nbsp;
%           if ( $manager && $manager->can($user, 'access_admin_section', 'closet:new') ){
                <a href="closet.html?id=NEW">[new]</a> 
%           }
%           if ( $manager && $manager->can($user, 'edit', $o) ){
                <a href="closet.html?id=<% $id %>&edit=closetinfo">[edit]</a> 
%           }
%           if ( $manager && $manager->can($user, 'delete', $o) ){
                <a href="delete.html?table=Closet&id=<% $id %>">[delete]</a>
%           }
%       }
    </div>
    <div class="containerbody">
        <!-- two column layout table -->
        <table width="100%" border="0" cellpadding="0" cellspacing="0"> 
        <tr>
	  <td>
	    <%perl>
	      (@field_headers, @cell_data) = ();
              {my @fields = ('name','room','dimensions','racks','outlets','shared_with','hvac_type',
			     'ground_buss','asbestos_tiles');
	       my @linkpages = ('', 'view.html', '', '', '', '', '', '', '');
	       $ui->add_to_fields(o=>$o, table=>"Closet", edit=>$editCloset, fields=>\@fields, 
				  field_headers=>\@field_headers, cell_data=>\@cell_data, 
				  linkpages=>\@linkpages,); }
            </%perl>
          <& /generic/attribute_table.mhtml, field_headers=>\@field_headers, data=>\@cell_data &>
	    
            </td>
            <td>

          <%perl>
	      (@field_headers, @cell_data) = ();
          {  my @fields = ('ru_avail','patch_panels','ot_blocks','ss_blocks','catv_taps','access_key_type',
			   'rack_type','pair_count','converted_patch_panels','work_needed');
             $ui->add_to_fields(o=>$o, table=>"Closet", edit=>$editCloset, fields=>\@fields, 
				field_headers=>\@field_headers, cell_data=>\@cell_data); }
	      </%perl>
	      <& /generic/attribute_table.mhtml, field_headers=>\@field_headers, data=>\@cell_data &>
	    
            </td>
        </tr>
      </table>
%      my %comments = $ui->form_field(object=>$o, table=>"Closet", column=>"info", edit=>$editCloset, 
%				      htmlExtra=>"rows='3' cols='80'");
       <div class="containerseparator"><% $comments{label} %></div>
       <% $comments{value} %>
    </div> <!-- close containerbody -->
    </form>
</div> <!-- close containerbody -->
<!-- End Closet Table -->

<!-- Begin Closet Pictures -->
% if (defined($o)) {
<A NAME="pics"></A>
<div class="container">
     <form name="cable_strand_form" action="closet.html" method="POST" enctype="multipart/form-data">
     <input type="hidden" name="id" value="<% $id %>">
%   if (!$editPictures) {
     <input type="hidden" name="_action" value="ADD_PICTURE">
%   } else {
     <input type="hidden" name="_action" value="UPDATE_PICTURES">
%   }

    <div class="containerheadleft">
        Closet Pictures
    </div>
    <div class="containerheadright">
%   if ($editPictures) {	 
        <input type="button" name="cancel_button" value="cancel" onClick="history.go(-1);"> &nbsp;&nbsp; <input name="submit" value="save" type="submit">
%   } else {
%       if ( $manager && $manager->can($user, 'edit', $o) ){
            <a href="closet.html?id=<% $id %>&edit=closetpictures#pics">[edit]</a>
%       }else{
            &nbsp;
% 	}
%   }
    </div>

    <div class="containerbody">

    <%perl>
    my @pictures;
    map { push (@pictures, $_) } $o->pictures;
    </%perl>

%	if (scalar(@pictures)) {

    <%perl>
    (@headers, @rows) = ();

    push( @headers, '<a href="closet.html?id=' . $id . '&pic_sort=filename#pics">File Name</a>' );
    push( @headers, '<a href="closet.html?id=' . $id . '&pic_sort=filesize#pics">Size</a>' );
    push( @headers, '<a href="closet.html?id=' . $id . '&o_sort=filetype#pics">File Type</a>' );
    push( @headers, '<a href="closet.html?id=' . $id . '&strand_sort=info#pics">Comments</a>' );
    </%perl>
    
    <%perl>
    my @sorted_pics;
    $pic_sort = "filename" if (!$pic_sort);
    @sorted_pics = sort { $a->$pic_sort cmp $b->$pic_sort } @pictures;
    </%perl>

    <%perl>
    foreach my $pic (@sorted_pics) {
    my @row = ();
    </%perl>
    <&| "/generic/HERE.mhtml" &>
%       if ( $editPictures ) {
            <input type="checkbox" name="ClosetPicture__<% $pic->id %>__delete">[del]
%           $ui->text_field(object=>$pic, table=>"ClosetPicture", column=>"filename", edit=>$editPictures);
            <br>
%       } else {
            <a href="display_bin.html?table=ClosetPicture&id=<% $pic->id %>" TARGET=_new><% $pic->filename %></a>
%       }
    </&>
    <%perl>push( @row, $_ );
    push( @row, $pic->filesize );
    push( @row, $ui->form_field(object=>$pic, column=>"filetype", edit=>$editPictures, returnValOnly=>1) );
    push( @row, $ui->form_field(object=>$pic, column=>"info", edit=>$editPictures, htmlExtra=>"rows='2' cols='40'", returnValOnly=>1) );
    push( @rows, \@row );
    } # foreach
    </%perl>

    <& /generic/data_table.mhtml, field_headers=>\@headers, data=>\@rows &>

%}

    <br>

%   if (!$editPictures) {
%       if ( $manager && $manager->can($user, 'access_admin_section', 'closet:add_picture') ){
            Add New Picture: &nbsp;<input type="file" name="__picture" size="30" value="">
	        &nbsp;<input type="submit" name="__picture_add" value="Add">
%       }
%   }

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

% }
<!-- End Closet Picture Table -->


<!-- Begin Device List-->
% if ($o && $o->room && $o->room->floor && $o->room->floor->site){
<div class="container">
    <div class="containerheadleft">
        Device List
    </div>

    <div class="containerheadright">
%       if ( $manager && $manager->can($user, 'access_admin_section', 'closet:new_device') ){
            <a href= "../management/device_tasks.html?view=new">[new]</a>
%       }else{
            &nbsp;
%       }
    </div>

    <div class="containerbody">
%       my $site = $o->room->floor->site->id;
%       my $room = $o->room->id;
%       my @device_list = Device->search(site=>$site, room=>$room);
        <& /generic/sortresults.mhtml, object=>\@device_list &>
    </div>
</div>
% }
<!-- End Device List-->



% if (defined($o)) {
<!-- 2-column layout table -->
<table>
<tr>
<td width="50%">
<!-- Begin BackboneCable Table -->
<%perl>
my @sites;
if ($editBackbones) {
    @sites = Site->retrieve_all();
    @sites = sort { $a->name cmp $b->name } @sites;
}
</%perl>

<A NAME="backbones"></A>
    <%perl>
    my @bbones;
    map { push (@bbones, $_) } ($o->backbones_start, $o->backbones_end);
    </%perl>


<div class="container">
    <form name="bbone_form" action="closet.html" method="POST">
    <input type="hidden" name="id" value="<% $id %>">
    <input type="hidden" name="_action" value="UPDATE">

    <div class="containerheadleft">
        Backbone Cable
    </div>
    <div class="containerheadright">
%       if ($editBackbones) {	 
            <input type="button" name="cancel_button" value="cancel" onClick="history.go(-1);"> &nbsp;&nbsp; <input name="submit" value="save" type="submit">
%       } else {
%           if ( $manager && $manager->can($user, 'access_admin_section', 'closet:new_backbone') ){
                <a href="cable_backbone.html?id=NEW&start_closet=<% $o->id %>">[new]</a>
%           }else{
                &nbsp;
%           }
%       }
    </div>
%	if (scalar(@bbones)) {
    <div class="containerbody">

    <%perl>
    (@headers, @rows) = ();
    
    push( @headers, '<a href="closet.html?id=' . $id . '&bb_sort=name#backbones">Name</a>' );
    push( @headers, '<a href="closet.html?id=' . $id . '&bb_sort=type#backbones">Type</a>' );
    push( @headers, '<a href="closet.html?id=' . $id . '&bb_sort=start_closet#backbones">Start Closet</a>' );
    push( @headers, '<a href="closet.html?id=' . $id . '&bb_sort=end_closet#backbones">End Closet</a>' );
    push( @headers, '<a href="closet.html?id=' . $id . '&bb_sort=owner#backbones">Owner</a>' );
    </%perl>

    <%perl>
    $bb_sort = "name" if (!defined($bb_sort) || $bb_sort eq "");
    my @sorted_bbones;
    if ($bb_sort eq "name") {
        @sorted_bbones = sort { $a->name cmp $b->name } @bbones;
    } elsif ($bb_sort eq "start_closet") {
        @sorted_bbones = sort { $a->start_closet->name cmp $b->start_closet->name } @bbones;
    } elsif ($bb_sort eq "end_closet") {
        @sorted_bbones = sort { $a->end_closet->name cmp $b->end_closet->name } @bbones;
    } elsif ($bb_sort eq "owner") {
        @sorted_bbones = sort { $a->owner->name cmp $b->owner->name } @bbones;
    } else {
        @sorted_bbones = sort { $a->$bb_sort cmp $b->$bb_sort } @bbones;
    }
    </%perl>

    <%perl>
    foreach my $bb (@sorted_bbones) {
    my @row = ();
    </%perl>

    <&| "/generic/HERE.mhtml" &>
%       if ($editBackbones) {
            <input type="checkbox" name="<% "BackboneCable__" . $bb->id . "__delete" %>">[del]
%           $ui->text_field(object=>$bb, table=>"BackboneCable", column=>"name",
%                          edit=>$editBackbones);
%       } else {
            <a href="cable_backbone.html?id=<% $bb->id %>"><% $bb->name %></a>
%       }
    </&>
    <%perl>
    push( @row, $_ );
    push( @row, $ui->form_field(object=>$bb, column=>"type", edit=>$editBackbones, returnValOnly=>1) );
    </%perl>

    <&| "/generic/HERE.mhtml" &>
%       if (!$editBackbones) {
            <a href="closet.html?id=<% $bb->start_closet %>"><% $bb->start_closet->get_label() %></a>
%       } else {
            <script language="JavaScript">
            <!--
            var startClosetList_<% $bb->id %> = new DynamicList("bbone_form", "BackboneCable__<% $bb->id %>__start_closet", "site", "Closet", "start_site_list<% $bb->id %>_srch");
            LIST_MANAGER.addList(startClosetList_<% $bb->id %>);
            -->
            </script>
            <SELECT NAME="start_site_list<% $bb->id %>_srch" onChange="startClosetList_<% $bb->id %>.doIt();">
            <OPTION VALUE="">Lookup by Site</OPTION>
            <%perl>
            foreach my $s (@sites)
            {
                printf("<OPTION VALUE=\"%s\">%s</OPTION>\n", $s->id, $s->name);
            }
            </%perl>
            </SELECT> &nbsp;
%           $ui->select_lookup(object=>$bb, table=>"BackboneCable", column=>"start_closet", 
%                             lookup=>"Closet", where=>{site=>($bb ? $bb->start_closet->room->floor->site : "")}, 
%                             edit=>$editBackbones);
%       }
    </&>
    <%perl>
    push( @row, $_ );
    </%perl>

    <&| "/generic/HERE.mhtml" &>
%       if (!$editBackbones) {
            <a href="closet.html?id=<% $bb->end_closet %>"><% $bb->end_closet->get_label() %></a>
%       } else {
            <script language="JavaScript">
            <!--
            var endClosetList_<% $bb->id %> = new DynamicList("bbone_form", "BackboneCable__<% $bb->id %>__end_closet", "site", "Closet", "end_site_list<% $bb->id %>_srch");
            LIST_MANAGER.addList(endClosetList_<% $bb->id %>);
            -->
            </script>
            <SELECT NAME="end_site_list<% $bb->id %>_srch" onChange="endClosetList_<% $bb->id %>.doIt();">
            <OPTION VALUE="">Lookup by Site</OPTION>
            <%perl>
            foreach my $s (@sites)
            {
                printf("<OPTION VALUE=\"%s\">%s</OPTION>\n", $s->id, $s->name);
            }
            </%perl>
            </SELECT> &nbsp;
%           $ui->select_lookup(object=>$bb, table=>"BackboneCable", column=>"end_closet", 
%                             lookup=>"Closet", where=>{site=>($bb ? $bb->end_closet->room->floor->site : "")},
%                             edit=>$editBackbones);
%       }
    </&>
    <%perl>
    push( @row, $_ );
    push( @row, $ui->form_field(object=>$bb, column=>"owner", edit=>$editBackbones, returnValOnly=>1) );
    push( @rows, \@row );
    } #foreach my $bb (@sorted_bbones)
    </%perl>

    <& /generic/data_table.mhtml, field_headers=>\@headers, data=>\@rows &>

    </div> <!-- close containerbody -->
%   }    #if scalar (@bbones)
    </form>
</div> <!-- close container -->
<!-- End BackboneCable Table -->

    </td>   <!-- 2-column layout cells -->
    <td>

<!-- Begin  HorizontalCable Table -->
<A NAME="horizontal"></A>
    <%perl>
    my @cables;
    map { push (@cables, $_) } $o->horizontalcables;
    </%perl>

<div class="container">   
    <form name="cable_form" action="closet.html" method="POST">
    <input type="hidden" name="id" value="<% $id %>">
    <input type="hidden" name="_action" value="UPDATE">

    <div class="containerheadleft">
        Horizontal Cable
    </div>
    <div class="containerheadright">
%   if ($editHorizontal) {
        <input type="button" name="cancel_button" value="cancel" onClick="history.go(-1);"> &nbsp;&nbsp; <input name="submit" value="save" type="submit">
%   } else {
%       if ( $manager && $manager->can($user, 'access_admin_section', 'closet:new_horizontal') ){
            <a href="edit.html?table=HorizontalCable">[new]</a>
%	}else{
            &nbsp;
%       }
%   }
    </div>
%	if (scalar(@cables)) {
    <div class="containerbody">

    <%perl>
    $cable_sort = "jackid" if (!defined($cable_sort) || $cable_sort eq "");
    my @sorted_cables;
    if ($cable_sort eq "jackid") {
        @sorted_cables = sort { $a->jackid cmp $b->jackid } @cables;
    } elsif ($cable_sort eq "type") {
        @sorted_cables = sort { $a->type->name cmp $b->type->name } @cables;
    } elsif ($cable_sort eq "room") {
        @sorted_cables = sort { $a->room->name cmp $b->room->name } @cables;
    } else {
        @sorted_cables = sort { $a->$cable_sort cmp $b->$cable_sort } @cables;
    }
    </%perl>

    <%perl>
    (@headers, @rows) = ();

    push( @headers, '<a href="closet.html?id=' . $id . '&cable_sort=jackid#horizontal">Jack ID</a>' );
    push( @headers, '<a href="closet.html?id=' . $id . '&cable_sort=type#horizontal">Type</a>' );
    push( @headers, '<a href="closet.html?id=' . $id . '&cable_sort=room#horizontal">Room</a>' );

    foreach my $cable (@sorted_cables) {
    my @row = ();
    </%perl>

    <&| "/generic/HERE.mhtml" &>
%       if ($editHorizontal) {
            <input type="checkbox" name="<% "HorizontalCable__" . $cable->id . "__delete" %>">[del]
%           $ui->text_field(object=>$cable, table=>"HorizontalCable", column=>"jackid",
%                          edit=>$editHorizontal);
%       } else {
            <a href="view.html?table=HorizontalCable&id=<% $cable->id %>"><% $cable->jackid %></a>
%       }
    </&>
    <%perl>
    push( @row, $_ );
    push( @row, $ui->form_field(object=>$cable, column=>"type", edit=>$editHorizontal, returnValOnly=>1) );
    push( @row, '<a href="view.html?table=Room&id=' . $cable->room . '">' . $cable->room->name . '</a>' );
    push( @rows, \@row );
    } # foreach my $cable (@sorted_cables)
    </%perl>

    <& /generic/data_table.mhtml, field_headers=>\@headers, data=>\@rows &>

    </div> <!-- close containerbody -->
%  }   #  end if scalar cables
    </form>
</div> <!-- close container -->

<!-- End HorizontalCable Table -->

    </td> <!-- close 2-column layout cell -->
  </tr>
</table> 
<!-- close 2-column layout table -->

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