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 ( $trial_id, $trial_name, $trial_stock_type );
{
    my %pos;
    for ( my $x = 0; $x < @_; $x += 2 )
    {
        $pos{ $_[$x] } = $x + 1;
    }
#line 3 "/home/production/cxgn/sgn/mason/breeders_toolbox/trial/create_spreadsheet_dialog.mas"
     $trial_id = exists $pos{'trial_id'} ? $_[ $pos{'trial_id'} ] :  undef;
#line 4 "/home/production/cxgn/sgn/mason/breeders_toolbox/trial/create_spreadsheet_dialog.mas"
     $trial_name = exists $pos{'trial_name'} ? $_[ $pos{'trial_name'} ] :  undef;
#line 5 "/home/production/cxgn/sgn/mason/breeders_toolbox/trial/create_spreadsheet_dialog.mas"
     $trial_stock_type = exists $pos{'trial_stock_type'} ? $_[ $pos{'trial_stock_type'} ] :  undef;
}
$m->debug_hook( $m->current_comp->path ) if ( HTML::Mason::Compiler::IN_PERL_DB() );

#line 1 "/home/production/cxgn/sgn/mason/breeders_toolbox/trial/create_spreadsheet_dialog.mas"
$m->print( '
' );
#line 7 "/home/production/cxgn/sgn/mason/breeders_toolbox/trial/create_spreadsheet_dialog.mas"
$m->print( '

<div class="modal fade" id="create_spreadsheet_dialog" name="create_spreadsheet_dialog" tabindex="-1" role="dialog" aria-labelledby="createSpreadsheetDialog">
  <div class="modal-dialog modal-lg" 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="createSpreadsheetDialog">Download Phenotype Spreadsheet for ' );
#line 14 "/home/production/cxgn/sgn/mason/breeders_toolbox/trial/create_spreadsheet_dialog.mas"
$m->print(  $trial_name  );
#line 14 "/home/production/cxgn/sgn/mason/breeders_toolbox/trial/create_spreadsheet_dialog.mas"
$m->print( '</h4>
      </div>
      <div class="modal-body">
        <div class="container-fluid">

            <form class="form-horizontal" role="form" method="post" >

                <div class="form-group">
		     
                    <label class="col-sm-3 control-label">Trial: </label>
                    <div class="col-sm-9">
                        <div id="select_trial_for_create_spreadsheet" >
' );
#line 26 "/home/production/cxgn/sgn/mason/breeders_toolbox/trial/create_spreadsheet_dialog.mas"
 if ($trial_id) {
$m->print( '                            <input type="text" id="input_for_trial_name_disabled" class="form-control" style="width:400px" value="' );
#line 27 "/home/production/cxgn/sgn/mason/breeders_toolbox/trial/create_spreadsheet_dialog.mas"
$m->print(  $trial_name  );
#line 27 "/home/production/cxgn/sgn/mason/breeders_toolbox/trial/create_spreadsheet_dialog.mas"
$m->print( '" text="' );
#line 27 "/home/production/cxgn/sgn/mason/breeders_toolbox/trial/create_spreadsheet_dialog.mas"
$m->print(  $trial_name  );
#line 27 "/home/production/cxgn/sgn/mason/breeders_toolbox/trial/create_spreadsheet_dialog.mas"
$m->print( '" disabled />
                            <input type="hidden" id="html_select_trial_for_create_spreadsheet" name="html_select_trial_for_create_spreadsheet" value="' );
#line 28 "/home/production/cxgn/sgn/mason/breeders_toolbox/trial/create_spreadsheet_dialog.mas"
$m->print(  $trial_id  );
#line 28 "/home/production/cxgn/sgn/mason/breeders_toolbox/trial/create_spreadsheet_dialog.mas"
$m->print( '" >
' );
#line 29 "/home/production/cxgn/sgn/mason/breeders_toolbox/trial/create_spreadsheet_dialog.mas"
 } else {
$m->print( '                        [Loading...]
' );
#line 31 "/home/production/cxgn/sgn/mason/breeders_toolbox/trial/create_spreadsheet_dialog.mas"
 }
$m->print( '                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label">Trait List: </label>
                    <div class="col-sm-9" >
                        <div id="trait_list_spreadsheet">
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label">Include Notes Column </label>
                    <div class="col-sm-9" >
                        <input type="checkbox" id="include_notes_column" name="include_notes_column" />
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label">Spreadsheet Format: </label>
                    <div class="col-sm-9" >
                        <select class="form-control" id="create_spreadsheet_phenotype_file_format" name="create_spreadsheet_phenotype_file_format">
                            <option value="ExcelBasic">Detailed</option>
                            <option value="ExcelBasicSimple">Simple</option>
                            <option value="ExcelAssociatedImages">Associated Images</option>
                        </select>
                    </div>
                </div>
                <div id="create_spreadsheet_phenotype_data_level_div">
                    <div class="form-group">
                        <label class="col-sm-3 control-label">Data Level: </label>
                        <div class="col-sm-9" >
                            <select class="form-control" id="create_spreadsheet_data_level">
                                <option value="plots">Plots</option>
                                <option value="plants">Plants</option>
                                <option value="subplots">Subplots</option>
                                <option value="plants_subplots">Plants and Subplots</option>
                                <option value="tissue_samples">Tissue Samples</option>
                            </select>
                        </div>
                    </div>
                </div>

            <div id="create_spreadsheet_plant_options" style="display:none">
                <hr>
                <div class="form-group">
                    <label class="col-sm-5 control-label "><small>Sample Number: </small></label>
                    <div class="col-sm-7" >
                        <input type="text" class="form-control" id="create_spreadsheet_sample_number" placeholder="Leave blank for all"/>
                    </div>
                </div>
            </div>

        </form><br/>

        </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" name="create_phenotyping_cancel_button" id="create_phenotyping_cancel_button" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary" name="create_phenotyping_ok_button" id="create_phenotyping_ok_button" title="Submit">Submit</button>
      </div>
    </div>
  </div>
</div>

<script>

jQuery(document).ready(function() {

    if(jQuery("#html_select_trial_for_create_spreadsheet").length == 0) {
        get_select_box(\'trials\', \'select_trial_for_create_spreadsheet\', { \'name\' : \'html_select_trial_for_create_spreadsheet\', \'id\' : \'html_select_trial_for_create_spreadsheet\', \'multiple\':1, \'size\':10 });
    }

    jQuery("[name=\'create_spreadsheet_link\']").click( function () {
        jQuery(\'#create_spreadsheet_dialog\').modal("show");
        var list = new CXGN.List();
        jQuery("#trait_list_spreadsheet").html(list.listSelect("trait_list_spreadsheet", [ \'traits\' ], undefined, undefined, undefined));
    });

    jQuery(document).on(\'change\', \'#create_spreadsheet_data_level\', function(){
' );
#line 110 "/home/production/cxgn/sgn/mason/breeders_toolbox/trial/create_spreadsheet_dialog.mas"
  if ($trial_id){
$m->print( '        var selected_trial_ids = [' );
#line 111 "/home/production/cxgn/sgn/mason/breeders_toolbox/trial/create_spreadsheet_dialog.mas"
$m->print(  $trial_id  );
#line 111 "/home/production/cxgn/sgn/mason/breeders_toolbox/trial/create_spreadsheet_dialog.mas"
$m->print( '];
        var selected_trial_names = ["' );
#line 112 "/home/production/cxgn/sgn/mason/breeders_toolbox/trial/create_spreadsheet_dialog.mas"
$m->print(  $trial_name  );
#line 112 "/home/production/cxgn/sgn/mason/breeders_toolbox/trial/create_spreadsheet_dialog.mas"
$m->print( '"];
' );
#line 113 "/home/production/cxgn/sgn/mason/breeders_toolbox/trial/create_spreadsheet_dialog.mas"
  } else {
$m->print( '        var selected_trial_ids = jQuery("#html_select_trial_for_create_spreadsheet").val();
        var selected_trial_names = [];
        jQuery("#html_select_trial_for_create_spreadsheet").find(\'option:selected\').each(function(){
            selected_trial_names.push(jQuery(this).text());
        });
' );
#line 119 "/home/production/cxgn/sgn/mason/breeders_toolbox/trial/create_spreadsheet_dialog.mas"
  }
$m->print( '        console.log(selected_trial_names);
        var level = jQuery(\'#create_spreadsheet_data_level\').val();
        var url = \'\';
        if (level == \'plants\'){
            url = \'has_plants\';
        }
        if (level == \'subplots\' || level == \'plants_subplots\'){
            url = \'has_subplots\';
        }
        if (level == \'tissue_samples\'){
            url = \'has_tissue_samples\';
        }

        for (var i=0; i<selected_trial_ids.length; i++){
            if (level == \'plants\' || level == \'subplots\' || level == \'plants_subplots\' || level == \'tissue_samples\'){
                new jQuery.ajax({
                    type: \'POST\',
                    url: \'/ajax/breeders/trial/\'+selected_trial_ids[i]+\'/\'+url,
                    dataType: "json",
                    beforeSend: function() {
                        jQuery("#working_modal").modal("show");
                    },
                    success: function (response) {
                        //console.log(response);
                        jQuery("#working_modal").modal("hide");
                        if(response.has_plants == 0 || response.has_subplots == 0 || response.has_tissue_samples == 0){
                            alert(\'The trial \'+response.trial_name+\' does not have \'+level);
                            jQuery(\'#create_spreadsheet_data_level\').val(\'plots\');
                            jQuery(\'#create_spreadsheet_plant_options\').hide();
                        }
                    },
                    error: function () {
                        jQuery("#working_modal").modal("hide");
                        alert(\'An error occurred checking trial \'+selected_trial_names[i]+\' for plants or subplots.\');
                    }
                });
            }
        }
    });

    jQuery(\'#create_spreadsheet_data_level\').change(function() {
        jQuery("#create_spreadsheet_sample_number").val(\'\');
        if ( jQuery(\'#create_spreadsheet_data_level\').val() == \'plants\' || jQuery(\'#create_spreadsheet_data_level\').val() == \'subplots\' ) {
            jQuery(\'#create_spreadsheet_plant_options\').show();
        } else {
            jQuery(\'#create_spreadsheet_plant_options\').hide();
        }
    });

    jQuery(\'#create_phenotyping_ok_button\').on(\'click\', function () {
        create_phenotype_spreadsheet();
    });

});


function create_phenotype_spreadsheet() {
    var list = new CXGN.List();
    var trait_list_id = jQuery(\'#trait_list_spreadsheet_list_select\').val();
    var include_notes = jQuery(\'#include_notes_column\').is(\':checked\');
    var trait_list;
    if (! trait_list_id == "") {
        trait_list = JSON.stringify(list.getList(trait_list_id));
    }
    else {
        alert("You must select a Trait List first");
        return;
    }
' );
#line 188 "/home/production/cxgn/sgn/mason/breeders_toolbox/trial/create_spreadsheet_dialog.mas"
  if ($trial_id){
$m->print( '        var trial_ids = [' );
#line 189 "/home/production/cxgn/sgn/mason/breeders_toolbox/trial/create_spreadsheet_dialog.mas"
$m->print(  $trial_id  );
#line 189 "/home/production/cxgn/sgn/mason/breeders_toolbox/trial/create_spreadsheet_dialog.mas"
$m->print( '];
' );
#line 190 "/home/production/cxgn/sgn/mason/breeders_toolbox/trial/create_spreadsheet_dialog.mas"
  } else {
$m->print( '        var trial_ids = jQuery("#html_select_trial_for_create_spreadsheet").val();
' );
#line 192 "/home/production/cxgn/sgn/mason/breeders_toolbox/trial/create_spreadsheet_dialog.mas"
  }
$m->print( '    var trial_stock_type = "' );
#line 193 "/home/production/cxgn/sgn/mason/breeders_toolbox/trial/create_spreadsheet_dialog.mas"
$m->print(  $trial_stock_type  );
#line 193 "/home/production/cxgn/sgn/mason/breeders_toolbox/trial/create_spreadsheet_dialog.mas"
$m->print( '";
    new jQuery.ajax({
        type: \'POST\',
        url: \'/ajax/phenotype/create_spreadsheet\',
        dataType: "json",
        data: {
            \'trial_ids\': JSON.stringify(trial_ids),
            \'trait_list\': trait_list,
            \'include_notes\': include_notes,
            \'data_level\': jQuery("#create_spreadsheet_data_level").val(),
            \'sample_number\': jQuery("#create_spreadsheet_sample_number").val(),
            \'format\': jQuery(\'#create_spreadsheet_phenotype_file_format\').val(),
            \'trial_stock_type\': trial_stock_type
        },
        beforeSend: function() {
            jQuery(\'#working_modal\').modal("show");
        },
        success: function (response) {
            jQuery(\'#working_modal\').modal("hide");
            if (response.error) {
                alert(response.error);
            } else {
                //alert(response.filename);
                jQuery(\'#working_modal\').modal("hide");
                window.location.href = "/download/"+response.filename;
            }
        },
        error: function () {
            jQuery(\'#working_modal\').modal("hide");
            alert(\'An error occurred creating a phenotype file.\');
            jQuery(\'#create_spreadsheet_dialog\').modal("hide");
        }
    });
}


</script>
' );
;return;
},
'declared_args' => {
  '$trial_id' => { default => ' undef' },
  '$trial_name' => { default => ' undef' },
  '$trial_stock_type' => { default => ' undef' }
},
'load_time' => 1763102299,

)
;