<!-- templates/accounts/user_management.html -->
{% extends 'core/base.html' %}
{% load static %}

{% block title %}User Management - SwiftPOS{% endblock %}

{% block topbar_title %}
User Management
{% endblock %}

{% block content %}
<div class="container-fluid px-0">

    <div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="mb-0">Users</h5>

    {% if can_add_user %}
        <a href="{% url 'create_user' %}"
           class="btn btn-primary btn-sm">
            Add User
        </a>
    {% else %}
        <button class="btn btn-secondary btn-sm" disabled>
            Add User
        </button>
    {% endif %}
</div>

{% if not can_add_user and user_limit_message %}
    <div class="alert alert-warning py-2 small mb-3">
        {{ user_limit_message }}
        {% if max_users %}
            <br>
            <span class="text-muted">
                Current active users: {{ current_users_count }} / {{ max_users }}
            </span>
        {% endif %}
    </div>
{% endif %}


    <!-- Toast Messages -->
    <div class="swiftpos-toast-container">
        {% if messages %}
            {% for message in messages %}
                <div class="swiftpos-toast swiftpos-toast-{{ message.tags }}">
                    <div class="toast-icon">
                        {% if message.tags == 'success' %}
                            <i class="fas fa-check-circle"></i>
                        {% elif message.tags == 'error' or message.tags == 'danger' %}
                            <i class="fas fa-exclamation-circle"></i>
                        {% elif message.tags == 'warning' %}
                            <i class="fas fa-exclamation-triangle"></i>
                        {% else %}
                            <i class="fas fa-info-circle"></i>
                        {% endif %}
                    </div>
                    <div class="toast-text">
                        {{ message }}
                    </div>
                </div>
            {% endfor %}
        {% endif %}
    </div>

    <div class="card">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h6 class="card-title mb-0 d-flex align-items-center gap-2">
                <i class="fas fa-users"></i> User List
            </h6>
        </div>
        <div class="card-body p-0">
            <div class="sp-table-container">
                <table class="table table-hover align-middle mb-0">
                    <thead>
                        <tr>
                            <th>User</th>
                            <th class="d-none d-md-table-cell">Contact</th>
                            <th>Role</th>
                            <th>Status</th>
                            <th class="text-end">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for user in users %}
                        <tr>
                            <td>
                                <div class="fw-semibold small">
                                    {{ user.username }}
                                </div>
                                <div class="text-muted small d-md-none">
                                    {{ user.email|default:"No email" }}
                                </div>
                            </td>

                            <td class="d-none d-md-table-cell">
                                <div class="small">
                                    {{ user.email|default:"No email" }}
                                </div>
                                <div class="text-muted small">
                                    {{ user.phone|default:"No phone" }}
                                </div>
                            </td>

                            <td>
                                {# Role badge – colour by role #}
                                {% if user.role == 'admin' %}
                                    <span class="badge bg-danger-subtle">Admin</span>
                                {% elif user.role == 'manager' %}
                                    <span class="badge bg-info-subtle">Manager</span>
                                {% elif user.role == 'cashier' %}
                                    <span class="badge bg-success-subtle">Cashier</span>
                                {% else %}
                                    <span class="badge bg-secondary"> {{ user.get_role_display }} </span>
                                {% endif %}
                            </td>

                            <td>
                                {% if user.is_active %}
                                    <span class="badge bg-success-subtle">Active</span>
                                {% else %}
                                    <span class="badge bg-danger-subtle">Inactive</span>
                                {% endif %}
                            </td>

                            <td class="text-end">
                                <div class="btn-group btn-group-sm" role="group">
                                    <a href="{% url 'edit_user' user.id %}"
                                       class="btn btn-outline-secondary"
                                       title="Edit user">
                                        <i class="fas fa-pen"></i>
                                    </a>
                                    <form method="post"
                                          action="{% url 'delete_user' user.id %}"
                                          class="d-inline"
                                          onsubmit="return confirm('Are you sure you want to delete this user?')">
                                        {% csrf_token %}
                                        <button type="submit"
                                                class="btn btn-outline-danger"
                                                title="Delete user">
                                            <i class="fas fa-trash"></i>
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="5" class="text-center text-muted small py-4">
                                No users found. Click
                                <a href="{% url 'create_user' %}">Add User</a> to create one.
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
{% endblock %}
