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 ( $sp_person_autocomplete_uri, $trait_autocomplete_uri, $onto_autocomplete_uri, $trait_db_name, $organisms, $stock_types, $breeding_programs, $editable_stock_props );
{
    my %pos;
    for ( my $x = 0; $x < @_; $x += 2 )
    {
        $pos{ $_[$x] } = $x + 1;
    }
#line 4 "/home/production/cxgn/sgn/mason/search/stocks.mas"
     $sp_person_autocomplete_uri = exists $pos{'sp_person_autocomplete_uri'} ? $_[ $pos{'sp_person_autocomplete_uri'} ] :  '/ajax/people/autocomplete';
#line 5 "/home/production/cxgn/sgn/mason/search/stocks.mas"
     $trait_autocomplete_uri = exists $pos{'trait_autocomplete_uri'} ? $_[ $pos{'trait_autocomplete_uri'} ] :  '/ajax/stock/trait_autocomplete';
#line 6 "/home/production/cxgn/sgn/mason/search/stocks.mas"
     $onto_autocomplete_uri = exists $pos{'onto_autocomplete_uri'} ? $_[ $pos{'onto_autocomplete_uri'} ] :  '/ajax/cvterm/autocomplete';
#line 7 "/home/production/cxgn/sgn/mason/search/stocks.mas"
     $trait_db_name = exists $pos{'trait_db_name'} ? $_[ $pos{'trait_db_name'} ] :  'SP';
#line 9 "/home/production/cxgn/sgn/mason/search/stocks.mas"
     $organisms = exists $pos{'organisms'} ? $_[ $pos{'organisms'} ] :  undef;
#line 10 "/home/production/cxgn/sgn/mason/search/stocks.mas"
     $stock_types = exists $pos{'stock_types'} ? $_[ $pos{'stock_types'} ] :  undef;
#line 11 "/home/production/cxgn/sgn/mason/search/stocks.mas"
     $breeding_programs = exists $pos{'breeding_programs'} ? $_[ $pos{'breeding_programs'} ] :  undef;
#line 12 "/home/production/cxgn/sgn/mason/search/stocks.mas"
     $editable_stock_props = exists $pos{'editable_stock_props'} ? $_[ $pos{'editable_stock_props'} ] :  undef;
}
$m->debug_hook( $m->current_comp->path ) if ( HTML::Mason::Compiler::IN_PERL_DB() );

#line 1 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( '
' );
#line 14 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( '
' );
#line 15 "/home/production/cxgn/sgn/mason/search/stocks.mas"

  my $user_role = '';
  if ($c->user) {
    $user_role = $c->user->get_object()->get_user_type();
  }
#line 21 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( '
' );
#line 22 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->comp(   '/util/import_javascript.mas', classes => [ 'jquery', 'jquery.dataTables', 'CXGN.Effects', 'CXGN.List' ]   
); #line 22 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( '

<div class="container-fluid">

' );
#line 26 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->comp(   '/page/page_title.mas', title=>'Search Accessions and Plots'   
); #line 26 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( '


' );
#line 29 "/home/production/cxgn/sgn/mason/search/stocks.mas"


use CXGN::Page::FormattingHelpers qw / conditional_like_input_html simple_selectbox_html/;

my $accession_cvterm_id;

for (my $i=0; $i<= scalar(@$stock_types); $i++) {
     if ( $stock_types->[$i][1] eq "accession" ) {
     	$accession_cvterm_id = $stock_types->[$i][0];
	last();
     }
}

my $stock_type_select = simple_selectbox_html(
  choices   => $stock_types,
  id        => "stock_type_select",
  selected => $accession_cvterm_id
);

my $organism_select = simple_selectbox_html(
  choices  =>  $organisms   ,
  id       => "organism_select",
);

my $breeding_programs_select = simple_selectbox_html(
    choices => $breeding_programs,
    id      => "breeding_program" ,
);

#line 59 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( '
' );
#line 60 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->comp( { content => sub {
#line 60 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( '

  <div id="stock_search_form" class="well well-sm">

    ' );
