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

#line 8 "/home/production/cxgn/sgn/mason/transformation/add_autogenerated_name_metadata_dialog.mas"
$m->print( '
' );
#line 9 "/home/production/cxgn/sgn/mason/transformation/add_autogenerated_name_metadata_dialog.mas"
$m->comp(   '/util/import_javascript.mas', classes => ['CXGN.TrialTreeFolders']   
); #line 9 "/home/production/cxgn/sgn/mason/transformation/add_autogenerated_name_metadata_dialog.mas"
$m->print( '


<div class="modal fade" id="add_autogenerated_name_metadata_dialog" name="add_autogenerated_name_metadata_dialog" tabindex="-1" role="dialog" aria-labelledby="addAutogeneratedNameMetadataDialog">
    <div class="modal-dialog modal-xl" role="document">
        <div class="modal-content">
            <div class="modal-header" style="text-align: center">
                <button type="reset" class="close" id="add_autogenerated_name_metadata_dismiss_button_1" name="add_autogenerated_name_metadata_dismiss_button" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="addAutogeneratedNameMetadataDialog">Add Autogenerated Name Metadata</h4>
            </div>
            <div class="modal-body">
                <div class="container-fluid">
                    <div>
                        <center>
                            ' );
#line 23 "/home/production/cxgn/sgn/mason/transformation/add_autogenerated_name_metadata_dialog.mas"
$m->comp( { content => sub {
#line 23 "/home/production/cxgn/sgn/mason/transformation/add_autogenerated_name_metadata_dialog.mas"
$m->print( '
                                <p>
                                    <b>Autogenerated Names</b>
                                    <br>
                                <a id="autogenerated_name_usage_info">Usage Information</a>
                            </p>
                        ' );
#line 29 "/home/production/cxgn/sgn/mason/transformation/add_autogenerated_name_metadata_dialog.mas"
} }, '/page/explanation.mas', title=>''
 );
#line 29 "/home/production/cxgn/sgn/mason/transformation/add_autogenerated_name_metadata_dialog.mas"
$m->print( '
                        </center>
                    </div>
                    <form class="form-horizontal" id="add_new_autogenerated_name_metadata_form" name="add_new_autogenerated_name_metadata_form">
' );
#line 33 "/home/production/cxgn/sgn/mason/transformation/add_autogenerated_name_metadata_dialog.mas"
 if ($programs) {
$m->print( '                        <div class="form-group">
                            <label class="col-sm-4 control-label">Breeding Program: </label>
                            <div class="col-sm-8">
                                <select class="form-control" id="autogenerated_name_metadata_program" name="autogenerated_name_metadata_program">
                                    <option value="">Select Breeding Program</option>
' );
#line 39 "/home/production/cxgn/sgn/mason/transformation/add_autogenerated_name_metadata_dialog.mas"

foreach my $program (@$programs) {
if (@$program[3]) {
print "<option selected=\"selected\" value='".@$program[0]."'>".@$program[1]."</option>";
} else {
print "<option value='".@$program[0]."'>".@$program[1]."</option>";
}
}
#line 48 "/home/production/cxgn/sgn/mason/transformation/add_autogenerated_name_metadata_dialog.mas"
$m->print( '                                </select>
                            </div>
                        </div>
' );
#line 51 "/home/production/cxgn/sgn/mason/transformation/add_autogenerated_name_metadata_dialog.mas"
 } elsif ($program_name) {
$m->print( '                        <div class="form-group">
                            <label class="col-sm-4 control-label">Breeding Program: </label>
                            <div class="col-sm-8">
                                <select class="form-control" id="autogenerated_name_metadata_program" name="autogenerated_name_metadata_program">
                                    <option value="' );
#line 56 "/home/production/cxgn/sgn/mason/transformation/add_autogenerated_name_metadata_dialog.mas"
$m->print( $program_id );
#line 56 "/home/production/cxgn/sgn/mason/transformation/add_autogenerated_name_metadata_dialog.mas"
$m->print( '">' );
#line 56 "/home/production/cxgn/sgn/mason/transformation/add_autogenerated_name_metadata_dialog.mas"
$m->print( $program_name );
#line 56 "/home/production/cxgn/sgn/mason/transformation/add_autogenerated_name_metadata_dialog.mas"
$m->print( '</option>
                                </select>
                            </div>
                        </div>
' );
#line 60 "/home/production/cxgn/sgn/mason/transformation/add_autogenerated_name_metadata_dialog.mas"
 }
$m->print( '                        <div class="form-group">
                            <label class="col-sm-4 control-label">Format Name: </label>
                            <div class="col-sm-8">
                                <input class="form-control" id="format_name" name="format_name" type="text" placeholder="For example: breedbase_transformants"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-4 control-label">Description: </label>
                            <div class="col-sm-8">
                                <input class="form-control" id="autogenerated_name_metadata_description" name="autogenerated_name_metadata_description" type="text" placeholder="Optional" />
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-4 control-label">Autogenerated Names for: </label>
                            <div class="col-sm-8">
                                <select class="form-control" id="name_type">
                                    <option value="">Select a name type</option>
                                    <option value="transformant">transformant (accession)</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-4 control-label">How many attributes for basename: </label>
                            <div class="col-sm-8">
                                <select class="form-control" id="number_of_attributes">
                                    <option value="">Select a number</option>
                                    <option value="1">1</option>
                                    <option value="2">2</option>
                                    <option value="3">3</option>
                                    <option value="4">4</option>
                                    <option value="5">5</option>
                                </select>
                            </div>
                        </div>
                        <div id ="name_attributes_form_div">
                        </div>
                        <div class="form-group">
                            <label class="col-sm-4 control-label">Last Serial Number: </label>
                            <div class="col-sm-8">
                                <input class="form-control" id="last_number" name="last_number" type="number" placeholder="For example: value 0. The next name would be BTI_1"/>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
            <div class="modal-footer">
                <button id="add_autogenerated_name_metadata_dismiss_button_2" name="add_autogenerated_name_metadata_dismiss_button" type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary" id="new_autogenerated_name_metadata_submit">Submit</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="add_autogenerated_name_metadata_message" name="add_autogenerated_name_metadata_message" tabindex="-1" role="dialog" aria-labelledby="addAutogeneratedNameMetadataMessage">
    <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="addAutogeneratedNameMetadataMessage">Autogenerated Name Metadata</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>
                        Autogenerated name format was stored successfully.
                    </p>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" name="add_autogenerated_name_format_close_button" id="add_autogenerated_name_format_close_button" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="autogenerated_name_usage_info_dialog" name="autogenerated_name_usage_info_dialog" tabindex="-1" role="dialog" aria-labelledby="autogeneratedNameUsageInfoDialog">
    <div class="modal-dialog modal-xl " role="document">
        <div class="modal-content">
            <div class="modal-header" style="text-align: center">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="autogeneratedNameUsageInfoDialog">How to Use Autogenerated Name Feature</h4>
            </div>
            <div class="well well-lg">
                <div class="panel panel-default">
                    <div class="modal-body">
                        <div class="container-fluid">
                            <h4>
                                Autogenerated Names:
                            </h4>
                            <li>You can use the autogenerated name feature to automatically generate names based on your desired format followed by a serial number. Before you can use this feature, you need to store information about your naming format and other related metadata. Currently, only the transformation tool has this autogenerated name feature for generating transformant names. If you are interested in using this feature in other types of projects, please contact us.</li>
                            <li>Naming formats can be shared within the same breeding program. All stored formats are displayed in the Autogenerated Name Metadata section on the Breeding Program page.</li>
                            <br>
                            <h4>
                                Autogenerated Name Metadata:
                            </h4>
                            <li><b>Breeding Program</b>: Each breeding program can store different types of naming formats. You can choose which naming format you would like to use for each project.</li>
                            <li><b>Format Name</b>: Format name must be globally unique. It is an identifier used for capturing all metadata related to each naming format.</li>
                            <li><b>Description</b>: Description of this naming format.</li>
                            <li><b>Name Type</b>: Name type is used to retrieve related information used as attributes in the naming format. For example, attributes for a transformant name can be breeding program, vector construct, plant material used in the transformation, transformation project, transformation ID, text field.</li>
                            <li><b>Number of Attributes</b>: Indicating how many attributes you would like to include in a name. You can also include texts.</li>
                            <li><b>Attributes for Basename</b>: You can specify attributes you would like to include in a basename, as well as the order of these attributes. If you would like to add text field in the basename, select "text", then specify the text field. Attributes are separated by an underscore. For example, selecting 3 attributes, if you would like to include breeding program, vector construct, and a text field. The generated name would be <b>"breedingProgram_vectorConstruct_text"</b> and a <b>serial number</b>.</li>
                            <li><b>Last Serial Number</b>: If you previously used this naming format before using the database. This input field allows you to continue using your naming format by indicating the last serial number that you used. If this is a new format, please indicates "0". The first name will have "1" as the first serial number. After you use the naming format to add new names in the database, the last serial number is automatically updated and stored as new metadata of that naming format.</li>
                            <br>
                            <h4>
                                Setting Up Naming Format for Each Project:
                            </h4>
                            <li>Once you store autogenerated name metadata for each format, you can set up specific naming format for each project</li>
                            <li>The link for setting up naming format is on each transformation project page</li>
                        </div>
                        <div class="modal-footer">
                            <button id="close_autogenerated_name_info_dialog" type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>


<script>

jQuery(document).ready(function(){

    jQuery("[name=\'add_autogenerated_name_metadata_link\']").click(function(e) {
        e.preventDefault();
        jQuery("#add_autogenerated_name_metadata_dialog").modal("show");
    });

    jQuery(\'#autogenerated_name_usage_info\').click(function(){
        jQuery(\'#autogenerated_name_usage_info_dialog\').modal("show");
    });

    jQuery("#number_of_attributes").change(function() {
        number_of_attributes = jQuery(\'#number_of_attributes\').val();
        if (number_of_attributes){
            name_attributes_form();
        }
    })

    function name_attributes_form () {
        let number_of_attributes = parseInt(jQuery(\'#number_of_attributes\').val());

        let html = \'<table class="table table-bordered" id="name_attributes_table"><thead><tr><th>Attribute Order</th><th>Select Attribute</th></tr></thead><tbody>\';
        for(let i=0; i<number_of_attributes; i++){
            let attribute_order = i+1;
            html = html + \'<tr><td>\' + attribute_order + \'</td><td><div name="attribute_selects" id="attribute_select_\' +i+ \'"></div></td><td><span id="selected_attribute_\'+ i +\'_string" data-related="\'+encodeURI(attribute_order)+\'" ></span></td></tr>\';
        };
        html = html + \'</tbody></table>\';

        jQuery(\'#name_attributes_form_div\').html(html);

        let attribute_list = jQuery(\'#name_attributes_table\')
            .find("div")
            .map(function() { return this.id; })
            .get();
        for(let i=0; i<attribute_list.length; i++){
            get_select_box(\'related_attributes\', attribute_list[i], {\'name\' : \'selected_attribute\', \'id\' : \'selected_attribute_\'+i,  });
        };
    };

    jQuery(document).on(\'change\', "select[name=\'selected_attribute\']", function() {
        let div_id = \'#\' + this.id;
        let attribute = jQuery(div_id).val();
        if (attribute == \'text\') {
            attribute_string(div_id, div_id + \'_string\');
        }
    });

    function attribute_string(div_id, select_id){
        let attribute_text = jQuery(div_id).val();
        let html = \'<td><input class="form-control"  name ="select_id" id="select_id" value="" placeholder="Please enter text string"/></td>\';
        jQuery(select_id).html(html);

    };

    jQuery(\'#new_autogenerated_name_metadata_submit\').click(function(e){
        e.preventDefault();
        let name_attributes = [];

        jQuery(\'select[name="selected_attribute"]\').each(function() {
            value = this.value;
            let text_string;
            if (value == \'text\') {
                text_string =  jQuery(\'#select_id\').val();
                name_attributes.push({\'text\':text_string});
            } else {
                name_attributes.push(value);
            }
        });

        let breeding_program = jQuery(\'#autogenerated_name_metadata_program\').val();
        if (!breeding_program) {
            alert("Breeding program is required");
            return;
        }

        let format_name = jQuery(\'#format_name\').val();
        if (!format_name) {
            alert("Format name is required");
            return;
        }

        let description = jQuery(\'#autogenerated_name_metadata_description\').val();

        let name_type = jQuery(\'#name_type\').val();
        if (!name_type) {
            alert("Name type is required");
            return;
        }

        let last_serial_number = jQuery(\'#last_number\').val();
        if (!last_serial_number) {
            alert("Last serial number is required");
            return;
        }

        jQuery.ajax({
            url: \'/ajax/transformation/add_autogenerated_name_metadata\',
            method: \'POST\',
            data: {
                \'breeding_program\': breeding_program,
                \'format_name\': format_name,
                \'description\': description,
                \'name_type\': name_type,
                \'last_serial_number\': last_serial_number,
                \'name_attributes\': JSON.stringify(name_attributes),
            },
            dataType:\'json\',
            beforeSend: function() {
                jQuery("#working_modal").modal("show");
            },
            success: function(response) {
                jQuery("#working_modal").modal("hide");
                if (response.error) {
                    alert(response.error);
                } else {
                    jQuery("#add_autogenerated_name_metadata_message").modal("show");
                    jQuery(\'#add_autogenerated_name_metadata_dialog\').modal("hide");

                }
            },
            error: function() {
                jQuery("#working_modal").modal("hide");
                alert(\'An error occurred storing autogenerated name metadata.\');
                jQuery(\'#add_autogenerated_name_metadata_dialog\').modal("hide");
            },
        });

    });

    jQuery("[name=\'add_autogenerated_name_metadata_dismiss_button\']").click(function() {
        jQuery(\'#add_autogenerated_name_metadata_dialog\').modal(\'hide\');
        jQuery(\'#autogenerated_name_metadata_program\').val(\'\');
        jQuery(\'#format_name\').val(\'\');
        jQuery(\'#autogenerated_name_metadata_description\').val(\'\');
        jQuery(\'#name_type\').val(\'\');
        jQuery(\'#last_number\').val(\'\');
        jQuery(\'#number_of_attributes\').val(\'\');
        location.reload()
    });

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

});

</script>
' );
;return;
},
'declared_args' => {
  '$program_id' => { default => ' undef' },
  '$program_name' => { default => ' undef' },
  '$programs' => { default => ' undef' }
},
'load_time' => 1763082279,

)
;