{% load custom_tags_and_filters %}
<a onclick="toggle_details(this)"
   data-toggle="collapse"
   data-target="#qualified_users"
   class="pointer">
    <span class="glyphicon glyphicon-user pull-left notification-icon primary-highlight"></span>
    <span class="glyphicon {% if expanded %}glyphicon-chevron-down{% else %}glyphicon-chevron-right{% endif %} pull-left chevron"></span>
</a>
<div class="media-body">
    <h4 class="media-heading">Qualified users</h4>
    <div id="qualified_users" class="collapse {% if expanded %}in{% endif %}">
        <p>Only qualified users are able to operate the {{ tool.name }}.</p>
        {% if tool.grant_physical_access_level_upon_qualification %}
            <p class="danger-highlight">
                Qualifying a user for
                this tool automatically grants them the physical access level &quot;{{ tool.grant_physical_access_level_upon_qualification }}&quot;.
            </p>
        {% endif %}
        <form id="add_qualified_user_form" style="margin-bottom:10px">
            <div class="row">
                {% if qualification_levels %}
                    <div class="col-sm-5 col-md-4 col-lg-3">
                        <div class="form-group">
                            <label class="control-label" for="qualification_level_dropdown">Qualification level</label>
                            <select id="qualification_level_dropdown" class="form-control" name="qualification_level" required>
                                {% if qualification_levels|length > 1 %}<option value="" disabled selected>Select a level</option>{% endif %}
                                {% for ql in qualification_levels %}<option value="{{ ql.id }}">{{ ql.name }}</option>{% endfor %}
                            </select>
                        </div>
                    </div>
                {% endif %}
                <div class="col-sm-6 col-md-4 col-lg-4">
                    <div class="form-group">
                        <label class="control-label" for="add_qualified_user_search_box">Add a qualified user</label>
                        <div class="input-group">
                            <input type="text"
                                   id="add_qualified_user_search_box"
                                   placeholder="Search for a user"
                                   class="form-control"
                                   required>
                            <span class="input-group-btn">
                                <button class="btn btn-default" type="button" onclick="qualify_user()">Add</button>
                            </span>
                        </div>
                    </div>
                </div>
            </div>
            <input type="hidden" name="chosen_tool[]" value="{{ tool.id }}">
            <input type="hidden" name="action" value="qualify">
        </form>
        {% if qualifications %}
            <p>
                You can email <a href="{% url 'compose_email' %}?audience=tool&selection={{ tool.id }}"><span class="glyphicon glyphicon-send"></span> all qualified users</a>.
            </p>
            <p>
                Current qualified users are listed below. Inactive qualified users are <span class="strikethrough light-grey">crossed out</span>.
                <br>
                {% for qualification in qualifications %}
                    {% with user=qualification.user %}
                        {% if user.is_active %}
                            <a href="javascript:remove_qualified_user('{{ user.id }}', '{{ tool.id }}')"
                               class="grey hover-black"
                               title="Disqualify {{ user.first_name }}">
                                <span class="glyphicon glyphicon-remove-circle"></span>
                            </a>
                            {{ user }} - {{ qualification.qualified_on|date:"SHORT_DATE_FORMAT" }}
                            {% if qualification.qualification_level %}- {{ qualification.qualification_level.name }}{% endif %}
                            {% if request.user.is_any_part_of_staff and qualification.training_details %}
                                - {{ qualification.training_details }}
                            {% endif %}
                        {% else %}
                            <a href="javascript:remove_qualified_user('{{ user.id }}', '{{ tool.id }}')"
                               class="grey hover-black"
                               title="Disqualify {{ user.first_name }}">
                                <span class="glyphicon glyphicon-remove-circle"></span>
                            </a>
                            <span class="strikethrough light-grey">{{ user }} - {{ qualification.qualified_on|date:"SHORT_DATE_FORMAT" }}
                                {% if qualification.qualification_level %}- {{ qualification.qualification_level.name }}{% endif %}
                            </span>
                        {% endif %}
                    {% endwith %}
                    <br>
                {% endfor %}
            </p>
        {% else %}
            No qualified users found.
        {% endif %}
    </div>
</div>
<script>
	$('#add_qualified_user_search_box').autocomplete('users', add_qualified_user, {{ users|json_search_base }}).focus();
</script>
