{# Render a list of submissions for a jury page #}
{# @var \App\Entity\ExternalJudgement externalJudgement #}

{% if showExternalResult is not defined %}
    {% set showExternalResult = false %}
{% endif %}
{% if showExternalTestcases is not defined %}
    {% set showExternalTestcases = false %}
{% endif %}

{% set rowSpan = 1 %}
{% set tdExtraClass = '' %}
{% if showExternalResult and showExternalTestcases %}
    {% set rowSpan = 3 %}
    {% set tdExtraClass = ' thick-border' %}
{% endif %}

{% set is_scoring = false %}
{% if current_contest and current_contest.scoreboardTypeString == 'score' %}
    {% set is_scoring = true %}
{% endif %}

{% if submissions is empty %}
    <div class="alert alert-warning">No submissions</div>
{% else %}
    <div>
        <span class="badge text-bg-info">{{ submissionCounts.total }} submitted</span>
        <span class="badge text-bg-success">{{ submissionCounts.correct }} correct</span>

        {% if submissionCounts.unverified is defined and submissionCounts.unverified > 0 %}
            <span class="badge text-bg-warning">{{ submissionCounts.unverified }} unverified</span>
        {% endif %}

        {% if submissionCounts.shadowUnverified is defined and submissionCounts.shadowUnverified > 0 %}
            <span class="badge text-bg-warning">{{ submissionCounts.shadowUnverified }} shadow differences unverified</span>
        {% endif %}

        {% if submissionCounts.ignored > 0 %}
            <span class="badge text-bg-dark">{{ submissionCounts.ignored }} ignored</span>
        {% endif %}

        {% if submissionCounts.judging > 0 %}
            <span class="badge text-bg-primary">{{ submissionCounts.judging }} judging</span>
        {% endif %}

        {% if submissionCounts.queued > 0 %}
            <span class="badge text-bg-danger">{{ submissionCounts.queued }} queued (from {{submissionCounts.perteam}} teams)</span>
        {% endif %}

        {% if submissionCounts.inContest > 0 %}
            <span class="badge text-bg-secondary">{{ submissionCounts.inContest }} in contest</span>
        {% endif %}
    </div>

    <table class="data-table table table-hover table{% if showExternalResult and showExternalTestcases %}-3{% endif %}-striped table-sm submissions-table table-full-clickable-cell">
        <thead class="thead-light">
        <tr>
            {% if showExternalResult and showExternalTestcases %}
                <th scope="col" colspan="2">ID</th>
            {% else %}
                <th scope="col">ID</th>
            {% endif %}
            {%- if showContest %}

                <th scope="col">contest</th>
            {%- endif %}

            <th scope="col">time</th>
            <th scope="col">team</th>
            <th scope="col">problem</th>
            <th scope="col">lang</th>
            {%- if rejudging is defined %}
                <th scope="col">old result</th>
            {%- endif %}
            {% if showExternalResult and showExternalTestcases %}
                <th scope="col" colspan="2">{%-if rejudging is defined %}new {% endif %}result</th>
            {% else %}
                <th scope="col">{%- if rejudging is defined %}new {% endif %}result</th>
            {% endif %}
            {% if is_scoring %}
                <th scope="col">score</th>
            {% endif %}
            {% if showExternalResult and not showExternalTestcases %}
                <th scope="col">ext. result</th>
                {% if is_scoring %}
                    <th scope="col">ext. score</th>
                {% endif %}
            {% endif %}
            {%- if showTestcases is defined and showTestcases %}
                <th scope="col" class="not-sortable not-searchable">test results</th>
            {%- endif %}
            {% if not showExternalResult or not showExternalTestcases %}
                <th scope="col" class="table-button-head-left">verified</th>
                <th scope="col" class="table-button-head-right">by</th>
            {% endif %}

        </tr>
        </thead>
        <tbody>
        {%- for submission in submissions %}
            {%- if rejudging is defined %}
                {%- set link = path('jury_submission', {contestId: submission.contest.externalid, submitId: submission.externalid, rejudgingid: rejudging.rejudgingid}) %}
            {%- else %}
                {%- set link = path('jury_submission', {contestId: submission.contest.externalid, submitId: submission.externalid}) %}
            {%- endif %}

            {% if submission.team.affiliation %}
                {% set affilid = submission.team.affiliation.externalid %}
            {% else %}
                {% set affilid = '' %}
            {% endif %}

            <tr class="{% if not submission.valid %}ignore{% endif %}">
                {% if showExternalResult and showExternalTestcases %}
                    <td class="{{ tdExtraClass }}">
                        <a href="{{ link }}">
                            Local
                        </a>
                    </td>
                {% endif %}
                <td class="{{ tdExtraClass }}">
                    <a href="{{ link }}">
                        {{ submission.externalid }}
                    </a>
                </td>
                {%- if showContest %}
                    <td class="{{ tdExtraClass }}"><a href="{{ link }}">{{ submission.contest.externalid }}</a></td>
                {%- endif %}

                <td rowspan="{{ rowSpan }}" class="{{ tdExtraClass }}">
                    <a href="{{ link }}">{{ submission.submittime | printtime(null, submission.contest) }}</a>
                </td>
                <td rowspan="{{ rowSpan }}" class="{{ tdExtraClass }}">
                    <a href="{{ link }}">
                        {{ submission.team.effectiveName | u.truncate(teamname_max_length, '…') }}
                    </a>
                </td>
                <td class="probid{{ tdExtraClass }}" rowspan="{{ rowSpan }}">
                    <a href="{{ link }}"
                       title="{{ submission.problem.name }}">{{ submission.contestProblem | problemBadge }}</a>
                </td>
                <td class="langid{{ tdExtraClass }}" rowspan="{{ rowSpan }}">
                    <a href="{{ link }}"
                       title="{{ submission.language.name }}">{{ submission.language | entityIdBadge }}</a>
                </td>
                {% if showExternalResult and showExternalTestcases %}
                    <td class="{{ tdExtraClass }}">
                        <a href="{{ link }}">
                            Local
                        </a>
                    </td>
                {% endif %}
                {%- if rejudging is defined %}
                    <td class="{{ tdExtraClass }}"><a href="{{ path('jury_submission', {contestId: submission.contest.externalid, submitId: submission.externalid}) }}">
                            {{ submission.oldResult | printValidJuryResult }}
                        </a></td>
                {%- endif %}
                <td class="{{ tdExtraClass }}">
                    <a href="{{ link }}">
                        {%- if rejudging is defined %}
                            ⇝
                        {% endif %}
                        {{ submission | printValidJurySubmissionResult }}
                    </a>
                </td>
                {% if is_scoring %}
                    <td class="{{ tdExtraClass }} right">
                        {% if submission.result is not null and submission.score is not null %}
                            {{ submission.score | number_format(2, '.', '') }}
                        {% else %}
                            {{ '-' }}
                        {% endif %}
                    </td>
                {% endif %}
                {% if showExternalResult and not showExternalTestcases %}
                    {% if submission.externalJudgements.empty %}
                        {% set externalJudgement = null %}
                    {% else %}
                        {% set externalJudgement = submission.externalJudgements.first %}
                    {% endif %}
                    <td class="{{ tdExtraClass }}">
                        <a href="{{ link }}">
                            {% if submission.source.value != 'shadowing' %}
                                {{- 'internal' | printValidJuryResult -}}
                            {% elseif externalJudgement is null or externalJudgement.result is empty %}
                                {{- 'pending' | printValidJuryResult -}}
                            {% else %}
                                {{- externalJudgement.result | printValidJuryResult -}}
                            {% endif %}
                        </a>
                    </td>
                    {% if is_scoring %}
                        <td class="{{ tdExtraClass }}">
                            {% if externalJudgement is not null and externalJudgement.score is not null %}
                                {{ externalJudgement.score | number_format(2, '.', '') }}
                            {% else %}
                                {{ '-' }}
                            {% endif %}
                        </td>
                    {% endif %}
                {% endif %}
                {%- if showTestcases is defined and showTestcases %}
                    <td class="testcase-results{{ tdExtraClass }}">
                        {{- submission | testcaseResults -}}
                    </td>
                {%- endif %}
                {% if not showExternalResult or not showExternalTestcases %}
                    {%- set claim = false %}
                    {%- if submission.judgings.first is empty or submission.judgings.first.result is empty -%}
                        {%- set verified = '' %}
                        {%- set juryMember = '' %}
                    {%- else %}
                        {%- set juryMember = submission.judgings.first.juryMember %}
                        {%- if submission.judgings.first.verified %}
                            {%- set verified = 'yes' %}
                        {%- else %}
                            {%- set verified = 'no' %}
                            {%- if submission.judgings.first.juryMember is empty %}
                                {%- set claim = true %}
                            {%- else %}
                                {%- set verified = 'claimed' %}
                            {%- endif %}
                        {%- endif %}
                    {%- endif %}
                    {%- if claim %}
                        {%- set claimArg = {claim: 1} %}
                    {%- else %}
                        {%- set claimArg = {unclaim: 1} %}
                    {%- endif %}
                    <td class="{{ tdExtraClass }}"><a href="{{ link }}">{{ verified }}</a></td>
                    {% if not showExternalResult or not showExternalTestcases %}
                        <td class="{{ tdExtraClass }}">
                            {%- if rejudging is defined %}
                                {%- set claimLink = path('jury_submission', claimArg | merge({contestId: submission.contest.externalid, submitId: submission.externalid, rejudgingid: rejudging.rejudgingid})) %}
                            {%- else %}
                                {%- set claimLink = path('jury_submission', claimArg | merge({contestId: submission.contest.externalid, submitId: submission.externalid})) %}
                            {%- endif %}
                            {%- if claim -%}
                                <a class="btn btn-outline-secondary btn-sm"
                                   href="{{ claimLink }}">claim</a>
                            {%- elseif (not submission.judgings.first or not submission.judgings.first.verified) and juryMember == app.user.username -%}
                                <a class="btn btn-info btn-sm" href="{{ claimLink }}">unclaim</a>
                            {%- else -%}
                                <a href="{{ link }}">{{ juryMember }}</a>
                            {%- endif -%}
                        </td>
                    {% endif %}
                {% endif %}

            </tr>
            {% if showExternalResult and showExternalTestcases %}
                <tr>
                    {% if submission.externalJudgements.empty %}
                        {% set externalJudgement = null %}
                    {% else %}
                        {% set externalJudgement = submission.externalJudgements.first %}
                    {% endif %}
                    {% if external_ccs_submission_url is not empty %}
                        {% set externalSubmissionUrl = submission | externalCcsUrl %}
                    {% else %}
                        {% set externalSubmissionUrl = null %}
                    {% endif %}
                    <td rowspan="2">
                        <a {% if externalSubmissionUrl is not null %}href="{{ externalSubmissionUrl }}"{% endif %}>
                            External
                        </a>
                    </td>
                    <td rowspan="2">
                        <a {% if externalSubmissionUrl is not null %}href="{{ externalSubmissionUrl }}"{% endif %}>
                            {{ submission.externalid }}
                        </a>
                    </td>
                    <td>
                        <a {% if externalSubmissionUrl is not null %}href="{{ externalSubmissionUrl }}"{% endif %}>
                            External
                        </a>
                    </td>
                    <td>
                        <a {% if externalSubmissionUrl is not null %}href="{{ externalSubmissionUrl }}"{% endif %}>
                            {% if submission.source.value != 'shadowing' %}
                                {{- 'internal' | printValidJuryResult -}}
                            {% elseif externalJudgement is null or externalJudgement.result is empty %}
                                {{- 'pending' | printValidJuryResult -}}
                            {% else %}
                                {{- externalJudgement.result | printValidJuryResult -}}
                            {% endif %}
                        </a>
                    </td>
                    {% if is_scoring %}
                        <td class="right">
                            {% if externalJudgement is not null and externalJudgement.score is not null %}
                                {{ externalJudgement.score | number_format(2, '.', '') }}
                            {% else %}
                                {{ '-' }}
                            {% endif %}
                        </td>
                    {% endif %}
                    <td class="testcase-results">
                        {{- submission | testcaseResults(true) -}}
                    </td>
                </tr>
                <tr>
                    <td colspan="{% if is_scoring %}4{% else %}3{% endif %}" class="inline-verify-form">
                        {# Check for score difference on scoring problems #}
                        {% set hasScoreDifference = false %}
                        {% set validJudging = submission.validJudging %}
                        {% if submission.problem.scoringProblem
                            and validJudging
                            and validJudging.result
                            and externalJudgement
                            and externalJudgement.result %}
                            {% set scoreDiffEpsilon = current_contest.scoreDiffEpsilon|default(0.0001) %}
                            {% set hasScoreDifference = (validJudging.score - externalJudgement.score)|abs > scoreDiffEpsilon %}
                        {% endif %}
                        {% if externalJudgement is not null and (not validJudging or validJudging.result != externalJudgement.result or hasScoreDifference) %}
                            {% include 'jury/partials/verify_form.html.twig' with {
                                label: 'Shadow difference verified',
                                judging: externalJudgement,
                                form_action: path('jury_shadow_difference_verify', {extjudgementid: externalJudgement.extjudgementid}),
                                show_form: true,
                                show_icat: false} %}
                        {% endif %}
                    </td>
                </tr>
            {% endif %}
        {%- endfor %}

        </tbody>
    </table>

    {{ knp_pagination_render(submissions) }}
{% endif %}
