{% extends "jury/base.html.twig" %}
{% import "jury/jury_macros.twig" as macros %}

{% block title %}Executable {{ executable.execid }} - {{ parent() }}{% endblock %}

{% block extrahead %}
    {{ parent() }}
    {{ macros.table_extrahead() }}
{% endblock %}

{% block content %}

    <h1>Executable <code>{{ executable.execid }}</code></h1>

    <div class="mb-4">
        {%- if is_granted('ROLE_ADMIN') -%}
            <p>
                {{ button(path('jury_executable_download', {'execId': executable.execid}), 'Download ZIP', 'secondary', 'file-download') }}
                {{ button(path('jury_executable_delete', {'execId': executable.execid}), 'Delete', 'danger', 'trash-alt', true) }}
            </p>
        {% endif %}
    </div>

    <div class="row">
        <div class="col-lg-4">
            <table class="table table-sm table-striped">
                <tr>
                    <th>Description</th>
                    <td>{{ executable.description }}</td>
                </tr>
                <tr>
                    <th>Type</th>
                    <td>{{ executable.type }}</td>
                </tr>
                <tr>
                    <th>Used as {{ executable.type }} script</th>
                    <td>
                        {% set used = false %}
                        {% if executable.type == 'compare' and default_compare == executable.execid %}
                            <em>default compare</em>
                            {% set used = true %}
                        {% elseif executable.type == 'run' and default_run == executable.execid %}
                            <em>default run</em>
                            {% set used = true %}
                        {% elseif executable.type == 'debug' and default_full_debug == executable.execid %}
                            <em>default full debug</em>
                            {% set used = true %}
                        {% endif %}
                        {% if executable.type == 'compare' %}
                            {% for problem in executable.problemsCompare %}
                                <a href="{{ path('jury_problem', {'probId': problem.externalid}) }}">
                                    {{ problem.externalid }} {{ problem | problemBadgeForContest }}
                                </a>
                                {% set used = true %}
                            {% endfor %}
                        {% elseif executable.type == 'run' %}
                            {% for problem in executable.problemsRun %}
                                <a href="{{ path('jury_problem', {'probId': problem.externalid}) }}">
                                    {{ problem.externalid }} {{ problem | problemBadgeForContest }}
                                </a>
                                {% set used = true %}
                            {% endfor %}
                        {% elseif executable.type == 'compile' %}
                            {% for language in executable.languages %}
                                <a href="{{ path('jury_language', {'langId': language.externalid}) }}">
                                    {{ language | entityIdBadge }}
                                </a>
                                {% set used = true %}
                            {% endfor %}
                        {% endif %}
                        {% if not used %}
                            <span class="nodata">none</span>
                        {% endif %}
                    </td>
                </tr>
            </table>
        </div>
    </div>

    <h1>Content</h1>

    {% if skippedBinary is not empty %}
        <div class="alert alert-warning">
            We exclude these files from editing since we could not detect their encoding (e.g. they are binary files):
            <ul>
                {% for data in skippedBinary %}
                    <li><code>{{ data.filename }}</code></li>
                {% endfor %}
            </ul>
        </div>
    {% endif %}

    {{ form_start(form) }}

    {% if not filenames %}
        <p class="nodata">No files in this executable.</p>
    {% else %}
        <ul class="nav nav-tabs source-tab-nav" role="tablist">
            {%- for idx, filename in filenames %}
                <li class="nav-item">
                    <a class="nav-link {% if (selected is null and loop.first) or selected == idx %}active{% endif %}" data-bs-toggle="tab"
                       href="#source-{{ idx }}" role="tab"><code>{{ filename }}</code>{%  if (executableBits[idx]) %} <sub>x-bit</sub>{% endif %}</a>
                </li>
            {%- endfor %}
        </ul>
        <div class="tab-content source-tab flex-pass">
            {%- for idx, filename in filenames %}
                <div class="tab-pane flex-pass fade {% if (selected is null and loop.first) or selected == idx %}show active{% endif %}" id="source-{{ idx }}"
                     role="tabpanel">
                    <div class="mb-1">
                        <a class="btn btn-secondary btn-sm"
                           href="{{ path('jury_executable_download_single', {execId: executable.execid, rank: ranks[idx]}) }}">
                            <i class="fas fa-download"></i> Download file
                        </a>
                        {% if is_granted('ROLE_ADMIN') %}
                            <a class="btn btn-secondary btn-sm"
                               data-ajax-modal
                               href="{{ path('jury_executable_delete_single', {execId: executable.execid, rankToDelete: ranks[idx]}) }}">
                                <i class="fas fa-trash"></i> Delete file
                            </a>
                        {% endif %}
                    </div>

                    {{ files[idx] | codeEditor(idx, null, is_granted('ROLE_ADMIN'), 'form_source' ~ idx, editorFilenames[idx]) }}
                    <script>
                        $(function () {
                            $('#form_source{{ idx }}').closest('div.mb-3').hide();
                        });
                    </script>
                </div>
            {%- endfor %}
        </div>

        <div class="row">
            <div class="col-lg-4">
                {{ form_widget(form) }}
            </div>
        </div>
    {% endif %}
    {{ form_end(form) }}

    {% if is_granted('ROLE_ADMIN') %}
        <hr>
        <i>or</i>

        <div class="row">
            <div class="col-lg-4">
                {{ form(uploadForm, { 'id': 'execUploadForm'}) }}
            </div>
        </div>
    {% endif %}

{% endblock %}
