{% extends 'base.html' %}

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

{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
    <h1 class="h3 mb-0">
        <i class="bi bi-people me-2"></i>
        User Management
    </h1>
    {% if user.is_superuser %}
    <a href="#" class="btn btn-primary">
        <i class="bi bi-plus-circle me-2"></i>
        New User
    </a>
    {% endif %}
</div>

<div class="card">
    <div class="card-body">
        {% if users %}
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead class="table-light">
                        <tr>
                            <th>Username</th>
                            <th>Full Name</th>
                            <th>Email</th>
                            <th>User Type</th>
                            <th>Department</th>
                            <th>Status</th>
                            <th>Last Login</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for user_obj in users %}
                        <tr>
                            <td>
                                <strong>{{ user_obj.username }}</strong>
                            </td>
                            <td>{{ user_obj.get_full_name|default:"Not set" }}</td>
                            <td>{{ user_obj.email|default:"Not set" }}</td>
                            <td>
                                <span class="badge bg-{% if user_obj.user_type == 'admin' %}danger{% elif user_obj.user_type == 'manager' %}primary{% else %}secondary{% endif %}">
                                    {{ user_obj.get_user_type_display }}
                                </span>
                            </td>
                            <td>{{ user_obj.department|default:"Not set" }}</td>
                            <td>
                                <span class="badge bg-{% if user_obj.is_active %}success{% else %}secondary{% endif %}">
                                    {{ user_obj.is_active|yesno:"Active,Inactive" }}
                                </span>
                            </td>
                            <td>{{ user_obj.last_login|date:"d/m/Y H:i"|default:"Never" }}</td>
                            <td>
                                <a href="{% url 'users:user_detail' user_obj.pk %}" class="btn btn-sm btn-outline-primary">
                                    <i class="bi bi-eye"></i>
                                </a>
                                {% if user.is_superuser %}
                                <a href="#" class="btn btn-sm btn-outline-warning ms-1">
                                    <i class="bi bi-pencil"></i>
                                </a>
                                {% endif %}
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            
            {% if is_paginated %}
            <nav aria-label="Page navigation" class="mt-4">
                <ul class="pagination justify-content-center">
                    {% if page_obj.has_previous %}
                        <li class="page-item">
                            <a class="page-link" href="?page={{ page_obj.previous_page_number }}">Previous</a>
                        </li>
                    {% endif %}
                    
                    <li class="page-item active">
                        <span class="page-link">Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}</span>
                    </li>
                    
                    {% if page_obj.has_next %}
                        <li class="page-item">
                            <a class="page-link" href="?page={{ page_obj.next_page_number }}">Next</a>
                        </li>
                    {% endif %}
                </ul>
            </nav>
            {% endif %}
        {% else %}
            <div class="text-center py-5">
                <i class="bi bi-people display-1 text-muted"></i>
                <h4 class="mt-3 text-muted">No Users Found</h4>
                <p class="text-muted">No users have been created yet.</p>
            </div>
        {% endif %}
    </div>
</div>
{% endblock %}
