{% extends "orga/base.html" %}
{% load bootstrap4 %}
{% load compress %}
{% load i18n %}
{% load review_score %}
{% load rules %}
{% load url_replace %}
{% load static %}

{% block scripts %}
    {% compress js %}
        <script src="{% static 'vendored/rslider/rSlider.min.js' %}" defer></script>
        <script src="{% static "orga/js/submission_filter.js" %}"></script>
        <script src="{% static 'orga/js/review.js' %}" defer></script>
    {% endcompress %}
    <link rel="stylesheet" type="text/css" href="{% static "vendored/rslider/rSlider.min.css" %}" />
{% endblock %}

{% block content %}
    {% has_perm 'orga.perform_reviews' request.user request.event as can_review %}
    {% has_perm 'orga.view_speakers' request.user request.event as can_view_speakers %}
    <div class="alert alert-info">
        {% if can_review and next_submission %}
            <a href="{{ next_submission.orga_urls.reviews }}">
                {{ missing_reviews }}
                {% blocktranslate trimmed count count=missing_reviews %}
                    proposal is waiting for your review.
                {% plural %}
                    proposals are waiting for your review.
                {% endblocktranslate %}
                {% translate "Click here to get started!" %}
            </a>
        {% elif can_review %}
            {% translate "You've got no proposals left to review!" %}
        {% else %}
            {% translate "Reviews are currently closed." %}
        {% endif %}
    </div>

    <div class="submit-group search-submit-group">
        <form class="search-form" id="review-filter-form">
            {% bootstrap_form search_form %}
            {% if show_submission_types and filter_form.submission_type %}{% bootstrap_field filter_form.submission_type %}{% endif %}
            <div class="d-flex flex-column form-group">
                {% bootstrap_field filter_form.state layout='inline' %}
                <div id="pending" class="d-none">{% bootstrap_field filter_form.pending_state__isnull layout='inline' %}</div>
            </div>
            {% if filter_form.track %}{% bootstrap_field filter_form.track %}{% endif %}
            {% if filter_form.content_locale %}{% bootstrap_field filter_form.content_locale %}{% endif %}
            {% if filter_form.tags %}{% bootstrap_field filter_form.tags %}{% endif %}
            {% if max_review_count and max_review_count > 1 %}
                <div id="range-wrapper">
                    <label for="review-count">{% translate "Number of reviews" %}</label>
                    <input type="text" id="review-count" name="review-count" data-max="{{ max_review_count }}"/>
                </div>
            {% endif %}
            <div class="form-group w-auto">
                <button class="btn btn-success" type="submit">{% translate "Search" %}</button>
            </div>
            <div class="ml-auto d-flex">
                <details class="dropdown flip ml-auto fix-height">
                    <summary class="btn btn-outline-info">
                        {% translate "Columns" %} <i class="fa fa-caret-down"></i>
                    </summary>
                    <ul id="column-select" class="dropdown-content dropdown-content-s{% if rtl %}w{% else %}e{% endif %}">
                        {% if can_see_all_reviews %}
                            <li class="dropdown-item form-check">
                                <input type="checkbox" id="col-score-avg" checked autocomplete=off>
                                <label for="col-score-avg">{% translate "Score" %}</label>
                            </li>
                        {% endif %}
                        {% if can_review or submissions_reviewed %}
                            <li class="dropdown-item form-check">
                                <input type="checkbox" id="col-score-user" checked autocomplete=off>
                                <label for="col-score-user">{% translate "Your score" %}</label>
                            </li>
                        {% endif %}
                        {% if can_review or submissions_reviewed %}
                            <li class="dropdown-item form-check">
                                <input type="checkbox" id="col-review-count" checked autocomplete=off>
                                <label for="col-review-count">{% translate "Reviews" %}</label>
                            </li>
                        {% endif %}
                        {% if can_view_speakers %}
                            <li class="dropdown-item form-check">
                                <input type="checkbox" id="col-speakers" checked autocomplete=off>
                                <label for="col-speakers">{% translate "Speakers" %}</label>
                            </li>
                        {% endif %}
                        {% for question in short_questions %}
                            <li class="dropdown-item form-check">
                                <input type="checkbox" id="col-question-{{ question.id }}" autocomplete=off>
                                <label for="col-question-{{ question.id }}">{% translate "Question" %}: {{ question.question }}</label>
                            </li>
                        {% endfor %}
                        <li class="dropdown-item form-check">
                            <input type="checkbox" id="col-track" checked autocomplete=off>
                            <label for="col-track">{% translate "Track" %}</label>
                        </li>
                        <li class="dropdown-item form-check">
                            <input type="checkbox" id="col-duration" autocomplete=off>
                            <label for="col-duration">{% translate "Duration" %}</label>
                        </li>
                        <li class="dropdown-item form-check">
                            <input type="checkbox" id="col-subtype" autocomplete=off>
                            <label for="col-subtype">{% translate "Session type" %}</label>
                        </li>
                        {% if filter_form.tags %}
                            <li class="dropdown-item form-check">
                                <input type="checkbox" id="col-tags" checked autocomplete=off>
                                <label for="col-tags">{% translate "Tags" %}</label>
                            </li>
                        {% endif %}
                        <li class="dropdown-item form-check">
                            <input type="checkbox" id="col-state" checked autocomplete=off>
                            <label for="col-state">{% translate "State" %}</label>
                        </li>
                    </ul>
                </details>
                <details class="dropdown flip fix-height">
                    <summary class="btn btn-info">
                        {% translate "Actions" %} <i class="fa fa-caret-down"></i>
                    </summary>
                    <ul class="dropdown-content dropdown-content-s{% if rtl %}e{% else %}w{% endif %}">
                        <li><a class="dropdown-item" href="{{ request.event.orga_urls.reviews }}regenerate">
                            <i class="fa fa-link"></i> {% translate "Regenerate decision emails" %}
                        </a></li>
                        <li><a class="dropdown-item" href="{{ request.event.orga_urls.apply_pending }}?next={{ request.path }}">
                            <i class="fa fa-link"></i> {% translate "Apply pending changes" %}
                        </a></li>
                    </ul>
                </details>
            </div>
        </form>
        {% if filter_form.is_valid and filter_form.cleaned_data.question %}
            <p class="text-muted ml-2">
                <span class="fa fa-filter"></span>
                {% blocktranslate trimmed with question=filter_form.cleaned_data.question.question %}
                    List filtered by answers to question "{{ question }}".
                {% endblocktranslate %}
                <a href="?{% url_replace request 'question' '' 'answer' '' 'answer__options' '' %}" class="text-muted">
                    <span class="fa fa-times"></span>
                    {% translate "Remove filter" %}
                </a>
            </p>
        {% endif %}
    </div>

    <form method="post">{% csrf_token %}
        <div class="table-responsive">
            <table class="table review-table table-hover table-flip">
                <colgroup>
                    {% if can_see_all_reviews %}
                        <col class="col-score-avg nowrap">
                        {% for category in independent_categories %}<col class="col-score-avg nowrap">{% endfor %}
                    {% endif %}
                    {% if can_review or submissions_reviewed %}
                        <col class="nowrap col-score-user">
                        {% if independent_categories and not can_see_all_reviews %}
                            {% for category in independent_categories %}<col class="col-score-avg nowrap">{% endfor %}
                        {% endif %}
                    {% endif %}
                    <col class="nowrap col-review-count">
                    <col class="w-75">
                    {% if can_view_speakers %}<col class="w-25 col-speakers">{% endif %}
                    {% for question in short_questions %}<col class="col-question-{{ question.id }} d-none">{% endfor %}
                    {% if filter_form.track %}<col class="nowrap col-track">{% endif %}
                    {% if filter_form.tags %}<col class="nowrap col-tags">{% endif %}
                    <col class="nowrap d-none col-duration">
                    {% if show_submission_types %}<col class="nowrap d-none col-subtype">{% endif %}
                    <col class="nowrap col-state">
                    <col class="nowrap">
                </colgroup>
                <thead>
                    <tr>
                        {% if can_see_all_reviews %}
                            <th class="col-score-avg">
                                {% if request.event.review_settings.aggregate_method == "median" %}
                                    {% translate "Median" %}
                                {% else %}
                                    {% translate "Average" %}
                                {% endif %}
                                <a href="?{% url_replace request 'sort' 'score' %}"><i class="fa fa-caret-down" title="{% translate "Sort by score (highest first)" %}"></i></a>
                                <a href="?{% url_replace request 'sort' '-score' %}"><i class="fa fa-caret-up" title="{% translate "Sort by score (lowest first)" %}"></i></a>
                            </th>
                            {% if independent_categories %}
                                {% for category in independent_categories %}
                                    <th class="col-score-avg"> {{ category.name }}</th>
                                {% endfor %}
                            {% endif %}
                        {% endif %}
                        {% if can_review or submissions_reviewed %}
                            <th class="col-score-user">
                                {% translate "Your score" %}
                                <a href="?{% url_replace request 'sort' 'my_score' %}"><i class="fa fa-caret-down" title="{% translate "Sort by score (highest first)" %}"></i></a>
                                <a href="?{% url_replace request 'sort' '-my_score' %}"><i class="fa fa-caret-up" title="{% translate "Sort by score (lowest first)" %}"></i></a>
                            </th>
                            {% if independent_categories and not can_see_all_reviews %}
                                {% for category in independent_categories %}
                                    <th class="col-score-user"> {{ category.name }}</th>
                                {% endfor %}
                            {% endif %}
                        {% endif %}
                        <th class="col-review-count">
                            {% translate "Reviews" %}
                            <a href="?{% url_replace request 'sort' 'count' %}"><i class="fa fa-caret-down" title="{% translate "Sort by review count (highest first)" %}"></i></a>
                            <a href="?{% url_replace request 'sort' '-count' %}"><i class="fa fa-caret-up" title="{% translate "Sort by review count (lowest first)" %}"></i></a>
                        </th>
                        <th>{% translate "Title" %}</th>
                        {% if can_view_speakers %}<th class="col-speakers">{% translate "Speakers" %}</th>{% endif %}
                        {% for question in short_questions %}
                            <th class="col-question-{{ question.id }} d-none">{{ question.question }}</th>
                        {% endfor %}
                        {% if filter_form.track %}<th class="col-track">{% translate "Track" %}</th>{% endif %}
                        <th class="d-none col-duration">{% translate "Duration" %}</th>
                        {% if show_submission_types %}<th class="col-subtype d-none">{% translate "Type" %}</th>{% endif %}
                        {% if filter_form.tags %}<th class="col-tags">{% translate "Tags" %}</th>{% endif %}
                        <th class="col-state">{% translate "State" %}</th>
                        <th><span class="action-row d-flex justify-space-around">
                            {% if can_accept_submissions %}
                                <div class="radio form-check accept">
                                    <input type="radio" id="a-all" name="s-all" value="accept">
                                    <label for="a-all" title="{% translate "Accept all" %}"></label>
                                </div>
                                <div class="radio form-check reject">
                                    <input type="radio" id="r-all" name="s-all" value="reject">
                                    <label for="r-all" title="{% translate "Reject all" %}"></label>
                                </div>
                                <div class="unmark-radio always-active" id="u-all" title="{% translate "Unset accept/reject vote for all" %}"><i class="fa fa-ban"></i></div>
                            {% endif %}
                        </span></th>
                    </tr>
                </thead>
                <tbody>
                    {% for submission in submissions %}
                        <tr class="{{ submission.state }}">
                            {% if can_see_all_reviews %}
                                <td class="text-center col-score-avg">
                                    {% review_score submission %}
                                </td>
                                {% if independent_categories %}
                                    {% for score in submission.independent_scores %}
                                        <td class="col-score-avg"> {% if score is None %}-{% else %}{{ score }}{% endif %}</td>
                                    {% endfor %}
                                {% endif %}
                            {% endif %}
                            {% if can_review or submissions_reviewed %}
                                <td class="text-center col-score-user">
                                    {% review_score submission True %}
                                </td>
                                {% if not can_see_all_reviews and independent_categories %}
                                    {% for score in submission.independent_scores %}
                                        <td class="col-score-user"> {% if score is None %}-{% else %}{{ score }}{% endif %}</td>
                                    {% endfor %}
                                {% endif %}
                            {% endif %}
                            <td class="text-center col-review-count">
                                {{ submission.review_nonnull_count|default:'-' }}
                                {% if submission.review_count != submission.review_nonnull_count %}({{ submission.review_count|default:'-' }}){% endif %}
                                {% if submission.pk in submissions_reviewed %}
                                    <i class="fa fa-check text-success" title="{% translate "You have reviewed this proposal" %}"></i>
                                {% elif request.user in submission.speakers.all %}
                                    <i class="fa fa-check text-muted" title="{% translate "You cannot review this proposal" %}"></i>
                                {% endif %}
                                {% if submission.is_assigned %}
                                    <i class="fa fa-star text-warning" title="{% translate "You have been assigned to this proposal" %}"></i>
                                {% endif %}
                            </td>
                            <td>
                                <a href="{% if can_review %}{{ submission.orga_urls.reviews }}{% else %}{{ submission.orga_urls.base }}{% endif %}">
                                    {% if can_view_speakers %}{{ submission.title }}{% else %}{{ submission.anonymised.title|default:submission.title }}{% endif %}
                                </a>
                            </td>
                            {% if can_view_speakers %}<td class="col-speakers">
                                {% for speaker in submission.speakers.all %}
                                    {{ speaker.get_display_name }}{% if not forloop.last %},{% endif %}<br>
                                {% endfor %}
                            </td>{% endif %}
                            {% if short_questions %}
                                {% for answer in submission.short_answers %}
                                    <td class="col-question-{{ answer.question_id }} d-none">
                                        {{ answer.answer_string }}
                                    </td>
                                {% endfor %}
                            {% endif %}
                            {% if filter_form.track %}<td class="col-track">{{ submission.track.name }}</td>{% endif %}
                            <td class="d-none col-duration">{{ submission.export_duration }}</td>
                            {% if show_submission_types %}<td class="col-subtype d-none">{{ submission.submission_type.name }}</td>{% endif %}
                            {% if filter_form.tags %}<td class="col-tags">
                                <div class="d-flex flex-column">
                                    {% for tag in submission.tags.all %}
                                        {{ tag.tag }}
                                    {% endfor %}
                                </div>
                            </td>{% endif %}
                            <td class="nowrap col-state">
                                {% include "cfp/event/fragment_state.html" with state=submission.state %}
                                {% if submission.pending_state %}<br>
                                    ({% translate "pending" %} {% include "cfp/event/fragment_state.html" with state=submission.pending_state %})
                                {% endif %}
                            </td>
                            <td class="action-row d-flex justify-space-around">
                                {% if submission.state == 'submitted' and can_accept_submissions %}
                                    <div class="radio form-check accept">
                                        <input type="radio" id="a-{{ submission.code }}" name="s-{{ submission.code }}" value="accept">
                                        <label for="a-{{ submission.code }}" title="{% translate "Accept" %}"></label>
                                    </div>
                                    <div class="radio form-check reject">
                                        <input type="radio" id="r-{{ submission.code }}" name="s-{{ submission.code }}" value="reject">
                                        <label for="r-{{ submission.code }}" title="{% translate "Reject" %}"></label>
                                    </div>
                                    <div class="unmark-radio" title="{% translate "Unset accept/reject vote" %}"><i class="fa fa-ban"></i></div>
                                {% endif %}
                            </td>
                        </tr>
                    {% empty %}
                        <tr>
                            <td colspan=10>{% translate "You don't seem to have any proposals yet." %}</td>
                        </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
        <div id="submitBar">
            <div id="pending" class="form-check mr-2">
                <input type="checkbox" id="id_pending" name="pending">
                <label for="id_pending">{% translate "Mark new states as 'pending'" %}</label>
            </div>
            <i class="fa fa-question-circle mr-4" data-toggle="tooltip" data-placement="top" title="{% translate "If you mark state changes as pending, they won't be visible to speakers right away. You can always apply pending changes for some or all proposals in one go once you're ready to make your decisions public." %}"></i>
            <span id="submitText" class="d-none ml-2">
                {% translate "Accept" %}: <span id="acceptCount" class="text-success"></span>
                {% translate "Reject" %}: <span id="rejectCount" class="text-danger"></span>
            </span>
            <button type="submit" class="btn btn-success">{% translate "Go!" %}</button>
        </div>
    </form>
{% endblock %}
