{% extends "administration/base.html" %}

{% load i18n %}
{% load passbook_utils %}

{% block content %}
<section class="pf-c-page__main-section pf-m-light">
    <div class="pf-c-content">
        <h1>
            <i class="pf-icon pf-icon-infrastructure"></i>
            {% trans 'Policies' %}
        </h1>
        <p>{% trans "Allow users to use Applications based on properties, enforce Password Criteria and selectively apply Stages." %}</p>
    </div>
</section>
<section class="pf-c-page__main-section pf-m-no-padding-mobile">
    <div class="pf-c-card">
        {% if object_list %}
        <div class="pf-c-toolbar">
            <div class="pf-c-toolbar__content">
                {% include 'partials/toolbar_search.html' %}
                <div class="pf-c-toolbar__bulk-select">
                    <pb-dropdown class="pf-c-dropdown">
                        <button class="pf-m-primary pf-c-dropdown__toggle" type="button">
                            <span class="pf-c-dropdown__toggle-text">{% trans 'Create' %}</span>
                            <i class="fas fa-caret-down pf-c-dropdown__toggle-icon" aria-hidden="true"></i>
                        </button>
                        <ul class="pf-c-dropdown__menu" hidden>
                            {% for type, name in types.items %}
                            <li>
                                <pb-modal-button href="{% url 'passbook_admin:policy-create' %}?type={{ type }}">
                                    <button slot="trigger" class="pf-c-dropdown__menu-item">
                                        {{ name|verbose_name }}<br>
                                        <small>
                                            {{ name|doc }}
                                        </small>
                                    </button>
                                    <div slot="modal"></div>
                                </pb-modal-button>
                            </li>
                            {% endfor %}
                        </ul>
                    </pb-dropdown>
                </div>
                {% include 'partials/pagination.html' %}
            </div>
        </div>
        <table class="pf-c-table pf-m-compact pf-m-grid-xl" role="grid">
            <thead>
                <tr role="row">
                    <th role="columnheader" scope="col">{% trans 'Name' %}</th>
                    <th role="columnheader" scope="col">{% trans 'Type' %}</th>
                    <th role="cell"></th>
                </tr>
            </thead>
            <tbody role="rowgroup">
                {% for policy in object_list %}
                <tr role="row">
                    <th role="columnheader">
                        <div>
                            <div>{{ policy.name }}</div>
                            {% if not policy.bindings.exists and not policy.promptstage_set.exists %}
                            <i class="pf-icon pf-icon-warning-triangle"></i>
                            <small>{% trans 'Warning: Policy is not assigned.' %}</small>
                            {% else %}
                            <i class="pf-icon pf-icon-ok"></i>
                            <small>{% blocktrans with object_count=policy.bindings.all|length %}Assigned to {{ object_count }} objects.{% endblocktrans %}</small>
                            {% endif %}
                        </div>
                    </th>
                    <td role="cell">
                        <span>
                            {{ policy|verbose_name }}
                        </span>
                    </td>
                    <td>
                        <pb-modal-button href="{% url 'passbook_admin:policy-update' pk=policy.pk %}">
                            <button slot="trigger" class="pf-c-button pf-m-secondary">
                                {% trans 'Edit' %}
                            </button>
                            <div slot="modal"></div>
                        </pb-modal-button>
                        <pb-modal-button href="{% url 'passbook_admin:policy-test' pk=policy.pk %}">
                            <button slot="trigger" class="pf-c-button pf-m-tertiary">
                                {% trans 'Test' %}
                            </button>
                            <div slot="modal"></div>
                        </pb-modal-button>
                        <pb-modal-button href="{% url 'passbook_admin:policy-delete' pk=policy.pk %}">
                            <button slot="trigger" class="pf-c-button pf-m-danger">
                                {% trans 'Delete' %}
                            </button>
                            <div slot="modal"></div>
                        </pb-modal-button>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
        <div class="pf-c-pagination pf-m-bottom">
            {% include 'partials/pagination.html' %}
        </div>
        {% else %}
        <div class="pf-c-toolbar">
            <div class="pf-c-toolbar__content">
                {% include 'partials/toolbar_search.html' %}
            </div>
        </div>
        <div class="pf-c-empty-state">
            <div class="pf-c-empty-state__content">
                <i class="pf-icon pf-icon-infrastructure pf-c-empty-state__icon" aria-hidden="true"></i>
                <h1 class="pf-c-title pf-m-lg">
                    {% trans 'No Policies.' %}
                </h1>
                <div class="pf-c-empty-state__body">
                {% if request.GET.search != "" %}
                    {% trans "Your search query doesn't match any policies." %}
                {% else %}
                    {% trans 'Currently no policies exist. Click the button below to create one.' %}
                {% endif %}
                </div>
                <pb-dropdown class="pf-c-dropdown">
                    <button class="pf-m-primary pf-c-dropdown__toggle" type="button">
                        <span class="pf-c-dropdown__toggle-text">{% trans 'Create' %}</span>
                        <i class="fas fa-caret-down pf-c-dropdown__toggle-icon" aria-hidden="true"></i>
                    </button>
                    <ul class="pf-c-dropdown__menu" hidden>
                        {% for type, name in types.items %}
                        <li>
                            <pb-modal-button href="{% url 'passbook_admin:policy-create' %}?type={{ type }}">
                                <button slot="trigger" class="pf-c-dropdown__menu-item">
                                    {{ name|verbose_name }}<br>
                                    <small>
                                        {{ name|doc }}
                                    </small>
                                </button>
                                <div slot="modal"></div>
                            </pb-modal-button>
                        </li>
                        {% endfor %}
                    </ul>
                </pb-dropdown>
            </div>
        </div>
        {% endif %}
    </div>
</section>
{% endblock %}