#line 64 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->comp( { content => sub {
#line 64 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( '

    <div class="row">
      <div class="col-sm-11">
        <div class="form-group form-group-sm">
      	  <label class="col-sm-3 control-label">Stock Name or Description: </label>
          <div class="col-sm-9" >
            <div class="form-group form-group-sm">
              <div class="input-group">
                <span class="input-group-btn" width="20%">
                  <select class="form-control" id="any_name_matchtype" name="any_name_matchtype">
                    <option title="starts_with" value="starts_with" selected="selected">starts with</option>
                    <option title="contains" value="contains">contains</option>
                    <option title="ends with" value="ends_with">ends with</option>
                    <option title="exactly" value="exactly">exactly</option>
                  </select>
                </span>
                  <span class="input-group-btn">
                  <input class="form-control" name="any_name" id="any_name" value="" size="30" type="text" placeholder="Type search here..."/>
                </span>
              </div>
            </div>
            <div class="form-group form-group-sm">
              <label class="col-sm-3 control-label">Stock Type: </label>
              <div class="col-sm-9" >
                ' );
#line 89 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print(  $stock_type_select  );
#line 89 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( '
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    ' );
#line 96 "/home/production/cxgn/sgn/mason/search/stocks.mas"
} }, '/page/info_section.mas', title => 'Name Search', collapsible=>1, collapsed=>0
 );
#line 96 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( '

    ' );
#line 98 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->comp( { content => sub {
#line 98 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( '

    ' );
#line 100 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->comp( { content => sub {
#line 100 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( '

    <div class="row">
      <div class="form-group form-group-sm">
        <label class="col-sm-4 control-label">Organism:</label>
        <div class="col-sm-8" >
          ' );
#line 106 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print(  $organism_select  );
#line 106 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( '
        </div>
      </div>
    </div>

    <br />

    <div class="row">
      <div class="form-group form-group-sm">
        <label class="col-sm-4 control-label">Organization:</label>
        <div class="col-sm-8" >
          <input type="text" class="form-control" id="organization" placeholder="Type to Autocomplete"/>
        </div>
      </div>
    </div>

    <br /><hr /><br />

    <div class="row">
      <div class="form-group form-group-sm">
        <label class="col-sm-4 control-label">Search By Another Property:</label>
        <div class="col-sm-8" >
          <div class="form-group">
            <div class="input-group">
              <span class="input-group-btn">
                <select class="form-control" id="editable_stockprop_search_term" name="editable_stockprop_search_term">
' );
#line 132 "/home/production/cxgn/sgn/mason/search/stocks.mas"
  foreach my $stockprop (sort {uc($a) cmp uc($b)} @$editable_stock_props){
$m->print( '                  <option title="' );
#line 133 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print(  $stockprop  );
#line 133 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( '" value="' );
#line 133 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print(  $stockprop  );
#line 133 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( '" >' );
#line 133 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print(  $stockprop  );
#line 133 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( '</option>
' );
#line 134 "/home/production/cxgn/sgn/mason/search/stocks.mas"
  }
$m->print( '                </select>
              </span>
              <span class="input-group-btn">
                <button class="btn btn-primary btn-sm" id="editable_stockprop_search_add">Add</button>
              </span>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div id="editable_stockprop_search_div"></div>

' );
#line 147 "/home/production/cxgn/sgn/mason/search/stocks.mas"
 if ( $user_role eq 'curator' ) {
$m->print( '    <br /><hr /><br />
    <div class="row">
      <div class="form-group form-group-sm">
        <label class="col-sm-4 control-label">Is Obsolete?:</label>
        <div class="col-sm-8" >
          <div class="form-group">
            <select class="form-control" id="obsolete_stock_search_term" name="obsolete_stock_search_term">
              <option value="false" selected>Not Obsolete</option>
              <option value="true">Is Obsolete</option>
            </select>
          </div>
        </div>
      </div>
    </div>
' );
#line 162 "/home/production/cxgn/sgn/mason/search/stocks.mas"
 }
$m->print( '
    ' );
#line 164 "/home/production/cxgn/sgn/mason/search/stocks.mas"
} }, '/page/info_section.mas', title => 'Properties', collapsible=>1, collapsed=>1
 );
#line 164 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( '
    ' );
