{% load staticfiles %}

<h1>Select Query</h1>

<p>
Select a query from the following list. Once you make the selection, it will
automatically load the appropriate form and display it on the next page.
</p>


{% if data.number_queries > 0 %}
    <form id="form_custom_queries" enctype="multipart/form-data" method="post">
        {% csrf_token %}
        {{ data.form }}
        <button class="button-right btn btn-default has-spinner"
                id="button_select_query"
                data-btn-text="Select Query"
                disabled>Select Query
        </button>
        <br>
    </form>
{% endif %}

{% if data.number_history > 0 %}
    <h3>History</h3>

    <form id="form_history" action="#" enctype="multipart/form-data" method="post">
        <input id="to_recover" name="to_recover" value="" type="text" class="hidden">
        {% csrf_token %}
        {{ data.formset_history.management_form }}
        {{ data.formset_history.non_form_errors.as_ul }}
        <table id="formset" class="table table-hover container-fluid">
            {% for form in data.formset.forms %}
                {% if forloop.first %}
                    <thead>
                        <tr>
                            {% for field in form.visible_fields %}
                                <th class="text-center col-xs-2">
                                    {{ field.label|capfirst }}
                                </th>
                            {% endfor %}
                            <th class="text-center col-xs-1"></th>
                        </tr>
                    </thead>
                {% endif %}

                <tr>
                {% for field in form.visible_fields %}
                    <td class="text-center">
                    {# Include the hidden fields in the form #}
                    {% if forloop.first %}
                        {% for hidden in form.hidden_fields %}
                            {{ hidden }}
                        {% endfor %}
                    {% endif %}
                    {{ field.errors.as_ul }}
                    {% if field.label|capfirst == "Last modified" %}
                        <span class="hidden"> {{ field }} </span>
                            {{ field.value|date:"D j M Y, H:i:s" }}
                    {% elif field.label|capfirst == "Delete" %}
                            {{ field }}
                    {% else %}
                        <span class="hidden"> {{ field }} </span>
                            {{ field.value }}
                    {% endif %}
                    </td>
                {% endfor %}
                {% if form.status.value == 0 %}
                    {% if form.query_message.value == "Error during query step." %}
                        <td class="col-xs-1 text-center">
                            <button class="recover_btn btn btn-danger detail-log"
                                    value="{{ form.id_history.value }}"
                                    type="button"
                                    disabled>
                                <i class="fa fa-times" aria-hidden="true" title="Align Left"></i>
                                Error
                            </button>
                        </td>
                    {% else %}
                        <td class="col-xs-1 text-center">
                            <button class="recover_btn btn btn-info detail-log"
                                    value="{{ form.id_history.value }}"
                                    type="button">
                                <i class="fa fa-search" aria-hidden="true" title="Align Left"></i>
                                Recover
                            </button>
                        </td>
                    {% endif %}
                {% elif form.status.value == 1 %}
                    {% if form.query_message.value == "Error during result files creation." %}
                        <td class="col-xs-1 text-center">
                            <button class="recover_btn btn btn-danger detail-log"
                                    value="{{ form.id_history.value }}"
                                    type="button"
                                    disabled>
                                <i class="fa fa-times" aria-hidden="true" title="Align Left"></i>
                                Error
                            </button>
                        </td>
                    {% else %}
                        <td class="col-xs-1 text-center">
                            <i class="fa fa-cog" aria-hidden="true" title="Align Left"></i>Pending
                        </td>
                    {% endif %}
                {% else %}
                    <td class="col-xs-1 text-center">
                        <a href="{% url 'core_custom_queries_app_output' form.id_history.value %}"
                           class="btn btn-primary">
                            <i class="fa fa-file-o" aria-hidden="true" title="Align Left"></i>
                            Output files
                        </a>
                    </td>
                {% endif %}
                </tr>
            {% endfor %}
        </table>
    </form>
    <button class="btn_all_history btn btn-default">
        <i class="fa fa-check-square-o" aria-hidden="true" title="Align Left"></i>Select all
    </button>

    <button class="btn btn-default delete-query" style="text-decoration:none" form="form_history">
        <i class="fa fa-trash" title="Align Left"></i>Delete selected queries
    </button>
{% endif %}


