{% extends 'oscar/dashboard/layout.html' %}
{% load sorting_tags %}
{% load i18n %}
{% block title %}
    {% trans "Partners" %} | {{ block.super }}
{% endblock %}
{% block breadcrumbs %}
    <nav aria-label="breadcrumb">
        <ol class="breadcrumb">
            <li class="breadcrumb-item">
                <a href="{% url 'dashboard:index' %}">{% trans "Dashboard" %}</a>
            </li>
            <li class="breadcrumb-item active" aria-current="page">
                {% trans "Partners" %}
            </li>
        </ol>
    </nav>
{% endblock %}
{% block header %}
    <div class="page-header">
        <a href="{% url 'dashboard:partner-create' %}" class="btn btn-primary float-right">
            <i class="fas fa-plus-circle"></i>
            {% trans "Create new partner" %}
        </a>
        <h1>{% trans "Partners" %}</h1>
    </div>
{% endblock header %}
{% block dashboard_content %}
    <div class="card card-body bg-light">
        <form method="get" class="form-inline">
            {% include 'oscar/dashboard/partials/form_fields_inline.html' with form=form %}
            <button type="submit" class="btn btn-primary" data-loading-text="{% trans 'Searching...' %}">
                {% trans "Search" %}
            </button>
            {% if is_filtered %}
                <a class="btn btn-secondary" href="{% url 'dashboard:partner-list' %}" >
                    {% trans "Reset" %}
                </a>
            {% endif %}
        </form>
    </div>
    <form method="post" class="order_table">
        {% csrf_token %}
        <table class="table table-striped table-bordered">
            <caption>
            <i class="fas fa-briefcase"></i>
            {{ queryset_description }}
            </caption>
            {% if partners %}
                <tr>
                    <th>{% anchor 'name' _('Name') %}</th>
                    <th>{% trans 'Users' %}</th>
                    <th>{% trans 'Addresses' %}</th>
                    <th>&nbsp;</th>
                </tr>
                {% for partner in partners %}
                    <tr>
                        <td>
                            <a href="{% url 'dashboard:partner-manage' pk=partner.pk %}">
                                {{ partner.name }}
                            </a>
                        </td>
                        <td>
                            {% with users=partner.users.all %}
                                {% if users %}
                                    <ul>
                                        {% for user in users %}
                                            <li>
                                                <a href="{% url 'dashboard:partner-user-update' partner_pk=partner.pk user_pk=user.id %}"
                                                   title="{% trans ">
                                                    {{ user.get_full_name|default:user.email }}
                                                </a>
                                            </li>
                                        {% endfor %}
                                    </ul>
                                {% else %}
                                    {% trans 'No users associated.' %}
                                {% endif %}
                            {% endwith %}
                        </td>
                        <td>
                            {% with addresses=partner.addresses.all %}
                                {% if addresses %}
                                    <ul>
                                        {% for address in addresses %}
                                            <li>
                                                {{ address.summary }}
                                            </li>
                                        {% endfor %}
                                    </ul>
                                {% else %}
                                    {% trans 'No address associated.' %}
                                {% endif %}
                            {% endwith %}
                        </td>
                        <td>
                            <div class="btn-toolbar">
                                <div class="dropdown">
                                    <button class="btn btn-secondary dropdown-toggle"
                                            type="button"
                                            id="dropdownMenuButton"
                                            data-toggle="dropdown"
                                            aria-haspopup="true"
                                            aria-expanded="false">
                                        {% trans "Actions" %}
                                    </button>
                                    <div class="dropdown-menu dropdown-menu-right"
                                         aria-labelledby="dropdownMenuButton">
                                        <a class="dropdown-item"
                                           href="{% url 'dashboard:partner-manage' pk=partner.pk %}">
                                            {% trans "Manage partner and users" %}
                                        </a>
                                        <a class="dropdown-item"
                                           href="{% url 'dashboard:partner-delete' pk=partner.pk %}">
                                            {% trans "Delete partner" %}
                                        </a>
                                        <a class="dropdown-item"
                                           href="{% url 'dashboard:partner-user-select' partner_pk=partner.pk %}">
                                            {% trans "Link an existing user" %}
                                        </a>
                                        <a class="dropdown-item"
                                           href="{% url 'dashboard:partner-user-create' partner_pk=partner.pk %}">
                                            {% trans "Link a new user" %}
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </td>
                    </tr>
                {% endfor %}
            {% else %}
                <tr>
                    <td>{% trans "No partners found." %}</td>
                </tr>
            {% endif %}
        </table>
        {% include "oscar/dashboard/partials/pagination.html" %}
    </form>
{% endblock dashboard_content %}
