{% extends 'events/surveys/management/_survey_base.html' %}

{% set has_users = submitted_surveys|rejectattr('is_anonymous')|any %}

{% block content %}
    {% if survey.private and survey.state.name in ('active_and_clean', 'active_and_answered') %}
        <div class="action-box highlight">
            <div class="section">
                <div class="icon icon-lock-center"></div>
                <div class="text">
                    <div class="label">
                        {%- trans %}The survey is private{% endtrans -%}
                    </div>
                    {% trans -%}
                        Share the secret link with anyone you want to submit a response.
                    {%- endtrans %}
                </div>
                <div class="toolbar">
                    <a href="{{ url_for('.display_survey_form', survey, survey.locator.token, _external=true) }}"
                       class="js-copy-to-clipboard i-button highlight icon-copy"
                       title="{% trans %}Copy link to clipboard{% endtrans %}"
                       data-clipboard-text="{{ url_for('.display_survey_form', survey, survey.locator.token, _external=true) }}"
                       data-clipboard-action="copy">
                        {% trans %}Copy secret link{% endtrans %}
                    </a>
                </div>
            </div>
        </div>
    {% endif %}

    {% if survey.state.name == 'not_ready' %}
        {% include 'events/surveys/management/survey_actions/not_ready.html' %}
    {% elif survey.state.name == 'ready_to_open' %}
        {% include 'events/surveys/management/survey_actions/ready_to_open.html' %}
    {% elif survey.state.name in ('active_and_clean', 'active_and_answered') %}
        {% include 'events/surveys/management/survey_actions/active.html' %}
    {% elif survey.state.name == 'finished' %}
        {% include 'events/surveys/management/survey_actions/finished.html' %}
    {% elif survey.state.name == 'limit_reached' %}
        {% include 'events/surveys/management/survey_actions/limit_reached.html' %}
    {% endif %}

    <div class="action-box">
        {% if not survey.event.is_locked %}
            <div class="section">
                <div class="icon icon-settings"></div>
                <div class="text">
                    <div class="label">
                        {% trans %}General Settings{% endtrans %}
                    </div>
                    {% trans %}Edit settings for this survey.{% endtrans %}
                </div>
                <div class="toolbar">
                    <button class="i-button icon-edit"
                            data-href="{{ url_for('.edit_survey', survey) }}"
                            data-title="{% trans %}Edit survey{% endtrans %}"
                            data-ajax-dialog
                            data-reload-after>
                        {% trans %}Edit{% endtrans %}
                    </button>
                </div>
            </div>
        {% endif %}

        {% if survey.state.name != 'not_ready' %}
            <div class="section">
                <div class="icon icon-clipboard"></div>
                <div class="text">
                    <div class="label">
                        {% trans %}Questionnaire{% endtrans %}
                    </div>
                    {% trans %}Add, remove or modify questions.{% endtrans %}
                </div>
                <div class="toolbar">
                    <a href="{{ url_for('.manage_questionnaire', survey) }}" class="i-button icon-settings">
                        {% trans %}Manage{% endtrans %}
                    </a>
                </div>
            </div>
        {% endif %}
        <div class="section">
            <div class="icon icon-mail"></div>
            <div class="text">
                <div class="label">
                    {% trans %}Contact event participants{% endtrans %}
                </div>
                {% trans %}Send an email to the event participants about this survey.{% endtrans %}
            </div>
            <div class="toolbar">
                <div id="email-submission-container" class="i-button-wrapper" data-event-id="{{ event.id }}"
                     data-survey-id="{{ survey.id }}" {% if survey.is_active %}data-survey-active{% endif %}>
                </div>
            </div>
        </div>
    </div>

    <h2>
        {% trans %}Submissions{% endtrans %}
    </h2>

    {% if submitted_surveys %}
        <div class="toolbars space-after">
            <div class="toolbar">
                <button class="i-button icon-checkbox-checked arrow js-dropdown" data-toggle="dropdown"></button>
                <ul class="i-dropdown">
                    <li>
                        <a href="#" id="select-all">{% trans 'Selection' %}All{% endtrans %}</a>
                    </li>
                    <li>
                        <a href="#" id="select-none">{% trans 'Selection' %}None{% endtrans %}</a>
                    </li>
                </ul>
            </div>
            <div class="toolbar">
                <a href="{{ url_for('.survey_results', survey) }}" class="i-button icon-chart">
                    {% trans %}Results{% endtrans %}
                </a>
                <button class="i-button arrow js-dropdown"
                        data-toggle="dropdown">
                    {%- trans %}Export{% endtrans -%}
                </button>
                <ul class="i-dropdown">
                    <li>
                        <a href="#" data-href="{{ url_for('.export_submissions_csv', survey) }}"
                           class="icon-file-spreadsheet js-submission-action js-export-submissions">
                            {% trans %}CSV{% endtrans %}
                        </a>
                    </li>
                    <li>
                        <a href="#" data-href="{{ url_for('.export_submissions_excel', survey) }}"
                           class="icon-file-excel js-submission-action js-export-submissions">
                            {% trans %}XLSX (Excel){% endtrans %}
                        </a>
                    </li>
                </ul>
                <button id="delete-submissions"
                        class="i-button icon-remove text-color color-on-hover danger hide-if-locked"
                        data-href="{{ url_for('.delete_submissions', survey) }}"
                        data-title="{% trans %}Remove all selected submissions?{% endtrans %}"
                        data-confirm="{% trans %}Are you sure you want to remove all selected submissions?{% endtrans %}"
                        data-method="DELETE"
                        disabled>
                    {% trans %}Delete{% endtrans %}
                </button>
            </div>
        </div>

        <table id="submission-list" class="i-table-widget fixed-width">
            <thead>
                <tr>
                    <th></th>
                    <th>{% trans %}ID{% endtrans %}</th>
                    <th>{% trans %}Submitted{% endtrans %}</th>
                    <th>{% trans %}User{% endtrans %}</th>
                    {% if has_users %}
                        <th>{% trans %}Email{% endtrans %}</th>
                    {% endif %}
                    <th></th>
                </tr>
            </thead>
            <tbody>
                {% for submission in submitted_surveys|sort(attribute='submitted_dt') %}
                    {{ _render_submission_row(submission) }}
                {% endfor %}
            </tbody>
        </table>
    {% else %}
        <div class="info-message-box">
            <div class="message-text">
                {% trans %}There are no submissions yet.{% endtrans %}
            </div>
        </div>
    {% endif %}
    <script>
        setupEmailSurveyParticipantsButton('#email-submission-container');
    </script>
{% endblock %}

