{% load i18n widget_tweaks %}

<tr class="show-duplicates" data-user-id="{{ mtpuser.pk }}">
    <td>
        <i class="fa fa-caret-right"></i>
        <i class="fa fa-caret-down"></i>
    </td>
    <td>{{ mtpuser.username }}</td>
    <td>{{ mtpuser.first_name }}</td>
    <td>{{ mtpuser.last_name }}</td>
    <td>{{ mtpuser.email }}</td>
    <td>{{ mtpuser.get_created_from_display }}</td>
    <td>{{ mtpuser.date_joined }}</td>
    <td>
        {% with duplicates_length=mtpuser.duplicate_users|length potential_duplicates_length=mtpuser.potential_duplicates|length %}
        <span class="
            {% if duplicates_length != potential_duplicates_length|add:duplicates_length %}
            text-danger
            {% else %}
            text-success
            {% endif %}
          ">
            {{ duplicates_length }} / {{ potential_duplicates_length|add:duplicates_length }}
        </span>
        {% endwith %}
    </td>
</tr>
{% for duplicate_user in mtpuser.potential_duplicates %}
    <tr class="danger duplicates-container duplicates-{{ mtpuser.pk }}">
        <td>
            <a href="" class="text-primary" data-toggle="modal" data-target="#info-potential-duplicates">
                <i class="fa fa-info-circle"></i>
            </a>
        </td>
        <td>{{ duplicate_user.username }}</td>
        <td>{{ duplicate_user.first_name }}</td>
        <td>{{ duplicate_user.last_name }}</td>
        <td>{{ duplicate_user.email }}</td>
        <td>{{ duplicate_user.get_created_from_display }}</td>
        <td>{{ duplicate_user.date_joined }}</td>
        <td>
            <div class="btn-group">
                <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
                    {% trans "Actions" %}
                    <span class="caret"></span>
                </button>
                <ul class="dropdown-menu">
                    <li>
                        <a class="dialog-box" href="{% url 'accounts:user-merge' user_uuid=mtpuser.id %}?duplicate_user_uuid={{ duplicate_user.id }}">
                            <i class="fa fa-code-fork"></i>
                            {% trans 'Merge' %}
                        </a>
                    </li>
                    <li>
                        <a class="dialog-box" href="{% url 'accounts:user-not-a-duplicate' user_uuid=mtpuser.id %}?duplicate_user_uuid={{ duplicate_user.id }}">
                            <i class="fa fa-times"></i>
                            {% trans 'Not a duplicate' %}
                        </a>
                    </li>
                </ul>
            </div>
        </td>
    </tr>
{% endfor %}
{% for duplicate_user in mtpuser.duplicate_users %}
    <tr class="success duplicates-container duplicates-{{ mtpuser.pk }}">
        <td></td>
        <td>{{ duplicate_user.username }}</td>
        <td>{{ duplicate_user.first_name }}</td>
        <td>{{ duplicate_user.last_name }}</td>
        <td>{{ duplicate_user.email }}</td>
        <td>{{ duplicate_user.get_created_from_display }}</td>
        <td>{{ duplicate_user.date_joined }}</td>
        <td>
            <strong class="text-success">{% trans 'Merged' %}</strong>
            {{ duplicate_user.original_users.first.origin_user.username }}
        </td>
    </tr>
{% endfor %}
<tr class="duplicates-container duplicates-{{ mtpuser.pk }}">
    <td></td>
    <td colspan="7">
        <form class="form" style="width: 200px" id="search-duplicate-{{ mtpuser.pk }}">
            {% for field in search_duplicate_form %}
                <div class="form-group">
                    {% render_field field class+="form-control" %}
                </div>
            {% endfor %}
            <script type="text/javascript">
                $(document).ready(function(){
                    var merge_user_url = '{% url 'accounts:user-merge' user_uuid=mtpuser.id %}';
                    $('#search-duplicate-{{ mtpuser.pk }} select').change(function(){
                        if ($(this).val() !== '') {
                            showModal(merge_user_url + '?duplicate_user_uuid=' + $(this).val());
                        }
                    });
                });
            </script>
        </form>
    </td>
</tr>
