{% extends "base.html" %}
{% block title %}{{ instrument.name|default:"Instrument not found" }} - Re-run past jobs{% endblock %}
{% load static %}

{% block body %}
    {% if not instrument %}
        <div class="text-center col-md-6 col-md-offset-3 card card-body">
            {{ message }}
        </div>
    {% else %}
        {% if instrument.is_active %}
            <div class="row">
                <div class="col-md-12 text-center">
                    <h2>{{ instrument.name }} - Re-run past jobs</h2>
                </div>
            </div>
            <div class="row">
                <div class="col-md-6 col-md-offset-3">
                    {% include "snippets/instrument_status.html" with processing=processing queued=queued last_instrument_run=last_instrument_run only %}
                </div>
            </div>
            <br/>
            <input type="hidden" id="last_run_number" value="{{ last_instrument_run.run_number }}"/>
            <form id="submit_jobs" method="POST" action="{% url 'runs:run_confirmation' instrument=instrument.name %}"
                  class="form-horizontal">
                {% csrf_token %}
                <div class="row">
                    <div class="col-md-9">
                        {% include "snippets/variables/form_warnings.html" %}
                        <br/>
                        <div class="row">
                            <div class="col-md-10 col-md-offset-1">
                                <div class="alert alert-warning collapse" id="run_range_warning">
                                    <strong>Warning:</strong> a maximum of 20 runs can be submitted at one time.
                                </div>
                            </div>
                        </div>
                        <div class="js-variable-by-run">
                            <div class="row">
                                <label for="runs" class="col-form-label col-md-3">
                                    Run Numbers
                                    <a href="#" data-toggle="popover"
                                       data-content="A list of run number to re-run, the list can include ranges e.g. 1, 4-6. All the run numbers must be in the same experiment in order to apply the desired variables."
                                       data-trigger="hover click focus" data-placement="top" data-container="body" aria-label="A list of run number to re-run, the list can include ranges e.g. 1, 4-6. All the run numbers must be in the same experiment in order to apply the desired variables.">
                                        <i class="fas fa-info-circle"></i>
                                    </a>
                                </label>
                                <div class="col-md-5">
                                    <input type="text" id="runs" name="runs"
                                           value="{{ last_instrument_run.run_number }}" class="form-control"/>
                                </div>
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label for="only_re_runs" class="col-form-label col-md-10">Only submit
                                            re-runs</label>
                                        <div class="col-md-2">
                                            <input type="checkbox" id="only_re_runs" name="only_re_runs" checked
                                                   disabled/>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="submit_run_description" class="col-form-label col-md-3">
                                Re-run description
                                <a href="#" data-toggle="popover"
                                   data-content="This will be appended onto the run number for you and others to identify the reason for re-submitting."
                                   data-trigger="hover" data-placement="top" data-container="body" aria-label="This will be appended onto the run number for you and others to identify the reason for re-submitting."><i
                                        class="fas fa-info-circle"></i></a>
                            </label>
                            <div class="col-md-9">
                                <input type="text" id="submit_run_description" name="run_description" class="form-control"/>
                            </div>
                        </div>
                        <div class="js-variables-container">
                            {% include "snippets/variables/form.html" with standard_variables=standard_variables advanced_variables=advanced_variables variable_help=variable_help instrument=instrument.name only %}
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="card card-body">
                            <div class="row">
                                <div class="col-md-12"><h4>Additional Actions</h4></div>
                            </div>
                            <div class="row">
                                <ul class="js-form-actions">
                                    {% include "snippets/reset_buttons.html" %}
                                </ul>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-12 js-action-explaination"></div>
                        </div>
                    </div>
                </div>
                <div class="form-group variables-buttons">
                    <div class="col-md-9 text-right">
                        <a href="{% url 'runs:list' instrument.name %}" class="btn btn-danger" id="cancel">Cancel</a>
                        <input type="submit" value="Submit Runs" class="btn btn-success" id="variableSubmit" />
                    </div>
                </div>
            </form>

            <div class="modal fade" id="default-variables-modal">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal"><span
                                    aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                            <h4 class="modal-title">Default Variables</h4>
                        </div>
                        <div class="modal-body">
                            <h5>Standard Variables</h5>
                            {% for name,variable in default_standard_variables.items %}
                                <div>
                                    <strong>{{ name }}:</strong> {{ variable.value }}
                                </div>
                            {% endfor %}
                            <h5>Advanced Variables</h5>
                            {% for name,variable in default_advanced_variables.items %}
                                <div>
                                    <strong>{{ name }}:</strong> {{ variable.value }}
                                </div>
                            {% endfor %}
                        </div>
                    </div>
                </div>
            </div>

            <div class="modal fade" id="script-preview-modal">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-body">
                            <div class="progress">
                                <div class="progress-bar progress-bar-striped active" role="progressbar"
                                     aria-valuenow="100" aria-valuemin="100" aria-valuemax="100" style="width: 100%">
                                </div>
                            </div>
                            <pre class="prettyprint js-script-container">
                            </pre>
                        </div>
                        <div class="modal-footer">
                            <div class="col-md-12 pull-right">
                                <a href="#downloadScript" id="downloadScript" class="btn btn-primary">Download
                                    Script</a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

        {% else %}
            <div class="text-center col-md-6 col-md-offset-3 card card-body">
                Instrument is not active - it has no runs.
            </div>
        {% endif %}
    {% endif %}
{% endblock %}

{% block stylesheets %}
    <link rel="stylesheet" href="{% static 'css/vendor/prettify.css' %}">
{% endblock %}
{% block scripts %}
    <script src="{% static 'javascript/vendor/prettify.js' %}"></script>
    <script src="{% static 'javascript/run_variables.js' %}"></script>
{% endblock %}
