{% extends 'templates/cms/assessments/certification_results_list.html' %}
{% load i18n %}

{% block section_table %}
    {% if is_business_unit_manager or is_job_profile_manager %}
        <form action="">
            <!-- Single button -->
            <div class="btn-group pull-right">
                <button type="button" class="btn btn-primary btn-lg dropdown-toggle" data-toggle="dropdown">
                    {% trans 'NAO actions' %} <span class="caret"></span>
                </button>
                <ul class="dropdown-menu">
                    {# Not using a method on the model for url because url is Nissan specific, and model is not Nissan specific #}
                    <li><a href="{% url 'assessments:certification-results-bulk-send-to-lms' %}" class="lms_action">{% trans 'Send to NAO' %}</a></li>
                    {# Not using a method on the model for url because url is Nissan specific, and model is not Nissan specific #}
                    <li><a href="{% url 'assessments:certification-results-bulk-skip' %}" class="lms_action">{% trans 'Treat manually' %}</a></li>
                </ul>
            </div>
        </form>
    {% endif %}

    <table class="table table-hover certification-results">
        <thead>
            <tr>
                <th><input type="checkbox" id="select_all_certification_results"></input></th>
                <th width="200px">{% trans 'Business unit' %}</th>
                <th>{% trans 'NAO user' %}</th>
                <th>{% trans 'Name' %}</th>
                <th>{% trans 'Score' %}</th>
                <th>{% trans 'Session Date' %}</th>
                <th>{% trans 'Status' %}</th>
                {% if is_job_profile_manager %}
                    <th>{% trans 'Force status' %}</th>
                {% endif %}
                {% if is_business_unit_manager or is_job_profile_manager %}
                    <th>{% trans 'NAO status' %}</th>
                {% endif %}
                <th></th>
            </tr>
        </thead>
        <tbody>

            {% for user_certification in certificationprogramuserstatus_list %}

                <tr>
                    <td>
                        <input type="checkbox"
                            class="select_row"
                            value="{{ user_certification.id }}"
                            {% if not user_certification.sync_status == 'pe' and not user_certification.sync_status == 'ko' or not user_certification.user.original_or_self_external_id %}
                                disabled="disabled"
                            {% endif %}>
                        </input>
                    </td>
                    <td>{{ user_certification.session.training.business_unit.name }}</td>
                    <td>
                        {% if not user_certification.user.original_or_self_external_id %}
                            <i class="fa fa-times"></i>
                        {% else %}
                            <i class="fa fa-check"></i>
                        {% endif %}
                    </td>
                    <td>{{ user_certification.user.get_full_name }}</td>
                    <td>{{ user_certification.score }}%</td>
                    <td>{{ user_certification.session.start_date|date:'SHORT_DATE_FORMAT' }}</td>
                    <td>
                        {{ user_certification.status.name }}
                        {% if user_certification.is_forced %}<i class="fa fa-level-up"></i>{% endif %}
                    </td>
                    {% if is_job_profile_manager %}
                        <td>
                            {% if user_certification.sync_status == 'pe' %}
                                <form method="post" action="{{ user_certification.get_update_url }}">{% csrf_token %}
                                    <input type="hidden" name="id" value="{{ user_certification.id }}">
                                    <select name="status">
                                        <option>{% trans 'Set new level' %}</option>
                                        {% for status in available_certification_statuses %}
                                            <option value="{{ status.id }}">
                                                {{ status.name }} ({{ status.global_threshold }}%)
                                            </option>
                                        {% endfor %}
                                    </select>
                                    <input class="btn btn-primary btn-xs" type="submit" value="{% trans 'OK' %}">
                                </form>
                            {% else %}
                                -
                            {% endif %}
                        </td>
                    {% endif %}
                    {% if is_business_unit_manager or is_job_profile_manager %}
                        <td>
                            {% trans user_certification.get_sync_status_display %}
                        </td>
                    {% endif %}
                    <td>
                        <a href="{{ user_certification.get_detail_url }}" class="btn btn-primary btn-xs">
                            <i class="fa fa-eye"></i> {% trans 'View results' %}
                        </a>
                        <a href="{{ user_certification.get_pdf_url }}" class="btn btn-info btn-xs">
                            <i class="fa fa-download"></i> {% trans 'Download' %}
                        </a>
                    </td>
                </tr>

            {% endfor %}

        </tbody>
    </table>

{% endblock %}

{% block section_table_javascript %}

    <div class="modal fade" id="modal-bulk" tabindex="-1" role="dialog"  aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
            </div>
        </div>
    </div>

    <script type="text/javascript">
        // shows a confirmation modal (for bulk sync_status update)
        function _showModal(url, nbChecked) {
            $modal = $('#modal-bulk');
            // inject certification results ids into modal form
            // no django form in modal, see why in modal template
            var fieldsHtml = $.map($('.select_row:checked'), function (row) {
                return '<input type="hidden" value="' + row.value + '" name="certification_result">';
            });
            $.get(url, function (data) {
                $modal.find('.modal-content').html(data);
                $modal.find('.fields').html(fieldsHtml.join(''));
                $modal.find('.nb-items').html(nbChecked);
                $modal.modal('show');
            }).fail(function (err) {
                console.log('Error fetching modal-bulk template', err);
            });
        }

        $(document).ready(function () {
            $('#select_all_certification_results').change(function () {
                $('.select_row:enabled').prop('checked', this.checked);
            });

            $('a.lms_action').click(function (e) {
                e.preventDefault();
                _showModal($(this).prop('href'), $('.select_row:checked').length);
            });
        });
    </script>
{% endblock %}
