{% extends "system_settings/base_settings.html" %}

{% block title %}Управление ролями пользователей{% endblock %}

{% block settings_content %}
<div class="d-flex justify-content-between align-items-center mb-4">
    <h3>Управление ролями пользователей</h3>
    <a href="{% url 'system_settings:user_roles:create' %}" class="btn btn-primary">
        <i class="bi bi-plus-circle"></i> Добавить пользователя
    </a>
</div>

{% if messages %}
{% for message in messages %}
<div class="alert alert-{{ message.tags }} alert-dismissible fade show" role="alert">
    {{ message }}
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
{% endfor %}
{% endif %}

<!-- Фильтр по активности -->
<div class="card mb-3">
    <div class="card-body">
        <form method="get" class="row g-3 align-items-center">
            <div class="col-auto">
                <div class="form-check">
                    <input class="form-check-input" type="checkbox" id="show_inactive" name="show_inactive" value="true"
                        {% if show_inactive %}checked{% endif %} onchange="this.form.submit()">
                    <label class="form-check-label" for="show_inactive">
                        Показать неактивных пользователей
                    </label>
                </div>
            </div>
        </form>
    </div>
</div>

<div class="card">
    <div class="card-body">
        {% if user_roles %}
        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>Email</th>
                        <th>Имя</th>
                        <th>Роль</th>
                        <th>Статус</th>
                        <th>Создан</th>
                        <th>Создал</th>
                        <th>Действия</th>
                    </tr>
                </thead>
                <tbody>
                    {% for user_role in user_roles %}
                    <tr>
                        <td>{{ user_role.user.email }}</td>
                        <td>{{ user_role.user.name }}</td>
                        <td>
                            <span class="badge
                                        {% if user_role.role.code == 'owner' %}bg-danger
                                        {% elif user_role.role.code == 'manager' %}bg-primary
                                        {% elif user_role.role.code == 'florist' %}bg-success
                                        {% elif user_role.role.code == 'courier' %}bg-info
                                        {% endif %}">
                                {{ user_role.role.name }}
                            </span>
                        </td>
                        <td>
                            {% if not user_role.is_active or not user_role.user.is_active %}
                            <span class="badge bg-secondary">Неактивен</span>
                            {% else %}
                            <span class="badge bg-success">Активен</span>
                            {% endif %}
                        </td>
                        <td>{{ user_role.created_at|date:"d.m.Y H:i" }}</td>
                        <td>
                            {% if user_role.created_by %}
                            {{ user_role.created_by.name }}
                            {% else %}
                            —
                            {% endif %}
                        </td>
                        <td>
                            {% if user_role.user != request.user %}
                            {% if user_role.is_active and user_role.user.is_active %}
                            <a href="{% url 'system_settings:user_roles:edit' user_role.pk %}"
                                class="btn btn-sm btn-outline-primary">
                                <i class="bi bi-pencil"></i> Изменить
                            </a>
                            <a href="{% url 'system_settings:user_roles:delete' user_role.pk %}"
                                class="btn btn-sm btn-outline-danger">
                                <i class="bi bi-x-circle"></i> Деактивировать
                            </a>
                            {% else %}
                            <a href="{% url 'system_settings:user_roles:reactivate' user_role.pk %}"
                                class="btn btn-sm btn-outline-success">
                                <i class="bi bi-arrow-clockwise"></i> Реактивировать
                            </a>
                            {% endif %}
                            {% else %}
                            <span class="text-muted small">
                                <i class="bi bi-lock"></i> Ваша роль
                            </span>
                            {% endif %}
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
        {% else %}
        <div class="text-center py-5">
            <p class="text-muted">Нет пользователей с ролями</p>
            <a href="{% url 'system_settings:user_roles:create' %}" class="btn btn-primary">
                Добавить первого пользователя
            </a>
        </div>
        {% endif %}
    </div>
</div>
{% endblock %}