#line 165 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->comp( { content => sub {
#line 165 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( '

    <div class="row">
      <div class="col-sm-6">
        <div class="form-group form-group-sm">
          <label class="col-sm-3 control-label">Project Name: </label>
          <div class="col-sm-9" >
            <input type="text" class="form-control" id="project" placeholder="Type to Autocomplete"/>
          </div>
        </div>
      </div>
      <div class="col-sm-6">
        <div class="form-group form-group-sm">
          <label class="col-sm-3 control-label">Project Location: </label>
          <div class="col-sm-9" >
            <input type="text" class="form-control" id="location" placeholder="Type to Autocomplete"/>
          </div>
        </div>
      </div>
    </div>
    <div class="row">
      <div class="col-sm-6">
        <div class="form-group form-group-sm">
          <label class="col-sm-3 control-label">Project Year: </label>
          <div class="col-sm-9" >
            <input type="text" class="form-control" id="year" placeholder="Type to Autocomplete"/>
          </div>
        </div>
      </div>
      <div class="col-sm-6">
        <div class="form-group form-group-sm">
          <label class="col-sm-3 control-label">Breeding Program: </label>
          <div class="col-sm-9" >
            ' );
#line 198 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print(  $breeding_programs_select  );
#line 198 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( '
          </div>
        </div>
      </div>
    </div>

    ' );
#line 204 "/home/production/cxgn/sgn/mason/search/stocks.mas"
} }, '/page/info_section.mas', title => 'Usage', collapsible=>1, collapsed=>1
 );
#line 204 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( '
    ' );
#line 205 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->comp( { content => sub {
#line 205 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( '

    <div class="row">
        <div class="col-sm-6">
          <div class="form-group form-group-sm">
            <label class="col-sm-3 control-label">Trait: </label>
            <div class="col-sm-9" >
              <input type="text" class="form-control" id="trait" placeholder="Type to Autocomplete"/>
            </div>
          </div>
        </div>
      <div class="col-sm-6">
        <div class="form-group form-group-sm">
          <label class="col-sm-3 control-label">Minimum Trait Value: </label>
          <div class="col-sm-9" >
            <input type="text" class="form-control" id="minimum_trait_value" />
          </div>
        </div>
      </div>
      </div>
      <div class="row">
      <div class="col-sm-6">
        <div class="form-group form-group-sm">
          <label class="col-sm-3 control-label">Maximum Trait Value: </label>
          <div class="col-sm-9" >
            <input type="text" class="form-control" id="maximum_trait_value" />
          </div>
        </div>
      </div>
    </div>
    ' );
#line 235 "/home/production/cxgn/sgn/mason/search/stocks.mas"
} }, '/page/info_section.mas', title => 'Phenotypes', collapsible=>1, collapsed=>1
 );
#line 235 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( '

    ' );
#line 237 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->comp( { content => sub {
#line 237 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( '
        <div class="row">
            <a href="/search/stocks_using_genotypes"><p>Go to Search Accessions Using Genotypes Page.</p></a>
        </div>
    ' );
#line 241 "/home/production/cxgn/sgn/mason/search/stocks.mas"
} }, '/page/info_section.mas', title => 'Search Accessions Using Genotypes', collapsible=>1, collapsed=>1
 );
#line 241 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( '

    ' );
#line 243 "/home/production/cxgn/sgn/mason/search/stocks.mas"
} }, '/page/info_section.mas', title => 'Advanced Search', collapsible=>1, collapsed=>1, id=>'advanced_search_panel'
 );
#line 243 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( ' <!-- closes advanced search -->

    <center>
    <button class="btn btn-primary" id="submit_stock_search" />Search</button>
    </center>
    <br/><br/>
  </div>
' );
#line 250 "/home/production/cxgn/sgn/mason/search/stocks.mas"
} }, '/page/info_section.mas', title => 'Search', collapsible=>1, collapsed=>0
 );
#line 250 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( '

<br />

' );
#line 254 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->comp( { content => sub {
#line 254 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( '

<div class="well well-sm">

    <div class="row">
      <div class="col-sm-11">
        <div class="form-group form-group-sm">
          <label class="col-sm-4 control-label">View Another Property: </label>
          <div class="col-sm-8" >
            <div class="form-group">
              <div class="input-group">
                <span class="input-group-btn">
                  <select class="form-control" id="editable_stockprop_view_column_term" name="editable_stockprop_view_column_term">
' );
#line 267 "/home/production/cxgn/sgn/mason/search/stocks.mas"
  foreach my $stockprop (sort { "\L$a" cmp "\L$b" } @$editable_stock_props){
$m->print( '                    <option title="' );
#line 268 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print(  $stockprop  );
#line 268 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( '" value="' );
#line 268 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print(  $stockprop  );
#line 268 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( '" >' );
#line 268 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print(  $stockprop  );
#line 268 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( '</option>
' );
#line 269 "/home/production/cxgn/sgn/mason/search/stocks.mas"
  }
