package SGN::View::Mason::Commands;
use strict;
use vars qw($m $c);
HTML::Mason::Component::FileBased->new(
'code' => sub {
use utf8; local $SGN::View::Mason::Commands::m = $HTML::Mason::Commands::m;
HTML::Mason::Exception::Params->throw
    ( error =>
      "Odd number of parameters passed to component expecting name/value pairs"
    ) if @_ % 2;
my ( $additional_image_button_id, $images, $dbh, $image_objects, $selected_image_id, $stock_id, $stock_image_flag, $curator );
{
    my %pos;
    for ( my $x = 0; $x < @_; $x += 2 )
    {
        $pos{ $_[$x] } = $x + 1;
    }
#line 46 "/home/production/cxgn/sgn/mason/image/print_images.mas"
     $additional_image_button_id = exists $pos{'additional_image_button_id'} ? $_[ $pos{'additional_image_button_id'} ] :  'Images';
#line 47 "/home/production/cxgn/sgn/mason/image/print_images.mas"
     $images = exists $pos{'images'} ? $_[ $pos{'images'} ] :  undef;
#line 48 "/home/production/cxgn/sgn/mason/image/print_images.mas"
     $dbh = exists $pos{'dbh'} ? $_[ $pos{'dbh'} ] :  undef;
#line 49 "/home/production/cxgn/sgn/mason/image/print_images.mas"
     $image_objects = exists $pos{'image_objects'} ? $_[ $pos{'image_objects'} ] :  undef;
#line 50 "/home/production/cxgn/sgn/mason/image/print_images.mas"
     $selected_image_id = exists $pos{'selected_image_id'} ? $_[ $pos{'selected_image_id'} ] :  undef;
#line 51 "/home/production/cxgn/sgn/mason/image/print_images.mas"
     $stock_id = exists $pos{'stock_id'} ? $_[ $pos{'stock_id'} ] :  undef;
#line 52 "/home/production/cxgn/sgn/mason/image/print_images.mas"
     $stock_image_flag = exists $pos{'stock_image_flag'} ? $_[ $pos{'stock_image_flag'} ] :  undef;
#line 53 "/home/production/cxgn/sgn/mason/image/print_images.mas"
     $curator = exists $pos{'curator'} ? $_[ $pos{'curator'} ] :  undef;
}
$m->debug_hook( $m->current_comp->path ) if ( HTML::Mason::Compiler::IN_PERL_DB() );

#line 1 "/home/production/cxgn/sgn/mason/image/print_images.mas"
$m->print( '

' );
#line 43 "/home/production/cxgn/sgn/mason/image/print_images.mas"
$m->print( '
' );
#line 56 "/home/production/cxgn/sgn/mason/image/print_images.mas"
$m->print( '
' );
#line 57 "/home/production/cxgn/sgn/mason/image/print_images.mas"


use CXGN::Page::FormattingHelpers qw / html_optional_show /;
use SGN::Image;

my $image_html     = "";
my $m_image_html   = "";
my $count;
my @more_is;

if ($images && !$image_objects) {
  my @image_object_list = map { SGN::Image->new( $dbh , $_->[0] ) }  @$images ;
  $image_objects = \@image_object_list;
}

###print qq{  <script src="jquery.colorbox-min.js"></script> };

if ($image_objects)  { # don't display anything for empty list of images
  if ($curator && $stock_image_flag == '1') { # Only include 'Set as Display Image' column for images of this stock, not for images of related stock table
    $image_html .= qq|<table class="table table-bordered"><thead><tr><th><button class="btn btn-sm btn-default" id="print_images_multi_image_view">View Selected</button></th><th>Image</th><th>Description</th><th>Type</th><th>Set as Display Image</th></tr></thead><tbody>|;
  } else {
    $image_html .= qq|<table class="table table-bordered"><thead><tr><th><button class="btn btn-sm btn-default" id="print_images_multi_image_view">View Selected</button></th><th>Image</th><th>Description</th><th>Type</th></tr></thead><tbody>|;
  }
  
  my $i = 0;
  foreach my $image_ob (@$image_objects) {
    $count++;
    my $image_id = $image_ob->get_image_id;
    my $image_name        = $image_ob->get_name();
    my $image_description = $image_ob->get_description();
    my $image_img  = $image_ob->get_image_url("medium");
    my $original_img  = $image_ob->get_image_url("large");
    my $small_image = $image_ob->get_image_url("thumbnail");
    my $image_page  = "/image/view/$image_id";

    ##$image_html .=  qq { $(".stock_image_group").colorbox({rel:'stock_image_group'}); };

    my $colorbox =
      qq|<a href="$image_img"  title="<a href=$image_page>Go to image page ($image_name)</a>" class="stock_image_group" rel="gallery-figures"><img src="$small_image" alt="$image_description" /></a> |;
    my $multi_open_colorbox =  qq|<a href="$image_img"  title="<a href=$image_page>Go to image page ($image_name)</a>" class="stock_image_group" rel="gallery-figures"><img src="$original_img" alt="$image_description" /></a> |;
    
    my $set_button = "<button class='btn btn-sm btn-default' onclick='setDisplayImage($image_id, $stock_id)'>Set</button";
    my $fhtml =
      qq|<tr><td><input type="checkbox" name="print_images_checkbox" data-html_text='$multi_open_colorbox'></td><td>|
        . $colorbox
          . $image_name
            . "</td><td>"
              . $image_description
                . "</td><td>"
                .  $images->[$i]->[1]
                .  "</td>"
                .  ($curator == 1 ? "<td>$set_button</td>" : "")
                . "</tr>";
    if ( $count < 3 ) { $image_html .= $fhtml; }
    else {
      push @more_is, $fhtml;
    }    #more than 3 figures- show these in a hidden div
    $i++;
      }
  $image_html .= "</tbody></table>";  #close the table tag or the first 3 figures

  $image_html .= "<script>
  jQuery(document).ready(function() {
      jQuery('a.stock_image_group').colorbox();
      jQuery(document).on('click', '#print_images_multi_image_view', function(){
          var colorbox_html = '';
          jQuery(\"input:checkbox[name='print_images_checkbox']:checked\").each(function(){
              var html = jQuery(this).data('html_text');
              colorbox_html = colorbox_html + html + '<br/>';
          });
          if (colorbox_html != ''){
              jQuery.colorbox({html:colorbox_html});
          }
      });
  }); </script>\n";

}
if ($curator && $stock_image_flag == '1') { # Only include 'Set as Display Image' column for images of this stock, not for images of related stock table
    $m_image_html .= qq|<table class="table table-bordered"><thead><tr><th><button class="btn btn-sm btn-default" id="print_images_multi_image_view">View Selected</button></th><th>Image</th><th>Description</th><th>Type</th><th>Set as Display Image</th></tr></thead><tbody>|; #open table tag for the hidden figures #4 and on
  } else {
    $m_image_html .= qq|<table class="table table-bordered"><thead><tr><th><button class="btn btn-sm btn-default" id="print_images_multi_image_view">View Selected</button></th><th>Image</th><th>Description</th><th>Type</th></tr></thead><tbody>|;
  }
my $more = scalar(@more_is);
foreach (@more_is) { $m_image_html .= $_; }

$m_image_html .= "</tbody></table>";    #close tabletag for the hidden figures

if (@more_is) {    #html_optional_show if there are more than 3 figures
  $image_html .= html_optional_show(
				    $additional_image_button_id,
				    "<b>See $more more images...</b>",
				    qq| $m_image_html |,
				    0, #< do not show by default
				    #'abstract_optional_show', #< don't use the default button-like style
				   );
}

#line 155 "/home/production/cxgn/sgn/mason/image/print_images.mas"
$m->print( '
<div class="modal fade" id="set_display_image_message_dialog" name="set_display_image_message_dialog" tabindex="-1" role="dialog" aria-labelledby="setDisplayMessageDialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="setDisplayMessageDialog">Success</h4>
            </div>
            <div class="modal-body">
                <div class="container-fluid">
                    <p>
                        <span class="ui-icon ui-icon-circle-check" style="float: left; margin: 0 7px 50px 0;"></span>
                        The display image was set successfully.
                    </p>
                </div>
            </div>
            <div class="modal-footer">
                <button id="dismiss_set_display_image_message_dialog" type="button" class="btn btn-default" data-dismiss="modal">Close & Reload</button>
            </div>
        </div>
    </div>
</div>

<script>
    function setDisplayImage(image_id, stock_id) {
      jQuery.ajax({
        url: \'/ajax/stock/set_display_image\',
        dataType: \'json\',
        type: \'POST\',
        data: {
          \'image_id\': image_id,
          \'stock_id\': stock_id
        },
        beforeSend: function(response) {
          jQuery(\'#working_modal\').modal(\'show\');
        },
        success: function(response) {
          jQuery(\'#working_modal\').modal(\'hide\');
          if (response.success == 1) {
            jQuery(\'#set_display_image_message_dialog\').modal(\'show\');
          } 
          if (response.error_string) {
            alert(response.error_string);
          }
        },
        error: function() {
          jQuery(\'#working_modal\').modal(\'hide\');
          alert(\'An error occured while setting the display image\');
        }
      });

      jQuery("#dismiss_set_display_image_message_dialog").click(function(){
        location.reload();
      });

    }
</script>

' );
#line 213 "/home/production/cxgn/sgn/mason/image/print_images.mas"
$m->print(  $image_html  );
#line 213 "/home/production/cxgn/sgn/mason/image/print_images.mas"
$m->print( '
' );
;return;
},
'declared_args' => {
  '$additional_image_button_id' => { default => ' \'Images\'' },
  '$curator' => { default => ' undef' },
  '$dbh' => { default => ' undef' },
  '$image_objects' => { default => ' undef' },
  '$images' => { default => ' undef' },
  '$selected_image_id' => { default => ' undef' },
  '$stock_id' => { default => ' undef' },
  '$stock_image_flag' => { default => ' undef' }
},
'load_time' => 1763079580,

)
;