{% macro _render_submission_row(submission) %}
    <tr class="submission-row">
        <td>
            <input type="checkbox" class="inline-vcentered submission-ids" value="{{ submission.id }}">
        </td>
        <td>
            <a href="{{ url_for('.display_submission', submission) }}">#{{ submission.friendly_id }}</a>
        </td>
        <td>{{ submission.submitted_dt|format_datetime(timezone=submission.survey.event.tzinfo) }}</td>
        <td>
            {% if not submission.is_anonymous %}
                {{ submission.user.full_name }}
            {% else %}
                <em>{% trans %}Anonymous{% endtrans %}</em>
            {% endif %}
        </td>
        {% if has_users %}
            <td>
                {% if not submission.is_anonymous %}
                    {{ submission.user.email }}
                {% endif %}
            </td>
        {% endif %}
        <td>
            {% set data_confirm %}
                {% trans %}Are you sure you want to remove this submission?{% endtrans %}
                {% if submission.is_anonymous %}
                    {% trans %}
                        Note: Authenticated users will not be able to submit an anonymous survey again
                        even if you delete their submission.
                    {% endtrans %}
                {% endif %}
            {% endset %}
            <a href="#" class="icon-remove right js-delete-submission hide-if-locked"
               data-href="{{ url_for('.delete_submissions', survey) }}"
               data-method="DELETE"
               data-title
               title="{% trans %}Remove submission{% endtrans %}"
               data-confirm="{{ data_confirm }}"
               data-submission-id="{{ submission.id }}"></a>
        </td>
    </tr>
{% endmacro %}