$m->print( '                  </select>
                </span>
                  <span class="input-group-btn">
                    <button class="btn btn-primary btn-sm" id="editable_stockprop_view_column_add">Add</button>
                </span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="panel panel-default">
        <div class="panel-body">
            <div id="stock_search_results_div">
                <table id="stock_search_results" width="100%" class="table table-hover table-striped">
                <thead>
                  <tr>
                    <th>Stock Id</th>
                    <th>Stock Name</th>
                    <th>Stock Type</th>
                    <th>Organism</th>
                    <th>Synonyms</th>
                    <th>Organization</th>
                </tr>
                </thead>

                </table>
            </div>
        </div>
    </div>

    <div class="panel panel-default">
        <div class="panel-body">
            ' );
#line 304 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->comp( { content => sub {
#line 304 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( '
            <br>
            <div style="text-align:right" id="results_to_list_menu"></div>
            <div id="search_result_names" style="display: none;"></div>
            ' );
#line 308 "/home/production/cxgn/sgn/mason/search/stocks.mas"
} }, '/page/info_section.mas', title => 'Copy Results to a List', collapsible=>1, collapsed=>0, subtitle=>'<i>Copy the stock names currently showing in the search results table to a new or exisiting list. Use "Show All entries" to capture all results.</i>'
 );
#line 308 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( '
        </div>
    </div>

</div>

' );
#line 314 "/home/production/cxgn/sgn/mason/search/stocks.mas"
} }, '/page/info_section.mas', title => 'Search Results', collapsible=>1, collapsed=>0
 );
#line 314 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( '

' );
#line 316 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->comp(   '/page/detail_page_2_col_section.mas', info_section_title => "<h4 style='display:inline'>Graphical Filtering on Phenotype Performance</h4>", info_section_subtitle => 'Graphically filter an accessions average performance for combinations of traits', icon_class => "glyphicon glyphicon-equalizer", info_section_id => "stocks_search_graphical_filtering"   
); #line 316 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( '


</div>

<script>

var stock_table;

