{# edit_select_query.html #}

{% extends "base.html" %}

{% block content %}

    {% include "query_nav.html" %}

    <h1>Edit/create SQL query</h1>

    <form action="{% url 'query' %}" method="post">
        {% csrf_token %}
        <table class="formtable">
            {{ form }}
        </table>
        <input type="submit" value="Add" />
    </form>

    <h2>Tips</h2>
    <ul>
        <li>The research database runs MySQL.</li>

        <li>Append <b><code>LIMIT n</code></b> or
        <b><code>LIMIT start, n</code></b> to limit the results, where
        <code>offset</code> is numbered from 0 (not 1), and <code>n</code> is the
        number of records to retrieve (so <code>LIMIT 50</code> or
        <code>LIMIT 0, 50</code> retrieves the first 50 records, and
        <code>LIMIT 50, 50</code> retrieves the next 50).</li>
    </ul>

    <h1>Previous queries</h1>

    {% include "expand_collapse_buttons.html" %}

    {% if queries %}
        <table>
            <tr>
                <th>Created at</th>
                <th>Delete query</th>
                <th>SQL</th>
                <th>Active?</th>
                <th>Choose this one</th>
            </tr>
            {% for query in queries %}
                <tr>
                    <td>{{ query.created }}</td>
                    <td>
                        {% include "action_only_form.html" with action="delete_query" pk=query.id button="Delete" %}
                    </td>
                    <td>{{ query.formatted_query_safe|safe }}</td>
                    <td>
                        {% if query.active %}<b>{% endif %}
                        {{ query.active|yesno }}
                        {% if query.active %}</b>{% endif %}
                    </td>
                    <td>
                        {% if query.active %}
                            (activated)
                        {% else %}
                            {% include "action_only_form.html" with action="activate_query" pk=query.id button="Choose" %}
                        {% endif %}
                    </td>
                </tr>
            {% endfor %}
        </table>
        {% include "page_nav.html" with items=queries %}

    {% else %}
        <div>None.</div>
    {% endif %}

{% endblock %}
