<!DOCTYPE html>

<html lang="en">
<head>
    <title>GBD Benchmark Database</title>
    <link rel="stylesheet" href="{{ url_for('static', filename='main.css') }}" />
    <!--<script src="{{ url_for('static', filename='jquery-3.6.0.min.js') }}"></script>
    <script src="{{ url_for('static', filename='sorttable.js') }}"></script>
    <script src="{{ url_for('static', filename='w3.js') }}"></script>-->
</head>

<body>
<div class="main">
    <header>
        <img center src="{{ url_for('static', filename='img/gbd_logo_small.png') }}" alt="GBD-Logo">
        <form method="post" id="mainform">
            <fieldset style="float:left">
                <legend>Select Context</legend>
                {% for c in contexts %}
                <button type="submit" id="context" name="context" value="{{ c }}" formaction="{{ url_for('quick_search') }}" form="mainform">
                    {% if c == context %}
                        <span style="color:var(--link-color-1)">{{ c }}</span>
                    {% else %}
                        {{ c }}
                    {% endif %}
                </button>
                {% endfor %}
            </fieldset>
            <fieldset>
                <legend>Select and Download '{{ context }}' - Databases</legend>
                {% for dbname in databases %}
                    <button type="submit" id="{{ dbname }}" name="selected_db" value="{{ dbname }}" formaction="{{ url_for('quick_search') }}" form="mainform">
                    {% if dbname == selected %}
                        <span style="color:var(--link-color-1)">{{ dbname }}</span>
                    {% else %}
                        {{ dbname }}
                    {% endif %}
                    </button>
                {% endfor %}
                <span style="width: max-content;"></span>
                <a href="{{ url_for('get_database_file', database=selected) }}" title="Download Database">Download {{ selected }}</a>
            </fieldset>
            <br />
            <fieldset>
                <legend>Query for Instances</legend>
                <input type="hidden" id="context" name="context" value="{{ context }}">
                <input type="text" id="query" name="query" class="query" placeholder="Query for Instances" value="{{ query }}">
                <button type="submit" class="submit" formaction="{{ url_for('quick_search') }}" form="mainform" id="action1" name="action1" value="show" title="Query for instances and selected features">Show</button>
            </fieldset>
            <br />
            <div class="info">
                {% if error is not none %}
                <span style="color:var(--link-color-1)">{{ error }}</span>
                {% else %}
                    Found {{ total }} Benchmark Instances
                {% endif %} 
            </div>
        </form>
        <div class="help">
            <fieldset>
                <legend>Quickstart</legend>
                <ul>
                    <li><b>Query: </b>
                        Queries are of the form "featurename ( '<' | '>' | '=' ) value" and can be combined with logical operators "and" and "or". 
                        Example: <a href="{{ url_for('quick_search', track='main_2023') }}">track=main_2023</a>
                    </li>
                    <li><b>Download: </b>
                        The file <a href="{{ url_for('get_url_file', context=context) }}">{{ query_name }}.uri</a> contains the download links for the selected instances. 
                        Download and use this file to fetch the actual instances. 
                        Example: <span style="font-family: monospace;">wget --content-disposition -i {{ query_name }}.uri</span>
                    </li>
                </ul>
            </fieldset>
        </div>
    </header>
    
    <div class="content">
        <table>
            <tr>
                <th>hash</th>
                {% for feature in features %}
                <th>{{ feature }}</th>
                {% endfor %}
            </tr>
            
            {% for row in result %}
            <tr>
                {% for item in row %}
                    {% if loop.index == 1 %}
                        <td><a href="{{ url_for('get_file', hashvalue=item, context=context) }}">{{ item }}</a></td>
                    {% elif item is link_field %}
                        <td><a href="{{ item }}">{{ item }}</a></td>
                    {% elif item is int_field %}
                        <td style="text-align: right;">{{ item }}</td>
                    {% elif item is num_field %}
                        <td style="text-align: right;">{{ '%0.2f'| format(item|float) }}</td>
                    {% else %}
                        <td>{{ item }}</td>
                    {% endif %}
                {% endfor %}
            </tr>
            {% endfor %}
        </table>
    </div>

    <footer>
        <div style="display: inline-block; vertical-align: center">
            
            {% if pages > 0 %}
                Showing 1000 of {{ total }} instances. Select page: 
                {% for i in range(0, pages) %}
                    {% if i == page %}
                    <button type="submit" formaction="/" form="mainform" name="page" value="{{ i }}" style="color:var(--link-color-1);">{{ i + 1 }}</button>
                    {% else %}
                    <button type="submit" formaction="/" form="mainform" name="page" value="{{ i }}">{{ i + 1 }}</button>
                    {% endif %}
                {% endfor %}
            {% else %}
                Found {{ total }} instances.
            {% endif %}
        </div>
        <div style="float: right;">
            If you find this useful, please cite: <a href="https://easychair.org/publications/paper/jQXv">A Problem Meta-Data Library for Research in SAT</a>
        </div>
    </footer>
</div>
</body>
</html>