jQuery(document).ready(function () {

    window.addEventListener(\'load\', () => {
      // Reset the advanced search form parameters on load.
      // Very similar to later functionality but that one is for advanced search and on load trigger only.
      var input_list = document.getElementById(\'advanced_search_panel_content\').getElementsByTagName(\'input\');
      for (let input_elem of input_list) {
        input_elem.value = \'\';
      }

      var select_list = document.getElementById(\'advanced_search_panel_content\').getElementsByTagName(\'select\');
      for (let select_elem of select_list) {
        select_elem.selectedIndex = 0;
      }

      var type_select = document.getElementById(\'stock_type_select\');
      type_select.selectedIndex = [...type_select.options].findIndex(elem => elem.text.trim().toLowerCase() == \'accession\') ?? 1;
    });


    const textFields = document.querySelectorAll(\'input[type="text"]\');
    // Loop through the text fields and set their values to an empty string
      textFields.forEach(textField => {
        textField.value = \'\';
     });

     // Get all the dropdown select elements in the form
     // Reset all but \'stock_type_select\' dropdown to zero value
     // Leave \'stock_type_select\' with the default "selected" value from the mason file.
     const selectFields = document.querySelectorAll(\'select\');
     selectFields.forEach(selectField => {
        if (selectField.id != \'stock_type_select\') {
          selectField.selectedIndex = 0;
        }
     });

     jQuery("#person").autocomplete({
        source: \'' );
#line 362 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print(  $sp_person_autocomplete_uri  );
#line 362 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( '\'
     });
     jQuery("#trait").autocomplete({
        source: \'' );
#line 365 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print(  $trait_autocomplete_uri  );
#line 365 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( '\'
     });
     jQuery("#onto").autocomplete({
        source: \'' );
#line 368 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print(  $onto_autocomplete_uri  );
#line 368 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( '\' + "?db_name=" + \'' );
#line 368 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print(  $trait_db_name  );
#line 368 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( '\'
     });
     jQuery("#project").autocomplete({
        source: \'/ajax/stock/project_autocomplete\',
     });
     jQuery("#any_name").autocomplete({
        source: \'/ajax/stock/stock_autocomplete?stock_type_id=\'+jQuery(\'#stock_type_select option:selected\').val(),
     });
     jQuery(\'#stock_type_select\').change(function(){
         jQuery("#any_name").autocomplete({
            source: \'/ajax/stock/stock_autocomplete?stock_type_id=\'+jQuery(\'#stock_type_select option:selected\').val(),
         });
     });
     jQuery("#location").autocomplete({
        source: \'/ajax/stock/geolocation_autocomplete\',
     });
     jQuery("#year").autocomplete({
        source: \'/ajax/stock/project_year_autocomplete\',
     });
     jQuery("#organization").autocomplete({
        source: \'/ajax/stock/stockproperty_autocomplete?property=organization\',
     });

    // Update changed organization
    jQuery(document).on("change", "#organization", function() {
      editable_stockprops_search["organization"] = {
        "matchtype": "contains",
        "value": jQuery(this).val()
      }
    });

    var editable_stockprops_search = {};
    var params = {};
    var matchtype = {};
    jQuery(\'#editable_stockprop_search_add\').click( function(){
        // console.log("editable stockprops search is "+JSON.stringify(editable_stockprops_search));
        // console.log("Value is "+jQuery(\'#editable_stockprop_search_term\').val());
        editable_stockprops_search[jQuery(\'#editable_stockprop_search_term\').val()] = {"matchtype": "contains"};
        console.log("editable stockprops search is "+JSON.stringify(editable_stockprops_search));
        _draw_editable_stockprops_search_fields();
    });

    jQuery(document).on(\'click\', \'button[name="editable_stockprop_search_remove"]\', function(){
        delete editable_stockprops_search[jQuery(this).data(\'property\')];
        _draw_editable_stockprops_search_fields();
    });

    jQuery(document).on("keyup", \'input[name="editable_stockprop_search_inputs"]\', function(){
        //Store typed value in object, in case of redraw inputs (input deletion or addition) the value will be still there. Also for search submission
        editable_stockprops_search[jQuery(this).data(\'property\')]["value"] = jQuery(this).val();
        console.log("editable stockprops search is "+JSON.stringify(editable_stockprops_search));
    });

    jQuery(document).on("change", \'select[name="editable_stockprop_matchtype"]\', function(){

    // jQuery(document).on("change", \'input[name="editable_stockprop_matchtype"]\', function(){
        //Store typed value in object, in case of redraw inputs (input deletion or addition) the value will be still there. Also for search submission
        console.log("prop is "+jQuery(this).data(\'property\'));
        editable_stockprops_search[jQuery(this).data(\'property\')]["matchtype"] = jQuery(this).val();
        console.log("editable stockprops search is "+JSON.stringify(editable_stockprops_search));
    });

    function _draw_editable_stockprops_search_fields(){
        jQuery(\'#editable_stockprop_search_div\').html(\'\');
        for (var property in editable_stockprops_search) {
            if (editable_stockprops_search.hasOwnProperty(property)) {
                jQuery(\'#editable_stockprop_search_div\').append(\'<div class="form-group form-group-sm"><label class="col-sm-4 control-label">\'+property+\': </label><div class="col-sm-8"><div class="form-group"><div class="input-group"><span class="input-group-btn" width="20%"><select class="form-control" id="editable_stockprop_matchtype" name="editable_stockprop_matchtype" data-property="\'+property+\'"><option title="contains" value="contains" selected="selected">contains</option><option title="starts with" value="starts_with">starts with</option><option title="ends with" value="ends_with">ends with</option><option title="exactly" value="exactly">exactly</option><option title="one of" value="one of">one of (separate multiple possibilities with commas)</option></select></span><span class="input-group-btn"><input type="text" class="form-control" name="editable_stockprop_search_inputs" id="\'+property+\'_input_id" data-property="\'+property+\'" placeholder="Type to Autocomplete"/></span><span class="input-group-btn"><button name="editable_stockprop_search_remove" data-property="\'+property+\'" class="btn"><span class="glyphicon glyphicon-remove"></span></button></span></div></div></div></div>\');

                jQuery(\'#editable_stockprop_search_div\').find("#"+property+"_input_id").autocomplete({
                   source: \'/ajax/stock/stockproperty_autocomplete?property=\'+property,
                });
            }
        }
    }

    //For adding stockprop columns to search result
    var stockprop_extra_columns_view = { \'organization\':1 };
    var stockprop_extra_columns_view_array = [ \'organization\' ];
    jQuery(\'#editable_stockprop_view_column_add\').click(function(){
        var selected_property = jQuery(\'#editable_stockprop_view_column_term\').val();
        if (!(selected_property in stockprop_extra_columns_view)){
            stockprop_extra_columns_view[selected_property] = 1;
            stockprop_extra_columns_view_array.push(selected_property);

            var table_header_html =
              \'<table id="stock_search_results" class="table table-hover table-striped" width="100%">\' +
                \'<thead><tr>\' +
                  \'<th>Stock Id</th>\' +
                  \'<th>Stock Name</th>\' +
                  \'<th>Stock Type</th>\' +
                  \'<th>Organism</th>\' +
                  \'<th>Synonyms</th>\';
            for (var i = 0; i < stockprop_extra_columns_view_array.length; i++) {
              table_header_html += \'<th>\' + stockprop_extra_columns_view_array[i] + \'</th>\';
            }
            table_header_html +=
                \'</tr></thead>\' +
              \'</table>\';
            jQuery(\'#stock_search_results_div\').html(table_header_html);
        }
        stock_table.destroy();
        _load_stock_search_results(jQuery(\'#stock_type_select option:selected\').text(), editable_stockprops_search, stockprop_extra_columns_view, stockprop_extra_columns_view_array);
    });

    parseArgs(editable_stockprops_search);

    jQuery(\'#stock_search_results\').on( \'draw.dt\', function () {
        const name_links = stock_table.column(1).data();
        let names = [];
        for (let i = 0; i < name_links.length; i++) { //extract text from anchor tags
            names.push(name_links[i].match(/<a [^>]+>([^<]+)<\\/a>/)[1]+\'\\n\');
        }

        let is_obsolete = \'\';
        is_obsolete = jQuery("#obsolete_stock_search_term option:selected").val();

        let  list_type = \'\';
        if (is_obsolete == \'true\') {
            list_type = \'obsoleted_stocks\';
        } else {
            list_type = jQuery(\'#stock_type_select option:selected\').text()+\'s\' || \'null\';
        }

        jQuery(\'#search_result_names\').html(names);
        addToListMenu(\'results_to_list_menu\', \'search_result_names\', {
            listType: list_type
        });
    });

   jQuery(\'#submit_stock_search\').click( function() {
        //stock_table.search("stock_search_results").draw();
        _load_stock_search_results(jQuery(\'#stock_type_select option:selected\').text(), editable_stockprops_search, stockprop_extra_columns_view, stockprop_extra_columns_view_array);
   });

   jQuery(\'#stock_search_form\').keypress( function( e ) {
           var code = e.keyCode || e.which;
           if( code == 13 ) {
                jQuery(\'#submit_stock_search\').click();
           }
    } );


    // For graphical filtering, using same search params
    var run = false;
    jQuery(\'#stock_search_graphical_filter_onswitch\').on("click", function(){
        var stock_type = jQuery(\'#stock_type_select option:selected\').text();
        if (stock_type == \'accession\'){
            jQuery.ajax ({
                url : \'/ajax/search/stocks\',
                data : {
                    minimal_info : 1,
                    any_name : jQuery(\'#any_name\').val(),
                    any_name_matchtype : jQuery(\'#any_name_matchtype\').val(),
                    stock_type : jQuery(\'#stock_type_select option:selected\').text(),
                    organism : jQuery(\'#organism_select\').val(),
                    person : jQuery(\'#person\').val(),
                    trait : jQuery(\'#trait\').val(),
                    minimum_trait_value : jQuery(\'#minimum_trait_value\').val(),
                    maximum_trait_value : jQuery(\'#maximum_trait_value\').val(),
                    project : jQuery(\'#project\').val(),
                    location : jQuery(\'#location\').val(),
                    year : jQuery(\'#year\').val(),
                    breeding_program : jQuery(\'#breeding_program\').val(),
                    editable_stockprop_values : JSON.stringify(editable_stockprops_search)
                },
                beforeSend: function() {
                    jQuery("#working_modal").modal("show");
                    jQuery(\'#stock_search_graphical_filter_div\').html("[LOADING...]");
                },
                success: function(response){
                    //console.log(response);
                    jQuery("#working_modal").modal("hide");
                    var stock_ids = [];
                    for (var i=0; i<response.data.length; i++){
                        stock_ids.push(response.data[i][0]);
                    }

                    if (run) {
                        jQuery("#stock_search_graphical_filter_results_div").DataTable().destroy();
                        jQuery("#stock_search_graphical_filter_results_div").html("");
                    }
                    run = true;
                    params = {"germplasmDbIds" : stock_ids, "observationLevel" : \'plot\'};
                    loadBrAPIData("' );
#line 551 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print(  $c->config->{main_production_site_url}  );
#line 551 "/home/production/cxgn/sgn/mason/search/stocks.mas"
$m->print( '",params,function(response){
                        useBrAPIData(response,true);
                    });

                },
                error: function(response){
                    jQuery("#working_modal").modal("hide");
                    alert("Error retrieving accessions in graphical filtering.");
                }
            });
        } else {
            jQuery(\'#stock_search_graphical_filter_div\').html("Graphical Filtering only configured for accessions. Currently searching across "+stock_type+". Change your search to accesions to use graphical filtering.");
        }
    });


});

function parseArgs(editable_stockprops_search) {
  const params = new URLSearchParams(window.location.search);
  if ( params.has(\'any_name\') ) {
    jQuery(\'#any_name\').val(params.get(\'any_name\'));
    jQuery(\'#stock_type_select\').val(0);
  }
  if ( params.has(\'stock_type\') ) {
    var type = params.get(\'stock_type\');
    var value =  jQuery("#stock_type_select option[title="+ type +"]").val();
    jQuery(\'#stock_type_select\').val(value);

  }
  if ( params.has(\'prop\') & params.has(\'value\') ) {
    editable_stockprops_search[params.get(\'prop\')] = {"matchtype": "exactly"};
    editable_stockprops_search[params.get(\'prop\')]["value"] = params.get(\'value\');
  }
}

function _load_stock_search_results(stock_type, editable_stockprops_search, stockprop_extra_columns_view, stockprop_extra_columns_view_array){
    console.log(stock_type);
    console.log("Making AJAX request with editable stockprop "+JSON.stringify(editable_stockprops_search));
    stock_table = jQuery(\'#stock_search_results\').DataTable({
        \'destroy\' : true,
        \'searching\' : false,
        \'ordering\'  : false,
        \'processing\': true,
        \'serverSide\': true,
        \'lengthMenu\': [
          [10,20,50,100,1000,10000,1000000000000000],
          [10,20,50,100,1000,10000,\'All\']
        ],
        \'iDisplayLength\': 10000,
        \'fixedFooter\' : true,
        \'scrollY\' : \'350px\',
        \'ajax\': { \'url\':  \'/ajax/search/stocks\',
            \'data\': function(d) {
              d.any_name  = jQuery(\'#any_name\').val();
              d.any_name_matchtype = jQuery(\'#any_name_matchtype\').val();
              d.stock_type   = stock_type;
              d.organism     = jQuery(\'#organism_select\').val();
              d.person       = jQuery(\'#person\').val();
              d.trait        = jQuery(\'#trait\').val();
              d.minimum_trait_value        = jQuery(\'#minimum_trait_value\').val();
              d.maximum_trait_value        = jQuery(\'#maximum_trait_value\').val();
              d.project      = jQuery(\'#project\').val();
              d.location     = jQuery(\'#location\').val();
              d.year         = jQuery(\'#year\').val();
              d.breeding_program = jQuery(\'#breeding_program\').val();
              d.editable_stockprop_values = JSON.stringify(editable_stockprops_search);
              d.extra_stockprop_columns_view = JSON.stringify(stockprop_extra_columns_view);
              d.stockprop_extra_columns_view_array = JSON.stringify(stockprop_extra_columns_view_array);
              d.is_obsolete = jQuery("#obsolete_stock_search_term option:selected").val()
            }
        }
    });
}

</script>
' );
;return;
},
'declared_args' => {
  '$breeding_programs' => { default => ' undef' },
  '$editable_stock_props' => { default => ' undef' },
  '$onto_autocomplete_uri' => { default => ' \'/ajax/cvterm/autocomplete\'' },
  '$organisms' => { default => ' undef' },
  '$sp_person_autocomplete_uri' => { default => ' \'/ajax/people/autocomplete\'' },
  '$stock_types' => { default => ' undef' },
  '$trait_autocomplete_uri' => { default => ' \'/ajax/stock/trait_autocomplete\'' },
  '$trait_db_name' => { default => ' \'SP\'' }
},
'load_time' => 1763116632,

)
;